Capture screen with C #

Source: Internet
Author: User
Using system;
Using system. Windows. forms;
Using system. drawing;
Using system. Drawing. imaging;
Using system. runtime. interopservices;

Namespace printscreen
{
// Some code comes from the network
[Structlayout (layoutkind. Sequential)]
Public struct rect
{
Public int left;
Public int top;
Public int right;
Public int bottom;
}
// Encapsulate Some APIs
Public class apitmp
{
[Dllimport ("user32.dll")]
Public static extern bool getcursorpos (ref point lppoint );
[Dllimport ("user32.dll")]
Public static extern int windowfrompoint (point lppoint );
[Dllimport ("user32.dll")]
Public static extern intptr getasktopwindow ();
[Dllimport ("user32.dll")]
Public static extern int getforegroundwindow ();
[Dllimport ("user32.dll")]
Public static extern int getwindowrect (INT hwnd, ref rect RC );
[Dllimport ("user32.dll")]
Public static extern int getwindowdc (INT hwnd );
[Dllimport ("gdi32.dll")]
Public static extern bool bitblt (
Intptr hdcdest, // handle of the target device
Int nxdest, // X coordinate in the upper left corner of the target object
Int nydest, // X coordinate in the upper left corner of the target object
Int nwidth, // The width of the rectangle of the target object
Int nheight, // The length of the rectangle of the target object
Intptr hdcsrc, // Source Device handle
Int nxsrc, // X coordinate in the upper left corner of the source object
Int nysrc, // X coordinate in the upper left corner of the source object
System. int32 dwdrop // The operation value of the grating
);
[Dllimportattribute ("gdi32.dll")]
Public static extern intptr createdc (
String lpszdriver, // driver name
String lpszdevice, // device name
String lpszoutput, // useless, can be set to "null"
Intptr lpinitdata // any printer data
);

}

// Encapsulate various screen capture operations
Public class ucapture
{
// Obtain the specified area of the screen
Public bitmap getscreen (INT left, int top, int width, int height)
{
Intptr DC1 = apitmp. createdc ("display", null, null, (intptr) null );
Graphics newgraphics = graphics. fromhdc (DC1 );
Bitmap IMG = new Bitmap (width, height, newgraphics );
Graphics thisgraphics = graphics. fromimage (IMG );
Intptr DC2 = thisgraphics. gethdc ();
Intptr DC3 = newgraphics. gethdc ();
Apitmp. bitblt (DC2, 13369376, width, height, DC3, left, top );
Thisgraphics. releasehdc (DC2 );
Newgraphics. releasehdc (DC3 );
Return IMG;
}
// Obtain the entire Screen
Public bitmap getfullscreen ()
{
Return getscreen (0, 0, screen. primaryscreen. bounds. Width, screen. primaryscreen. bounds. Height );
}
// Capture the window indicated by the handle
Public bitmap getscreenfromhandle (INT hwnd)
{
Rect rc = new rect ();
Apitmp. getwindowrect (hwnd, ref RC );
Return getscreen (RC. Left, RC. Top, RC. right-rc.left, RC. bottom-rc.top );
}
// Capture the activity window
Public bitmap getscreenfromactivewindow ()
{
Int handle = apitmp. getforegroundwindow ();
Return getscreenfromhandle (handle );
}
// Save to various formats
Public String savepic (bitmap BMP)
{
Savefiledialog savedialog = new savefiledialog ();
Savedialog. filter = "bitmap file (*. BMP) | *. BMP | JPG file (*. JPG) | *. JPG | GIF file (*. GIF) | *. GIF | TIFF File (*. tiff) | *. tiff | "+
"EMF file (*. EMF) | *. EMF | icon file (*. ICO) | *. ICO | WMF file (*. WMF) | *. WMF | PNG file (*. PNG) | *. PNG ";
Savedialog. defaultext = "*. BMP ";
If (savedialog. showdialog () = dialogresult. OK)
{
String ext = savedialog. filename. substring (savedialog. filename. Length-4, 4 );
Switch (EXT)
{
Case ". BMP ":
BMP. Save (savedialog. filename, imageformat. BMP );
Break;
Case ". GIF ":
BMP. Save (savedialog. filename, imageformat. GIF );
Break;
Case ". jpg ":
BMP. Save (savedialog. filename, imageformat. JPEG );
Break;
Case ". EMF ":
BMP. Save (savedialog. filename, imageformat. EMF );
Break;
Case ". ICO ":
BMP. Save (savedialog. filename, imageformat. Icon );
Break;
Case ". WMF ":
BMP. Save (savedialog. filename, imageformat. WMF );
Break;
Case ". PNG ":
BMP. Save (savedialog. filename, imageformat. PNG );
Break;
Case ". Tiff ":
BMP. Save (savedialog. filename, imageformat. Tiff );
Break;
Default:
Return "";
}
Return savedialog. filename;
}
Return "";
}
}
}
 

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.