Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

My purpose is to load Image one time at OnInitDialog and set several transparent pixel color on that Image. In my current code, pixel color are filled over Image and I totally can't see the based Image. Here is my code.

BOOL CTestColorDlg::OnInitDialog()
{
C#
CString szFileName(_T("C:\\Users\\User\\Desktop\\ImageData.bmp"));
    hBmp = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0,  LR_LOADFROMFILE);
    m_bitmap.SetBitmap(hBmp); //hBmp is HBITMAP type member variable
    return TRUE;

}

void CTestColorDlg::OnPaint()
{
C#
CDialogEx::OnPaint();
ShowPixelData();

}

void CTestColorDlg::ShowPixelData()
{
C#
BITMAP bmpInfo;
   GetObject(hBmp, sizeof(BITMAP), &bmpInfo);

   CDC* pdc = GetDC();
   CDC memDC;
   if (memDC.CreateCompatibleDC(pdc))
   {
		HBITMAP old_hBitmap = (HBITMAP)memDC.SelectObject(hBmp);
		memDC.SetBkColor(TRANSPARENT);

		for (int i = 0; i < bmpInfo.bmHeight; i++) {
			for(int j = 0; j < bmpInfo.bmWidth; j++) {
				memDC.SetPixel(j, i, RGB(200, 100, 0)); 
			}
    }

	//pdc->BitBlt(100, 100, bmpInfo.bmWidth+100, bmpInfo.bmHeight+100, &memDC, 0, 0,  SRCAND);
	memDC.SelectObject((HBITMAP)old_hBitmap);
	memDC.DeleteDC();
   }

   ReleaseDC(pdc);

}

I want to see the Image and fill transparent color on it. With the upper code, I just can see opaque color fill on the Image. Could anyone give me advice for showing transparent color based on upper code? (I don't want to use alphablend.)

And what is the best way to clear setpixel color, when I would like to set completely different setpixel color ? Should I redraw Image and set different color again ??

Thanks in advanced.
Posted
Comments
lewax00 16-May-12 12:34pm    
What's wrong with alpha? I've got bad news for you...alpha is the channel that sets transparency, you can't have transparency without it.

1 solution

Use GDI+[^], something like this should work:
C++
#include <gdiplus.h>
...
  Gdiplus::Graphics g(memDC);
  g.SetPageUnit(Gdiplus::UnitPixel);

  Gdiplus::SolidBrush brush(Gdiplus::Color(128, 200, 100, 0));
  for (int i = 0; i < bmpInfo.bmHeight; i++) {
    for(int j = 0; j < bmpInfo.bmWidth; j++) {
      g.FillRectangle(&brush, j, i, 1, 1);
    }
  }
...


You also need to initialize and shut down GDI+ somewhere in your code using GdiplusStartup[^] and GdiplusShutDown[^].

See MSDN about GDI+ for details...
 
Share this answer
 
v2
Comments
Sandeep Mewara 17-May-12 1:16am    
5!
Code-o-mat 17-May-12 3:43am    
Thanks
April2004 17-May-12 10:27am    
Thank you very much for your advice.
Actually, I would like to fill various color in Rectangle. I need to set color, brush in for loop. My desire based on your concept is such as:

for (int i = 0; i < bmpInfo.bmHeight; i++) {
for(int j = 0; j < bmpInfo.bmWidth; j++) {
Gdiplus::SolidBrush brush(Gdiplus::Color(128, rand()%100, rand()%20, rand()%100));
g.FillRectangle(&brush, j, i, 1, 1);
}
}
With above code, I found that it is very slow to fill rectangle.
Could you please guide me any solution for speedy ?
And when the timer is arrive to target time(eg. 10 minutes), I would like to refill the rectangle with another various color. If I didn't clear the previous color, the correct color can't be displayed in next time. So, could you please guide me for the fastest way to clear Rectangle ?
Code-o-mat 17-May-12 12:42pm    
Well, GDI+ wasn't built for speed, as far as i know, so i am not surprised it isn't "speedy" as you said. I think in your situation the best bet would be to access the bitmap's pixels directly. You could do this e.g. by adding LR_CREATEDIBSECTION to your LoadImage, after this your BITMAP structure's bmBits member -after you use GetObject- should point at the pixel data. Doing it this way is a bit complicated, try searching in Google or Code Project for related topics.

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