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. Using a mask for drawing non rectangular bitmaps

Using a mask for drawing non rectangular bitmaps

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpcsharpquestion
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.
  • W Offline
    W Offline
    Ward 0
    wrote on last edited by
    #1

    I have a problem with drawing a non rectangular bitmap using a mask bitmap. The two bitmaps are derived from an icon file in the resources. I want to create a HBITMAP object that contains the correct result of the masked drawing. HICON hIcon5 = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON12)); GetIconInfo(hIcon5, &iinfo);//retreive the BITMAP bm; GetObject(iinfo.hbmColor,sizeof(bm),&bm); HDC hdcToolbar = GetDC(hToolbar);//hToolbar is a window HDC hdcBitmap5 = CreateCompatibleDC(hdcToolbar); HDC hdcMask = CreateCompatibleDC(hdcToolbar) ; HDC hdcColor = CreateCompatibleDC(hdcToolbar) ; HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight); HGDIOBJ hOldBitmap5 = SelectObject(hdcBitmap5, hBitmap5); HGDIOBJ hOldMask = SelectObject(hdcMask, iinfo.hbmMask) ; HGDIOBJ hOldColor = SelectObject(hdcColor, iinfo.hbmColor); //Now comes the difficult part where I can't find the solution BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, NULL, 0, 0, WHITENESS); BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcMask, 0, 0, 0x220326);//found this code in Pedzold, chapter 14) BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcColor, 0, 0, SRCPAINT) ; SelectObject(hdcMask, hOldMask) ; SelectObject(hdcColor, hOldColor); SelectObject(hdcBitmap5, hOldBitmap5); DeleteDC(hdcMask); DeleteDC(hdcColor); DeleteDC(hdcBitmap5); ReleaseDC(hToolbar, hdcToolbar); Download the icon i used for the test here Can someone help me please? thanks in advance, Ward

    PJ ArendsP 1 Reply Last reply
    0
    • W Ward 0

      I have a problem with drawing a non rectangular bitmap using a mask bitmap. The two bitmaps are derived from an icon file in the resources. I want to create a HBITMAP object that contains the correct result of the masked drawing. HICON hIcon5 = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON12)); GetIconInfo(hIcon5, &iinfo);//retreive the BITMAP bm; GetObject(iinfo.hbmColor,sizeof(bm),&bm); HDC hdcToolbar = GetDC(hToolbar);//hToolbar is a window HDC hdcBitmap5 = CreateCompatibleDC(hdcToolbar); HDC hdcMask = CreateCompatibleDC(hdcToolbar) ; HDC hdcColor = CreateCompatibleDC(hdcToolbar) ; HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight); HGDIOBJ hOldBitmap5 = SelectObject(hdcBitmap5, hBitmap5); HGDIOBJ hOldMask = SelectObject(hdcMask, iinfo.hbmMask) ; HGDIOBJ hOldColor = SelectObject(hdcColor, iinfo.hbmColor); //Now comes the difficult part where I can't find the solution BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, NULL, 0, 0, WHITENESS); BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcMask, 0, 0, 0x220326);//found this code in Pedzold, chapter 14) BitBlt (hdcBitmap5, 0, 0, bm.bmWidth,bm.bmHeight, hdcColor, 0, 0, SRCPAINT) ; SelectObject(hdcMask, hOldMask) ; SelectObject(hdcColor, hOldColor); SelectObject(hdcBitmap5, hOldBitmap5); DeleteDC(hdcMask); DeleteDC(hdcColor); DeleteDC(hdcBitmap5); ReleaseDC(hToolbar, hdcToolbar); Download the icon i used for the test here Can someone help me please? thanks in advance, Ward

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #2

      Why not just use DrawIcon()?

      Ward wrote:

      HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight);

      At this point in your code hdcBitmap5 has a one by one pixel monchrome bitmap selected into it. When you create a compatible bitmap it will also be a monochrome bitmap. You should use hdcToolbar as the HDC for creating compatible bitmaps. [blatant self-promotion] Have a look at http://www.codeproject.com/tools/imageviewer.asp[^] [/blatant self-promotion]


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

      Within you lies the power for good; Use it!

      W 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        Why not just use DrawIcon()?

        Ward wrote:

        HBITMAP hBitmap5 = CreateCompatibleBitmap(hdcBitmap5,bm.bmWidth,bm.bmHeight);

        At this point in your code hdcBitmap5 has a one by one pixel monchrome bitmap selected into it. When you create a compatible bitmap it will also be a monochrome bitmap. You should use hdcToolbar as the HDC for creating compatible bitmaps. [blatant self-promotion] Have a look at http://www.codeproject.com/tools/imageviewer.asp[^] [/blatant self-promotion]


        "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

        W Offline
        W Offline
        Ward 0
        wrote on last edited by
        #3

        Well, at first I've tried DrawIcon, but when I wanted to use the bitmap hBitmap5 as the bitmap for a toolbar button (used in TB_ADDBUTTON-message) the area which was supposed to be the background color of the toolbarbuttons, were turned in white. I did found a solution last friday (at last). First I fill the hBitmap5 with the color of the toolbar buttons. The color of the toolbar buttons I was able to retreive using GetSysColor. Then I used the DrawIcon on this hBitmap5. And finally I passed this hBitmap5 to the toolbar with the TB_ADDBUTTON-message. The final result is now exact what I wanted. Anyway, thanks for your reply. kind regards, Ward -- modified at 16:09 Sunday 25th December, 2005

        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