Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I would like to draw transparent color on bitmap. But, when I write the code as follow and run it, the bitmap is covered on color. What is wrong on my code? Also, is there any unnecessary code in my program ?
The process is as follow:
I import bitmap resource to MFC CDialog program. Add picture control to CDialog and add the previous imported bitmap to picture control. Set picture control property as transparent.
Then, I add the follow code to onpaint.
C++
CDC dc;
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP_TESTBITMAP);
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
CDC* pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
CBitmap* pOldBmp = dc.SelectObject(&bmp);
for (int i = 0; i < bmpInfo.bmHeight; i++) {
    for(int j = 0; j < bmpInfo.bmWidth; j++) {
        dc.SetPixel(j, i, RGB(rand()%100, rand()%50, rand()%100));
    }
}
pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dc, 0, 0, SRCAND);
dc.SelectObject(pOldBmp);
bmp.DeleteObject();
dc.DeleteDC();
this->ReleaseDC(pDC);
CDialogEx::OnPaint();

Thanks for your help and time.
Posted
Updated 22-Apr-12 1:28am
v2

1 solution

 
Share this answer
 
Comments
April2004 22-Apr-12 9:05am    
Espen Harlinn, thanks for your advice.
I think I just need to modify or add one or two things on my code.
But I dont know yet what are the problems.
Any idea on my code ?

With Regards,
April2004
Espen Harlinn 22-Apr-12 11:27am    
I think all you need is the code in the first block of the first article I referenced.
Personally I would use GDI+ and not GDI
http://msdn.microsoft.com/en-us/library/ms533798(VS.85).aspx
GDI+ supports the alpha channel too, making this a whole lot easier.

You can construct a GDI+ Graphics object from a HDC using the constructor documented here: http://msdn.microsoft.com/en-us/library/ms536160(VS.85).aspx
April2004 22-Apr-12 11:49am    
Thank you very much for your help. I tried with the first block of the first article that you said. There are some errors of CMemDC at DrawTransparentBitmap function. Actually, I don't know how to solve it. Until now, I can't get the solution yet.
VJ Reddy 23-Apr-12 19:40pm    
Good references. 5!
Espen Harlinn 24-Apr-12 6:25am    
Thank you VJ :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900