Advertisement

07.12.2008 at 12:50PM PDT, ID: 23559949
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.1

Creating mask bitmap not working

Asked by wdbdesign in Windows Programming, C# Programming Language, .NET Framework 2.x

(Repost of neglected question to different topic areas and upping point value)

I'm trying to create a monochrome mask of a 32 x 32 bitmap given a transparent color.  The result of the below function should be a monochrome bitmap where white replaces the transparent color, and black replaces all other pixels.

I've checked the return value of SetBkColor and BitBlt, and both are returning success.

Oddly, if I uncomment the GetPixel/SetPixel loop, the mask comes out perfectly.  I just can't see what's wrong with the blt.

If it would help to post the Win32 class I will, but it's fairly straightforward p/invoke stuff. Given that the GetPixel/SetPixel loop results in a good mask, the problem seems to be in the blt itself, or in the setting up of the blt.

Can someone see anything wrong here?  Thanks in advance for any help!
Start Free Trial
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:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
public static Bitmap CreateMask (Bitmap bmp, int crTransparent)
{
    // get the desktop dc
    IntPtr hDC = Win32.GetWindowDC(IntPtr.Zero);
 
    // create the source and mask dcs
    IntPtr hSrcDC = Win32.CreateCompatibleDC(hDC);
    IntPtr hMaskDC = Win32.CreateCompatibleDC(hDC);
 
    // create the mask
    IntPtr hSrcBmp = bmp.GetHbitmap();
    IntPtr hMaskBmp = Win32.CreateBitmap(bmp.Width, bmp.Height, 1, 1, IntPtr.Zero);
 
    // release the desktop dc
    Win32.ReleaseDC(IntPtr.Zero, hDC);
 
    // select the source and mask bmps
    IntPtr hOldSrcBitmap = Win32.SelectObject(hSrcDC, hSrcBmp);
    IntPtr hOldMaskBitmap = Win32.SelectObject(hMaskDC, hMaskBmp);
 
    // create mask
    int nOldBkColor = Win32.SetBkColor(hSrcDC, crTransparent);
    Win32.BitBlt(hMaskDC, 0, 0, bmp.Width, bmp.Height, hSrcDC, 0, 0, (int)Win32.ROP.SRCCOPY);
    Win32.SetBkColor(hSrcDC, nOldBkColor);
 
    /*
    for (int y = 0; y < 32; y++)
        for (int x = 0; x < 32; x++)
            Win32.SetPixel(hMaskDC, x, y, Win32.GetPixel(hSrcDC, x, y) == crTransparent ? 0x00FFFFFF : 0);
    */
 
    // clean up the dcs
    Win32.SelectObject(hSrcDC, hOldSrcBitmap);
    Win32.SelectObject(hMaskDC, hOldMaskBitmap);
 
    // Delete the helper DC
    Win32.DeleteDC(hMaskDC);
    Win32.DeleteDC(hSrcDC);
 
    // delete the source handle we created
    Win32.DeleteObject(hSrcBmp);
 
    // create a bitmap object from the handle
    Bitmap b = Bitmap.FromHbitmap(hMaskBmp);
    
    // finally, delete the mask handle
    Win32.DeleteObject(hMaskBmp);
 
    return b;
}
[+][-]07.13.2008 at 02:06PM PDT, ID: 21994204

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.13.2008 at 04:00PM PDT, ID: 21994661

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.13.2008 at 07:39PM PDT, ID: 21995312

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 12:00AM PDT, ID: 21996143

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 06:18AM PDT, ID: 21997749

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 10:33AM PDT, ID: 22000200

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 12:03PM PDT, ID: 22000965

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Windows Programming, C# Programming Language, .NET Framework 2.x
Sign Up Now!
Solution Provided By: JohnGaby
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.14.2008 at 12:16PM PDT, ID: 22001073

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 12:39PM PDT, ID: 22001325

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.14.2008 at 05:18PM PDT, ID: 22003211

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628