Convert CImageList image to transparent GIF
-
Hi, I want to extract images from a CImageList and save them as transparent GIF files. I'm using the following GDI+ code and the transparent area comes out black.
`const HICON hIcon = myImageList.ExtractIcon( nImage ); Bitmap* gdiBMP = Bitmap::FromHICON( hIcon ); CLSID encoderClsid; GetEncoderClsid( T2W( "image/gif" ), &encoderClsid ); Status stat = gdiBMP->Save( L"myfilename.gif", &encoderClsid, NULL ); delete gdiBMP;`
Have Google'd and searched CP to no avail. Any/all suggestions most welcome.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
Hi, I want to extract images from a CImageList and save them as transparent GIF files. I'm using the following GDI+ code and the transparent area comes out black.
`const HICON hIcon = myImageList.ExtractIcon( nImage ); Bitmap* gdiBMP = Bitmap::FromHICON( hIcon ); CLSID encoderClsid; GetEncoderClsid( T2W( "image/gif" ), &encoderClsid ); Status stat = gdiBMP->Save( L"myfilename.gif", &encoderClsid, NULL ); delete gdiBMP;`
Have Google'd and searched CP to no avail. Any/all suggestions most welcome.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
Does something like this work?
IMAGEINFO imageinfo;
myImageList.GetImageInfo(nImage, &imageinfo);int width = imageinfo.rcImage.right - imageinfo.rcImage.left;
int height = imageinfo.rcImage.bottom - imageinfo.rcImage.top;Bitmap TransparentBitmap(width, height, PixelFormat32bppARGB);
Graphics *pBitmapGraphics = Graphics::FromImage(&TransparentBitmap);
pBitmapGraphics->SetCompositingMode(CompositingModeSourceCopy);SolidBrush TransparentBrush(Gdiplus::Color::Transparent);
pBitmapGraphics->FillRectangle(&TransparentBrush, 0, 0, width, height);
HDC hdc = pBitmapGraphics->GetHDC();
ImageList_Draw(myImageList, nImage, hdc, 0, 0, ILD_TRANSPARENT);
pBitmapGraphics->ReleaseHDC(hdc);
delete pBitmapGraphics;
CLSID encoderClsid;
GetEncoderClsid( T2W( "image/gif" ), &encoderClsid );
Status stat = TransparentBitmap.Save( L"myfilename.gif", &encoderClsid, NULL );Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Does something like this work?
IMAGEINFO imageinfo;
myImageList.GetImageInfo(nImage, &imageinfo);int width = imageinfo.rcImage.right - imageinfo.rcImage.left;
int height = imageinfo.rcImage.bottom - imageinfo.rcImage.top;Bitmap TransparentBitmap(width, height, PixelFormat32bppARGB);
Graphics *pBitmapGraphics = Graphics::FromImage(&TransparentBitmap);
pBitmapGraphics->SetCompositingMode(CompositingModeSourceCopy);SolidBrush TransparentBrush(Gdiplus::Color::Transparent);
pBitmapGraphics->FillRectangle(&TransparentBrush, 0, 0, width, height);
HDC hdc = pBitmapGraphics->GetHDC();
ImageList_Draw(myImageList, nImage, hdc, 0, 0, ILD_TRANSPARENT);
pBitmapGraphics->ReleaseHDC(hdc);
delete pBitmapGraphics;
CLSID encoderClsid;
GetEncoderClsid( T2W( "image/gif" ), &encoderClsid );
Status stat = TransparentBitmap.Save( L"myfilename.gif", &encoderClsid, NULL );Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi Mark, Thanks for the reply. I've got a feeling your code isn't the answer, but I'll give it a try.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
Hi Mark, Thanks for the reply. I've got a feeling your code isn't the answer, but I'll give it a try.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
Good luck :) There's a little trial and error involved in figuring out which combination of GDI and GDI+ calls preserve alpha and transparency data. It may look ridiculously complicated for a seemingly simple operation, but with GDI especially, alpha and transparency data is pretty much ignored except with DIBSections. Please let me know what works for you! Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi Mark, Thanks for the reply. I've got a feeling your code isn't the answer, but I'll give it a try.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
BTW, try just drawing the icon anywhere on a window or the screen right after the ExtractIcon call. Does it still have transparency? If not - that's where you're losing the "background". If so, then you're losing transparency when crossing the GDI-GDI+ line when you call FromHICON(). Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
BTW, try just drawing the icon anywhere on a window or the screen right after the ExtractIcon call. Does it still have transparency? If not - that's where you're losing the "background". If so, then you're losing transparency when crossing the GDI-GDI+ line when you call FromHICON(). Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
BTW, try just drawing the icon anywhere on a window or the screen right after the ExtractIcon call. Does it still have transparency?
Good question. I suspect it doesn't have transparency at this point. Comments in articles on CP refer to "black" pixels where the transparent pixels should be. I think I need to convert the C# code at http://www.bobpowell.net/giftransparency.htm[^] into C++, but I'm not a C# guy. I've started converting them by hand as quick stop-gap measure.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
Mark Salsbery wrote:
BTW, try just drawing the icon anywhere on a window or the screen right after the ExtractIcon call. Does it still have transparency?
Good question. I suspect it doesn't have transparency at this point. Comments in articles on CP refer to "black" pixels where the transparent pixels should be. I think I need to convert the C# code at http://www.bobpowell.net/giftransparency.htm[^] into C++, but I'm not a C# guy. I've started converting them by hand as quick stop-gap measure.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
Neville Franks wrote:
I suspect it doesn't have transparency at this point
Well, I tested it. Both of our versions of code keep the transparency fine. Of course, yours was much simpler :) The GDI+ Save code, however, ignores it. Same with PNG. Find a solution yet (beyond that link)? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Neville Franks wrote:
I suspect it doesn't have transparency at this point
Well, I tested it. Both of our versions of code keep the transparency fine. Of course, yours was much simpler :) The GDI+ Save code, however, ignores it. Same with PNG. Find a solution yet (beyond that link)? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I did find one other article which refactored Bob Powell's code, but it was still in C# and seemed to have problems. I also found and saved "Bitmaps with Transparency" http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnargdi/html/msdn_transblt.asp[^]. I've not had any time to try any moe coding.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
I did find one other article which refactored Bob Powell's code, but it was still in C# and seemed to have problems. I also found and saved "Bitmaps with Transparency" http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnargdi/html/msdn_transblt.asp[^]. I've not had any time to try any moe coding.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
I'm going to take a look at making a C++ version today. The selection of the transparent color should be automatic. FWIW, saving the icon as a TIFF preserves the transparency :rolleyes:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I'm going to take a look at making a C++ version today. The selection of the transparent color should be automatic. FWIW, saving the icon as a TIFF preserves the transparency :rolleyes:
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
I'm going to take a look at making a C++ version today.
Sounds great. Look forward to seeing how you go.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
I'm going to take a look at making a C++ version today. The selection of the transparent color should be automatic. FWIW, saving the icon as a TIFF preserves the transparency :rolleyes:
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi Mark, Did you ever get this written and working?
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
Hi Mark, Did you ever get this written and working?
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
OMG I never even looked at it again! Wow, that was really a year ago? Man, did I get sidetracked :)
Mark Salsbery Microsoft MVP - Visual C++ :java: