Slice the image and study the bitblt () method-C #

Source: Internet
Author: User
Tags transparent image

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.

  1. [Dllimport ("gdi32.dll")]
  2. Private Static extern bool bitblt (
  3. Intptr hdcdest, // handle to destination DC
  4. Int nxdest, // X-coord of destination upper-left corner
  5. Int nydest, // y-coord of destination upper-left corner
  6. Int nwidth, // width of destination rectangle
  7. Int nheight, // height of destination rectangle
  8. Intptr hdcsrc, // handle to source DC
  9. Int nxsrc, // X-coordinate of source upper-left corner
  10. Int nysrc, // y-coordinate of source upper-left corner
  11. System. int32 dwdrop // raster operation code
  12. );

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:

  1. Private bitmap copyrect (picturebox pic, rectangle rect)
  2. {
  3. Const int srccopy = 0x00cc0020;
  4. // Get a graphics object from the form
  5. Graphics G1 = pic. creategraphics ();
  6. // Create a empty bitmap from that graphics
  7. Bitmap bit2 = new Bitmap (100,100, G1 );
  8. // Create a graphics object in memory from that bitmap
  9. Graphics g2 = graphics. fromimage (bit2 );
  10. // Get the intptr's of the graphics
  11. Intptr hdc1 = g1.gethdc ();
  12. // Get the intptr's of the graphics
  13. Intptr hdc2 = g2.gethdc ();
  14. // Get the picture
  15. Bitblt (hdc2, p1.location. X, p1.location. Y, rect. Width, rect. Height, hdc1, rect. X, rect. Y, srccopy );
  16. Bitmap B = (Bitmap) bit2.clone ();
  17. // Clear all
  18. G1.releasehdc (hdc1 );
  19. G2.releasehdc (hdc2 );
  20. G1.dispose ();
  21. G2.dispose ();
  22. Return B;
  23. }

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:

  1. Private bitmap copyrect (Bitmap bit1, rectangle rect)
  2. {
  3. Const int srccopy = 0x00cc0020;
  4. Graphics G1 = graphics. fromimage (bit1 );
  5. Bitmap bit2 = new Bitmap (100,100, G1 );
  6. Graphics g2 = graphics. fromimage (bit2 );
  7. Intptr hdc1 = g1.gethdc ();
  8. Intptr hdc2 = g2.gethdc ();
  9. Bitblt (hdc2, p1.location. X, p1.location. Y, rect. Width, rect. Height, hdc1, rect. X, rect. Y, 13369376 );
  10. Bitmap B = (Bitmap) bit2.clone ();
  11. G1.releasehdc (hdc1 );
  12. G2.releasehdc (hdc2 );
  13. G1.dispose ();
  14. G2.dispose ();
  15. Return B;
  16. }

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:

  1. Private bitmap copyrect (Bitmap bit1, rectangle rect)
  2. {
  3. Const int srccopy = 0x00cc0020;
  4. Picturebox p1 = new picturebox ();
  5. P1.image = bit1;
  6. Graphics G1 = p1.creategraphics ();
  7. Bitmap bit2 = new Bitmap (100,100, G1 );
  8. Graphics g2 = graphics. fromimage (bit2 );
  9. Intptr hdc1 = g1.gethdc ();
  10. Intptr hdc2 = g2.gethdc ();
  11. Bitblt (hdc2, p1.location. X, p1.location. Y, rect. Width, rect. Height, hdc1, rect. X, rect. Y, 13369376 );
  12. Bitmap B = (Bitmap) bit2.clone ();
  13. G1.releasehdc (hdc1 );
  14. G2.releasehdc (hdc2 );
  15. G1.dispose ();
  16. G2.dispose ();
  17. Return B;
  18. }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.