Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert CImageList image to transparent GIF

Convert CImageList image to transparent GIF

Scheduled Pinned Locked Moved C / C++ / MFC
graphicswinformscom
12 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mark Salsbery

    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:

    N Offline
    N Offline
    Neville Franks
    wrote on last edited by
    #3

    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

    M 2 Replies Last reply
    0
    • N Neville Franks

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #4

      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:

      1 Reply Last reply
      0
      • N Neville Franks

        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

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #5

        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:

        N 1 Reply Last reply
        0
        • M Mark Salsbery

          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:

          N Offline
          N Offline
          Neville Franks
          wrote on last edited by
          #6

          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

          M 1 Reply Last reply
          0
          • N Neville Franks

            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

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #7

            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:

            N 1 Reply Last reply
            0
            • M Mark Salsbery

              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:

              N Offline
              N Offline
              Neville Franks
              wrote on last edited by
              #8

              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

              M 1 Reply Last reply
              0
              • N Neville Franks

                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

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #9

                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:

                N 2 Replies Last reply
                0
                • M Mark Salsbery

                  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:

                  N Offline
                  N Offline
                  Neville Franks
                  wrote on last edited by
                  #10

                  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

                  1 Reply Last reply
                  0
                  • M Mark Salsbery

                    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:

                    N Offline
                    N Offline
                    Neville Franks
                    wrote on last edited by
                    #11

                    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

                    M 1 Reply Last reply
                    0
                    • N Neville Franks

                      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

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #12

                      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:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups