The bitblt method is used to quickly capture a part of a device chip in the memory and display it.
Bitblt is an API method, which is referenced first.
- [Dllimport ("gdi32.dll")]
- 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 VB, this method is interpreted:
Bitblt target HDC, target X, target y, Image Height, image width, source HDC, source X, source y, grating operation constant
[Function]
Bitblt
[Operating system]
Win9x: Yes
Winnt: Yes
[Statement]
Bitblt lib "GDI32" alias "bitblt" (byval hdestdc as long, byval X as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long
[Description]
Copy a bitmap from one device scenario to another. The source and Target DC must be compatible with each other.
[Return value]
Long, non-zero indicates success, and zero indicates failure. Getlasterror is set.
[Others]
In the NT environment, if a world transmission request is to cut or rotate the data in the source device scenario, the execution of this function will fail.
If the ing between the target and the source DC requires that the pixel size in the rectangle must be changed during transmission, this function will automatically scale, rotate, collapse, or cut as needed, to complete the final transmission process
[Parameter table]
Hdestdc -------- long, target device scenario
X, Y ------------ long: Specifies the point in the upper-left corner of the target rectangle in the Target DC. Expressed by the logical coordinates of the Target DC
Nwidth, nheight-long, the width and height of the image to be transmitted
Hsrcdc --------- long, Source Device scenario. If the source is not specified for the grating operation, the value is set to 0.
Xsrc, ysrc ------ long, the point that describes the upper left corner of the source rectangle in the source DC. Expressed by the logical coordinates of the source DC
Dwrop ---------- long: grating operation to be performed during transmission
Then use the following method to intercept:
- Private bitmap copyrect (picturebox pic, rectangle rect)
- {
- Const int srccopy = 0x00cc0020;
- // Get a graphics object from the form
- Graphics G1 = pic. creategraphics ();
- // Create a empty bitmap from that graphics
- Bitmap bit2 = new Bitmap (100,100, G1 );
- // Create a graphics object in memory from that bitmap
- Graphics g2 = graphics. fromimage (bit2 );
- // Get the intptr's of the graphics
- Intptr hdc1 = g1.gethdc ();
- // Get the intptr's of the graphics
- Intptr hdc2 = g2.gethdc ();
- // Get the picture
- Bitblt (hdc2, p1.location. X, p1.location. Y, rect. Width, rect. Height, hdc1, rect. X, rect. Y, srccopy );
- Bitmap B = (Bitmap) bit2.clone ();
- // Clear all
- G1.releasehdc (hdc1 );
- G2.releasehdc (hdc2 );
- G1.dispose ();
- G2.dispose ();
- Return B;
- }
However, as you can see, the copyrect method has a parameter of the picturebox type. The picturebox control must be displayed on a device (such as a form. If this is not the case, we will not be able to take the screenshot. That is to say, it can only take pictures of controls that have been displayed on the device.
If we change the above Code:
- Private bitmap copyrect (Bitmap bit1, rectangle rect)
- {
- Const int srccopy = 0x00cc0020;
- Graphics G1 = graphics. fromimage (bit1 );
- Bitmap bit2 = new Bitmap (100,100, G1 );
- Graphics g2 = graphics. fromimage (bit2 );
- Intptr hdc1 = g1.gethdc ();
- Intptr hdc2 = g2.gethdc ();
- Bitblt (hdc2, p1.location. X, p1.location. Y, rect. Width, rect. Height, hdc1, rect. X, rect. Y, 13369376 );
- Bitmap B = (Bitmap) bit2.clone ();
- G1.releasehdc (hdc1 );
- G2.releasehdc (hdc2 );
- G1.dispose ();
- G2.dispose ();
- Return B;
- }
Then we will not get any content, just a transparent image.
Alternatively, we can consider declaring a picturebox and assigning the source image to it, and then using this picturebox as the device for image capturing.
The following code:
- Private bitmap copyrect (Bitmap bit1, rectangle rect)
- {
- Const int srccopy = 0x00cc0020;
- Picturebox p1 = new picturebox ();
- P1.image = bit1;
- Graphics G1 = p1.creategraphics ();
- Bitmap bit2 = new Bitmap (100,100, G1 );
- Graphics g2 = graphics. fromimage (bit2 );
- Intptr hdc1 = g1.gethdc ();
- Intptr hdc2 = g2.gethdc ();
- Bitblt (hdc2, p1.location. X, p1.location. Y, rect. Width, rect. Height, hdc1, rect. X, rect. Y, 13369376 );
- Bitmap B = (Bitmap) bit2.clone ();
- G1.releasehdc (hdc1 );
- G2.releasehdc (hdc2 );
- G1.dispose ();
- G2.dispose ();
- Return B;
- }
But we are still wrong, but this time we get not a transparent image, but a screenshot of the rect size starting from the top left of your screenshot.
We can see from the above that the bitblt method can only intercept images already displayed on the control. But cannot intercept images in memory.
We can extract part of the image from one picturebox box and fill it with another picturebox.