Figure 1-form captured into a jpeg file
In most cases, GDI + speeds up your programming of graphics because (1) It is not a thin veneer over the windows SDK (2) it makes sense. however, whenever you lose granularity to create a simpler to use architecture, you tend to lose some functionality. form capture is one of these cases. in the case where you say to yourself: "Hey! I cocould do that in GDI, why can't I do that in GDI + ?? ". The answer is you can.
C # has an attribute that allows you to bring in exactly any pre-Millenium windows sdk dll. this, of course, includes des the famed GDI. DLL. the way to do this is shown below using the dllimportattribute:
[System. runtime. interopservices. dllimportattribute ("gdi32.dll")]
The way to do form capture in GDI is to get the device context to the screen and bit blast it to a bitmap in memory. the GDI external Declaration for bit blsting is shown below (note that this call has been converted to the equivalent call in C # so it can be used in our program ):
Private Static extern bool bitblt (
Intptr hdcdest, // handle to destination DC
Int nxdest, // X-coord of destination upper-left corner
Int nydest, // y-coord of destination upper-left corner
Int nwidth, // width of destination rectangle
Int nheight, // height of destination rectangle
Intptr hdcsrc, // handle to source DC
Int nxsrc, // X-coordinate of source upper-left corner
Int nysrc, // y-coordinate of source upper-left corner
System. int32 dwdrop // raster operation code
);
In order to use this GDI call in GDI + you need to have hooks into the device contexts. Luckily GDI + provides a method of getting the GDI device context through the GDI + graphics object:
Intptr DC1 = agraphicsobject. gethdc ();
Now we'll put the whole thing together in our form. below is the series of callneeded to get the image from the form and stick it in a bitmap using the following steps:
- Get a graphics object to the form on the screen
- Create an empty bitmap the size of the form's client area
- Get the device context for the form
- Get the device context for the bitmap
- Bit blast the form on the screen into the bitmap
- Release the device context for the form
- Release the device context for the bitmap
- Save the image into a jpeg file
Remember, its important to release the GDI device context after your done using it to make the GDI call (S), otherwise GDI + will have problems. below is the full code for capturing the forms client area into a jpeg file:
Private void capture_click (Object sender, system. eventargs E)
{
Graphics G1 = This. creategraphics ();
Image myimage = new Bitmap (this. clientrectangle. Width, this. clientrectangle. Height, G1 );
Graphics g2 = graphics. fromimage (myimage );
Intptr DC1 = g1.gethdc ();
Intptr DC2 = g2.gethdc ();
Bitblt (DC2, 0, 0, this. clientrectangle. Width, this. clientrectangle. Height, DC1, 0, 0, 13369376 );
G1.releasehdc (DC1 );
G2.releasehdc (DC2 );
Myimage. Save (@ "C:/captured.jpg", imageformat. JPEG );
MessageBox. Show ("finished saving image ");
}
Happy Bit Blasting!
Http://www.c-sharpcorner.com/UploadFile/mgold/ScreenCapturingaForm09162005063420AM/ScreenCapturingaForm.aspx