By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

Q: BitBlt destination is always "blank" (VC++, .NET)

P: 1
Hi Folks,

My setup: Visual Studio Express 2008, VC++, .NET, BitBlt

Here is a summary of the problem:
- I am trying to copy a bitmap of my main form into a picture box.
- To do this, I bitblt using the device contexts of the form and a bitmap object.
- After blitting, the bitmap image is always blank. I don't understand what I'm doing wrong here.

Here is my relevant code:
Expand|Select|Wrap|Line Numbers
  1. MainMenuForm_Shown(...)
  2. {
  3. Graphics ^formGraphics = this->CreateGraphics();
  4. Bitmap ^bitmap = gcnew Bitmap(this->Width, this->Height, formGraphics);
  5. Graphics ^bitmapGraphics = Graphics::FromImage(bitmap);
  6. IntPtr formDC = formGraphics->GetHdc();
  7. IntPtr bitmapDC = bitmapGraphics->GetHdc();
  9. BitBlt(bitmapDC, 0, 0, this->Width, this->Height, formDC, 0, 0, 0xCC0020);
  11. formGraphics->ReleaseHdc(formDC);
  12. bitmapGraphics->ReleaseHdc(bitmapDC);
  14. bitmap->Save(CCarthageDefs::DIR_IMAGES + "test.jpg");
  15. }
After running the above code, the file test.jpg is tiny and basically blank
(maybe all solid grayish white). I expected it to be a copy of the form.
Any comments on why this is happening would be most appreciated.

I thought maybe the problem was with the form, so I tried an even simpler
test: copying one image to another. Here is the code:
Expand|Select|Wrap|Line Numbers
  1. Bitmap ^srcImage = gcnew Bitmap("input.jpg");
  2. Graphics ^srcG = Graphics::FromImage(srcImage);
  3. Bitmap ^toImage = gcnew Bitmap(srcImage->Width, srcImage->Height,
  4. srcG);
  5. Graphics ^toG = Graphics::FromImage(toImage);
  7. IntPtr srcDc = srcG->GetHdc();
  8. IntPtr toDc = toG->GetHdc();
  10. BitBlt(toDc, 0, 0, srcImage->Width, srcImage->Height, srcDc, 0, 0,
  11. 0xCC0020);
  13. srcG->ReleaseHdc(srcDc);
  14. toG->ReleaseHdc(toDc);
  16. srcImage->Save("test1.jpg");
  17. toImage->Save("test2.jpg");
After running the above code, test1.jpg (an output of the source image) is a
correct copy of the source image. test2.jpg is *blank*?? Again, something
with the BitBlt seems to have failed ...

Also, to make sure I was actually hitting the genuine BitBlt code, I tried
using the options BLACKNESS and WHITENESS. Both worked ... the destination
turned the appropriate color.

Finally, in case anyone is interested in why I'm doing this ... I am having
some ugly flicker at the load-up of my form. I'm trying to take a copy of
the form's look, put it in a screen-sized picture box, and display that to
the user before they see the real form.

Mar 27 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.