crankeboy
Programmer
- Mar 26, 2008
- 1
Hi Folks,
Regarding: 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:
MainMenuForm_Shown(...)
{
Graphics ^formGraphics = this->CreateGraphics();
Bitmap ^bitmap = gcnew Bitmap(this->Width, this->Height, formGraphics);
Graphics ^bitmapGraphics = Graphics::FromImage(bitmap);
IntPtr formDC = formGraphics->GetHdc();
IntPtr bitmapDC = bitmapGraphics->GetHdc();
BitBlt(bitmapDC, 0, 0, this->Width, this->Height, formDC, 0, 0, 0xCC0020);
formGraphics->ReleaseHdc(formDC);
bitmapGraphics->ReleaseHdc(bitmapDC);
bitmap->Save(CCarthageDefs:IR_IMAGES + "test.jpg");
}
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:
Bitmap ^srcImage = gcnew Bitmap("input.jpg");
Graphics ^srcG = Graphics::FromImage(srcImage);
Bitmap ^toImage = gcnew Bitmap(srcImage->Width, srcImage->Height, srcG);
Graphics ^toG = Graphics::FromImage(toImage);
IntPtr srcDc = srcG->GetHdc();
IntPtr toDc = toG->GetHdc();
BitBlt(toDc, 0, 0, srcImage->Width, srcImage->Height, srcDc, 0, 0, 0xCC0020);
srcG->ReleaseHdc(srcDc);
toG->ReleaseHdc(toDc);
srcImage->Save("test1.jpg");
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.
Thanks
Regarding: 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:
MainMenuForm_Shown(...)
{
Graphics ^formGraphics = this->CreateGraphics();
Bitmap ^bitmap = gcnew Bitmap(this->Width, this->Height, formGraphics);
Graphics ^bitmapGraphics = Graphics::FromImage(bitmap);
IntPtr formDC = formGraphics->GetHdc();
IntPtr bitmapDC = bitmapGraphics->GetHdc();
BitBlt(bitmapDC, 0, 0, this->Width, this->Height, formDC, 0, 0, 0xCC0020);
formGraphics->ReleaseHdc(formDC);
bitmapGraphics->ReleaseHdc(bitmapDC);
bitmap->Save(CCarthageDefs:IR_IMAGES + "test.jpg");
}
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:
Bitmap ^srcImage = gcnew Bitmap("input.jpg");
Graphics ^srcG = Graphics::FromImage(srcImage);
Bitmap ^toImage = gcnew Bitmap(srcImage->Width, srcImage->Height, srcG);
Graphics ^toG = Graphics::FromImage(toImage);
IntPtr srcDc = srcG->GetHdc();
IntPtr toDc = toG->GetHdc();
BitBlt(toDc, 0, 0, srcImage->Width, srcImage->Height, srcDc, 0, 0, 0xCC0020);
srcG->ReleaseHdc(srcDc);
toG->ReleaseHdc(toDc);
srcImage->Save("test1.jpg");
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.
Thanks