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. How to save TransParent IMage after drawing on another image?

How to save TransParent IMage after drawing on another image?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestion
3 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.
  • 0 Offline
    0 Offline
    002comp
    wrote on last edited by
    #1

    Hello Friends I am using GDIplus to save an Image.I am getting the Graphics reference from Image not from Device.And then m drawing another image of same size (White background with one Red Rectangle) by using DrawImage method of Graphics. Here is the code below:

    CString FileName = "c:\\temp.TIF";
    LPCSTR cstr=FileName;
    int maxlen=FileName.GetLength()*2;
    WCHAR *str=new WCHAR[maxlen];
    MultiByteToWideChar(CP_ACP,0,cstr,-1,str,maxlen);
    Image image(cstr, FALSE);
    Graphics g(&image);

    CString str = "c:\\rectangle.TIF";
    Bitmap bmp(str);

    ImageAttributes attr;
    Gdiplus::ColorMatrix cm =
    {
    1.0f, 0.0f, 0.0f, 0, 0,
    0.0f, 1.0f, 0.0f, 0, 0,
    0.0f, 0.0f, 1.0f, 0, 0,
    0.0f, 0.0f, 0.0f, 0.6f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f, 1.0f
    };
    attr.SetColorMatrix(&cm, Gdiplus::ColorMatrixFlagsDefault,Gdiplus::ColorAdjustTypeBitmap);
    g.DrawImage(&bmp,Gdiplus::Rect(0, 0, bmp.GetWidth(), bmp.GetHeight()), 0,0, bmp.GetWidth(), bmp.GetHeight(),UnitPixel,&attr);
    bmp.Save(str,pClsid,NULL);

    I used ImageAttributes for Transparency but by using setColorMatrix,It sets transparency for whole image. Now,I have two problems: 1)I want to set transparency for White pixels so that when I draw rectangle.tif on temp.tif file I can see background.I want to use as layer. 2)When I draw rectangle with ImageAttribute on temp file and then its not saving with transparency info even. Any Ideas Thanks In Advance. Regards Yogesh

    R 1 Reply Last reply
    0
    • 0 002comp

      Hello Friends I am using GDIplus to save an Image.I am getting the Graphics reference from Image not from Device.And then m drawing another image of same size (White background with one Red Rectangle) by using DrawImage method of Graphics. Here is the code below:

      CString FileName = "c:\\temp.TIF";
      LPCSTR cstr=FileName;
      int maxlen=FileName.GetLength()*2;
      WCHAR *str=new WCHAR[maxlen];
      MultiByteToWideChar(CP_ACP,0,cstr,-1,str,maxlen);
      Image image(cstr, FALSE);
      Graphics g(&image);

      CString str = "c:\\rectangle.TIF";
      Bitmap bmp(str);

      ImageAttributes attr;
      Gdiplus::ColorMatrix cm =
      {
      1.0f, 0.0f, 0.0f, 0, 0,
      0.0f, 1.0f, 0.0f, 0, 0,
      0.0f, 0.0f, 1.0f, 0, 0,
      0.0f, 0.0f, 0.0f, 0.6f, 0.0f,
      0.0f, 0.0f, 0.0f, 0.0f, 1.0f
      };
      attr.SetColorMatrix(&cm, Gdiplus::ColorMatrixFlagsDefault,Gdiplus::ColorAdjustTypeBitmap);
      g.DrawImage(&bmp,Gdiplus::Rect(0, 0, bmp.GetWidth(), bmp.GetHeight()), 0,0, bmp.GetWidth(), bmp.GetHeight(),UnitPixel,&attr);
      bmp.Save(str,pClsid,NULL);

      I used ImageAttributes for Transparency but by using setColorMatrix,It sets transparency for whole image. Now,I have two problems: 1)I want to set transparency for White pixels so that when I draw rectangle.tif on temp.tif file I can see background.I want to use as layer. 2)When I draw rectangle with ImageAttribute on temp file and then its not saving with transparency info even. Any Ideas Thanks In Advance. Regards Yogesh

      R Offline
      R Offline
      Rozis
      wrote on last edited by
      #2

      I hope i understand your questions clearly. Im not using GDIplus (but GDI) and Im not using your program language, but maybe this helps a little: - to get transparency on the pixellevel every pixel in your bitmap must be 4 bytes (RGBA) and not 3 bytes (RGB). See MSDN for exact info. In the A byte (called alpha channel) you may set the transparency level between 0 and 255. You should use the winapi function Alphablend() (or the gdiplus equevalent) to copy the bitmap to the screen. - One of the problems with transparent bitmaps is that when you put them on screen the transparency is visible but this information is lost (on the screen). Let me explain this: Suppose you want to fade in a bitmap. Then for every step you must first repaint the background (that what wad on the screen before the bitmap was painted on it) and then copy the transparent bitmap on it, decreasing transparency in every step. If you don't repaint the background first you will not get the desired result (but a transparent bitmap on a tranparent bitmap). The screen has no transparency set (it is a RGB-surface) so saving it will result in a bitmap that only shows 'what was on screen'. Transparency is a property of a bitmap not of the screen that displays it (the screen shows the result only). Hope this helps a bit... Good luck, Rozis

      0 1 Reply Last reply
      0
      • R Rozis

        I hope i understand your questions clearly. Im not using GDIplus (but GDI) and Im not using your program language, but maybe this helps a little: - to get transparency on the pixellevel every pixel in your bitmap must be 4 bytes (RGBA) and not 3 bytes (RGB). See MSDN for exact info. In the A byte (called alpha channel) you may set the transparency level between 0 and 255. You should use the winapi function Alphablend() (or the gdiplus equevalent) to copy the bitmap to the screen. - One of the problems with transparent bitmaps is that when you put them on screen the transparency is visible but this information is lost (on the screen). Let me explain this: Suppose you want to fade in a bitmap. Then for every step you must first repaint the background (that what wad on the screen before the bitmap was painted on it) and then copy the transparent bitmap on it, decreasing transparency in every step. If you don't repaint the background first you will not get the desired result (but a transparent bitmap on a tranparent bitmap). The screen has no transparency set (it is a RGB-surface) so saving it will result in a bitmap that only shows 'what was on screen'. Transparency is a property of a bitmap not of the screen that displays it (the screen shows the result only). Hope this helps a bit... Good luck, Rozis

        0 Offline
        0 Offline
        002comp
        wrote on last edited by
        #3

        Thanks Rozis and It helps a lot and am looking further.

        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