473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Q: (.NET/GDI/BitBlt) Problem copying form to bitmap

Hi Folks,

I'm not sure where this post belongs since I'm using managed vc.net, but the
issue is around GDI 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::DIR_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(CCarthageDefs::DIR_IMAGES + "test1.jpg");
toImage->Save(CCarthageDefs::DIR_IMAGES + "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

Mar 26 '08 #1
1 3106
I thought the following might also be helpful:
- I tried to use control->DrawToBitmap(). but it doesn't draw all the
sub-controls that I need it to.
- I am using VC++ under Visual Studio Express 2008.
- Here is my prototype for BitBlt:
[System::Runtime::InteropServices::DllImportAttribu te("gdi32.dll")]
extern bool BitBlt(IntPtr hDestDC,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hSrcDC,
int xSrc,
int ySrc,
int dwRop);

Mar 26 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pavan Arise | last post by:
Hi all.. I have this particular problem. I have a form with some plotting done on that. Now, I was wondering if I could copy the whole drawing/plotting of that form onto a different form & then do...
1
by: Steve Darby | last post by:
Hi there, How can I copy content from one form field into another at the click of a button?
3
by: Jerry Q | last post by:
Hello, Where could aI find a sample code which would show how to use Marshal.Copy to copy a "graphics image" to pixel based image? There is a MS (really) quick supportpage for that...
15
by: dgk | last post by:
I'm trying to save the image of a webbrowser control. Not a particular image being shown on the page, but rather the visible display of the webbrowser. Using VS2005 I figured that maybe...
3
by: Dave | last post by:
Hey all, I have a problem and I can't find anything on the net to really address it. We have a program at work that has lots of numbers on the front of the form because it is a control program...
1
by: MLM450 | last post by:
I am creating a bitmap using a memory stream. Is there any way to load the bitmap data so the memory stream can be disposed? Perhaps copying the bitmap in some simple, quick way? Thanks
15
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
1
by: tbaxter | last post by:
Hello all, I'm seeing something very odd when I create a System.Drawing.Bitmap from an existing Bitmap. If the original Bitmap has a resolution of 72x72, the new resulting Bitmap has a resolution...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.