Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am trying to get transparent color on bitmap. I am trying it for several ways and several hours. (I know the way to load bitmap and load color in the same function. But I would like to get it in separate functions.)
Until now, I can't get simple solution yet. Please help me if possible.
I use MFC CDialog. The coding is as follow:
C++
BOOL CTestingDlg::OnInitDialog(){ //Load base bitmap
//To do
  LoadFileBitmap(&m_bmpPLmain, _T("C:\\Test\\testbitmap.bmp");
}
================
void CTestingDlg::OnPaint(){ //Load color on bitmap, Actually, I want transparent color, but my problem is the following code can't get transparent one.
//To do
		CDC hdcMem;
		BITMAP bmpInfo;
		m_bmpPLmain.GetBitmap(&bmpInfo);
		CDC* pDC = this->GetDC();
		hdcMem.CreateCompatibleDC(pDC);
		CBitmap* pOldBmp = hdcMem.SelectObject(&m_bmpPLmain);
		
		for (int i = 0; i < 150; i++) {
			for(int j = 0; j < 150; j++) {
				hdcMem.SetPixel(j, i, RGB(200,60,20));
			}
		}
		pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &hdcMem, 10, 10, SRCAND);
		CDialogEx::OnPaint();
}
================
BOOL CTestingDlg::LoadFileBitmap(CBitmap* pBmp, LPCTSTR szFilename){
   pBmp->DeleteObject();
   return pBmp->Attach(LoadImage(NULL, szFilename, IMAGE_BITMAP, 0, 0,
                     LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_LOADTRANSPARENT));
}
================

I don't know why I can't get transparent color. Please help me for that problem.
Thank you very much.

Best Regards,
April2004
Posted
Updated 22-Apr-12 5:52am
v2
Comments
Code-o-mat 22-Apr-12 11:59am    
Explain what you mean by "transparent color". Usually transparent color in relation with a bitmap means the color on the bitmap that indicates "holes" in the bitmap, so when you render the bitmap to some surface, the pixels on the "target surface" would stay unchanged if they would be overwritten by the specified transparent color. But i get the feeling you are not talking about this.
Nelek 22-Apr-12 12:27pm    
OP wrote a comment to answer you, but didn't wrote it as "reply" to you. Have a look below
Code-o-mat 22-Apr-12 12:32pm    
I noticed, but thank you very much for telling me.
Nelek 22-Apr-12 17:34pm    
You are welcome :)
April2004 22-Apr-12 12:06pm    
Suppose, I load Circle shape bitmap in OnInitDialog. Then, I paint the red color on it. I would like to see the Circle even after the red color is painted. With the above code, I just can see the red color as the red color is covered on the circle.
That is the meaning of transparent that I want. Thank you very much for care of my problem.

Hello All,
Thank you very much for your helps. I got the output that I want after controlling Invalidate(TRUE) and Invalidate(FALSE).
From all of your answers, I got a lot of knowledge. I will study more about the keywords that you are guiding. Again, thank you very much.
 
Share this answer
 
Still not completely sure what you mean, but try something like this:
C++
int Red, Green, Blue;
COLORREF Color;
for (int i = 0; i < 150; i++) {
    for(int j = 0; j < 150; j++) {
        Color = hdcMem.GetPixel(j, i);
        Red   = GetRValue(Color);
        Green = GetGValue(Color);
        Blue  = GetBValue(Color);
        Red   += (200 - Red  ) / 2;
        Green += ( 60 - Green) / 2;
        Blue  += ( 20 - Blue ) / 2;
        hdcMem.SetPixel(j, i, RGB((UCHAR)Red, (UCHAR)Green, (UCHAR)Blue));
    }
}

But manupulating a picture using GetPixel/SetPixel can be very time consuming, you might consider using GDI+ or maybe directly accessing the pixel-data in the bitmap.

E.g somethig like:
C++
...
Gdiplus::Graphics Gp(hdcMem);
Gp.SetPageUnit(Gdiplus::UnitPixel);
Gdiplus::SolidBrush GBr(Gdiplus::Color(128, 200, 60, 20));
Gp.FillRectangle(&GBr, 0, 0, 150, 150);
...


You might need to change the code here or there, i'm writing this by heart.
 
Share this answer
 
If you override the OnPaint function of a picture control you take full responsibility for the drawing. And your BitBlt does not do transparent drawing.

Look into what TransparentBlt does and AlphaBlend. They probably produce the result that you want. Lookup the reference information on MSDN for these two functions and that will get you on track.
 
Share this answer
 

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