Question : Convert image to 8bit transparent png in dot net

I am using FreeImage dll to create 8bpp transparent png. I got the following code to convert image in C++ using FreeImage.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
#include "FreeImage.h"
int main(int argc, char* argv[]) {
FIBITMAP *hDIB24bpp = FreeImage_Load(FIF_BMP, "test.bmp", 0);
if (hDIB24bpp) {
// color-quantize 24bpp (results in a 8bpp bitmap to set transparency)
FIBITMAP *hDIB8bpp = FreeImage_ColorQuantize(hDIB24bpp, FIQ_WUQUANT);
// get palette and find bright green
RGBQUAD *Palette = FreeImage_GetPalette(hDIB8bpp);
BYTE Transparency[256];
for (unsigned i = 0; i < 256; i++) {
Transparency[i] = 0xFF;
if (Palette[i].rgbGreen >= 0xFE &&
Palette[i].rgbBlue == 0x00 &&
Palette[i].rgbRed == 0x00) {
Transparency[i] = 0x00;
}
}
// set the tranparency table
FreeImage_SetTransparencyTable(hDIB8bpp, Transparency, 256);
// save 8bpp image as transparent PNG
FreeImage_Save(FIF_PNG, hDIB8bpp, "test.png", 0);
FreeImage_Unload(hDIB24bpp);
FreeImage_Unload(hDIB8bpp);
}
return 0;
}



After converting this code in C#.net, m getting error. The error is
 "Cannot implicitly convert type 'System.IntPtr' to 'FreeImageAPI.RGBQUAD" in this line
"RGBQUAD Palette = FreeImage.GetPalette(hDIB8bpp);". While the same code is given in C++ documenation

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
FIBITMAP hDIB24bpp = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_JPEG, "C:/Sunset.jpg", 0);
            FIBITMAP hDIB8bpp = FreeImage.ColorQuantize(hDIB24bpp, FREE_IMAGE_QUANTIZE.FIQ_WUQUANT);
            RGBQUAD Palette = FreeImage.GetPalette(hDIB8bpp);
            byte[] transparency = new byte[256];
            for (int i = 0; i < 256; i++)
            {
                transparency[i] = 0xFF;
                if (Palette[i].rgbGreen >= 0xFE &&
                Palette[i].rgbBlue == 0x00 &&
                Palette[i].rgbRed == 0x00)
                {
                    transparency[i] = 0x00;
                }
            }

Answer : Convert image to 8bit transparent png in dot net

Try this code to get the actual RGBQUAD data.
1:
2:
RGBQUAD *pPalette = FreeImage.GetPalette(hDIB8bpp);
RGBQUAD Palette = *pPalette;
Random Solutions  
 
programming4us programming4us