如何使用PixelFormat从PictureBox获取图像:Format8bppIndexed

时间:2021-01-20 16:00:04

how to get image from PictureBox with Format8bppRgb?? becouse this function return Format24bppRgb:

如何使用Format8bppRgb从PictureBox获取图像?因为这个函数返回Format24bppRgb:

Bitmap ^getBitmap8() {
Bitmap ^tmpBmp = gcnew Bitmap(pictureBox1->Image->Width, pictureBox1->Image->Height, System::Drawing::Imaging::PixelFormat::Format8bppIndexed);
tmpBmp = dynamic_cast<Bitmap^>(pictureBox1->Image); <- this one change PixelFormat from 8 to 24bpp

Rectangle rec = Rectangle(0,0,tmpBmp->Width, tmpBmp->Height);
tmpBmp->Clone(rec, System::Drawing::Imaging::PixelFormat::Format8bppIndexed); <- this one doesn't work 

return tmpBmp;
}

help, i need to return Bitmap tmpBmp with Format8bppRGB to use aFormge filter.

帮助,我需要使用Format8bppRGB返回Bitmap tmpBmp以使用aFormge过滤器。

1 个解决方案

#1


0  

ok, i've just solved the problem. If someone needs:

好的,我刚刚解决了这个问题。如果有人需要:

Bitmap ^ConvertTo8bpp() {

    Bitmap ^originalImage = gcnew Bitmap(pictureBox1->Image->Width, pictureBox1->Image->Height, PixelFormat::Format8bppIndexed);
    originalImage = dynamic_cast<Bitmap^>(pictureBox1->Image);

    Bitmap ^newImage = gcnew Bitmap(pictureBox1->Image->Width, pictureBox1->Image->Height, PixelFormat::Format8bppIndexed);

    Rectangle rec = Rectangle(0, 0, newImage->Width, newImage->Height);

    BitmapData^ originalData = originalImage->LockBits(rec, System::Drawing::Imaging::ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb);

    BitmapData^ newData = newImage->LockBits(rec, System::Drawing::Imaging::ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb);

    int originalStride = originalData->Stride;
    System::IntPtr originalScan0 = originalData->Scan0;

    int newStride = newData->Stride;
    System::IntPtr newScan0 = newData->Scan0;

    unsigned char* pOriginal = (unsigned char*)(void*)originalScan0;
    unsigned char* pNew = (unsigned char*)(void*)newScan0;

    int nOffset = originalStride - newImage->Width *3;
    unsigned char red, green, blue;

    for(int y=0; y<newImage->Height; ++y) {
        for(int x=0; x<newImage->Width; ++x) {
            blue = pOriginal[0];
            green = pOriginal[1];
            red = pOriginal[2];

            unsigned char newPixel = System::Convert::ToByte((red+green+blue) / 3);

            pNew[0] = newPixel;
            pNew[1] = newPixel;
            pNew[2] = newPixel;

            pOriginal += 3;
            pNew += 3;
        }
        pOriginal += nOffset;
        pNew += nOffset;
    }

    originalImage->UnlockBits(originalData);
    newImage->UnlockBits(newData);
    return newImage;
}

#1


0  

ok, i've just solved the problem. If someone needs:

好的,我刚刚解决了这个问题。如果有人需要:

Bitmap ^ConvertTo8bpp() {

    Bitmap ^originalImage = gcnew Bitmap(pictureBox1->Image->Width, pictureBox1->Image->Height, PixelFormat::Format8bppIndexed);
    originalImage = dynamic_cast<Bitmap^>(pictureBox1->Image);

    Bitmap ^newImage = gcnew Bitmap(pictureBox1->Image->Width, pictureBox1->Image->Height, PixelFormat::Format8bppIndexed);

    Rectangle rec = Rectangle(0, 0, newImage->Width, newImage->Height);

    BitmapData^ originalData = originalImage->LockBits(rec, System::Drawing::Imaging::ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb);

    BitmapData^ newData = newImage->LockBits(rec, System::Drawing::Imaging::ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb);

    int originalStride = originalData->Stride;
    System::IntPtr originalScan0 = originalData->Scan0;

    int newStride = newData->Stride;
    System::IntPtr newScan0 = newData->Scan0;

    unsigned char* pOriginal = (unsigned char*)(void*)originalScan0;
    unsigned char* pNew = (unsigned char*)(void*)newScan0;

    int nOffset = originalStride - newImage->Width *3;
    unsigned char red, green, blue;

    for(int y=0; y<newImage->Height; ++y) {
        for(int x=0; x<newImage->Width; ++x) {
            blue = pOriginal[0];
            green = pOriginal[1];
            red = pOriginal[2];

            unsigned char newPixel = System::Convert::ToByte((red+green+blue) / 3);

            pNew[0] = newPixel;
            pNew[1] = newPixel;
            pNew[2] = newPixel;

            pOriginal += 3;
            pNew += 3;
        }
        pOriginal += nOffset;
        pNew += nOffset;
    }

    originalImage->UnlockBits(originalData);
    newImage->UnlockBits(newData);
    return newImage;
}