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

My question is can I combine 2 BitBlt into 1 time?

I use BitBlt one time to show Base Bitmap on screen. Then, I use BitBlt one time again to show Pixels on Base Bitmap. In current code, the Base Bitmap is shown and after that Pixels are shown on Base Bitmap.

My desire is to show the user Base Bitmap and Pixels in one time.
I think I need to combine it before I show on the screen. But I don't know the way to do it. Could anyone help me for my problem?

The current code is as the following.
C++
void TestDlg::ShowData() {
    CBitmap bmp;
    bmp.LoadBitmap(IDB_BITMAP_TESTBITMAP);
    BITMAP bmpInfo;
    bmp.GetBitmap(&bmpInfo);
    CDC* pDC = this->GetDC();
    CDC dc;
    dc.CreateCompatibleDC(pDC);
    pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dc, 0, 0, SRCCOPY);//show Base Bitmap

    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)); //just example, actually pixel colors are reading from file
	}
    }
    pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dc, 0, 0, SRCAND); //show pixel colors data on Base Bitmap
    dc.SelectObject(pOldBmp);
    bmp.DeleteObject();
    dc.DeleteDC();
    ReleaseDC(pDC);
}


Is there the way to combine Base Bitmap and Pixel colors before showing to user? Thank you very much for your great help.

[EDIT] Added pre-tags for better readability - Code-o-mat [/EDIT]
Posted
Updated 14-May-12 5:16am
v2
Comments
enhzflep 14-May-12 11:19am    
Yeah, of course there is. It's in your code already, in fact.
a) Comment-out the first BitBlt
b) Make the 2nd BitBlt SRCCOPY instead of SRCAND

There's no need to show a bitmap if you're immediately going to modify it and reshow it once more. Just load it, modify it and display it.
April2004 14-May-12 12:11pm    
Thank you very much for your advice.
May be my misunderstanding. I tested as you said. If I comment out the first BitBlt, the Base Bitmap can be shown on screen. Any idea on it ?
Thanks again.

I personally don't think there's an easy way of doing that (if i understand your "desire" correctly), it would probably be possible with going DirectX/Direct3D and doing somekind of multisampling or with some shader code.
What you could try to do is creating an intermediate buffer, do those blits and pixel modifications on this buffer, and then blit this buffer onto the screen.

Something similar to this should do it (writing this by hearth so it might need some fixups here or there, so don't just blindly copy-paste it):
C++
void TestDlg::ShowData() {
    CBitmap bmp;
    bmp.LoadBitmap(IDB_BITMAP_TESTBITMAP);
    BITMAP bmpInfo;
    bmp.GetBitmap(&bmpInfo);
    CDC* pDC = this->GetDC();
    CDC dc;
    dc.CreateCompatibleDC(pDC);
    CBitmap *pOldBml = dc.SelectObject(bmp);

    //Create intermediate buffer and DC
    CBitmap interBitmap;
    CDC     interDC;

    interBitmap.CreateCompatibleBitmap(pDC, bmpInfo.bmWidth, bmpInfo.bmHeight);
    interDC.CreatecompartibleDC(pDC);
    Cbitmap *oldInterBitmap = interDC.SelectObject(&interBitmap);

    interDC.BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dc, 0, 0, SRCCOPY);//move Base Bitmap to intermediate buffer

    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)); //just example, actually pixel colors are reading from file
	}
    }
    interDC.BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dc, 0, 0, SRCAND); //show pixel colors data on intermediate buffer
    pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &interDC, 0, 0, SRCCOPY); //finally, show result on screen

    //Free up resources
    interDC.SelectObject(oldInterBitmap);
    interDC.DeleteDC();
    interBitmap.DeleteObject();

    dc.SelectObject(pOldBmp);
    bmp.DeleteObject();
    dc.DeleteDC();
    ReleaseDC(pDC);
}
 
Share this answer
 
v2
Comments
April2004 14-May-12 12:09pm    
"What you could try to do is creating an intermediate buffer, do those blits and pixel modifications on this buffer, and then blit this buffer onto the screen"
Thank you for your advice. Could you please give me an example of intermediate buffer or the link to refer it ? I am trying a lot of ways. But, cant solve until now. Again, Thank you very much.
Code-o-mat 14-May-12 12:23pm    
I modified the solution and added some code, please see above.
April2004 14-May-12 12:35pm    
I copied your code and run it. I can see the output is what I want. Thank you very much. Now, I am trying to understand your code.
Code-o-mat 14-May-12 12:40pm    
Yourwelcome and good luck.
April2004 19-May-12 14:11pm    
Hello,

Thank you very much for helping me several times. I found that above code is very effective for me. Now, I have additional desires on it.

In above code - Within one function, load the bitmap and set pixel on it.
My additional desires - In one function (for example: OnInitDialog), I load the bitmap. Set the pixel in another function (for example : ShowPixel). When the timer arrive specified time (for example : 1 minute), the based bitmap is stand still as it is loaded. And I just want to redraw ShowPixel with another different pixel color. Based on your code, I tried several times to get my requirements. But until now, I can't get solution yet. If possible, could you please guide me on it? Thanks again.
Code-o-mat might have the ultimate answer...

But AlphaBlend() may serve your purposes.

To do pixel blending, you'll need an alpha channel. This is just another channel, to add to RGB, for RGBA. It will *often* (there are exceptions), the same type as your red, blue and green colors.

This channel determines how much weight that pixel has. 0 is considered completely transparent and 0xff or 1.0 is completely opaque.
 
Share this answer
 
v2
Comments
April2004 14-May-12 12:21pm    
AlphaBlend() is totally new for me. Thank you very much for your advice. I will study about it. If you have an example or reference links, could you please point it out for me? Thank you again JackDingler.
JackDingler 14-May-12 12:23pm    
I don't have a current example. MSDN may have one.
April2004 14-May-12 12:35pm    
Ok. I will study it. Thank you for your guideline.

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