Created
January 9, 2017 15:22
-
-
Save mrousavy/f4d2f24a3f95993dfc4f94afadff8466 to your computer and use it in GitHub Desktop.
Get Image from whole screen (BitmapSource)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetScreenshot{ | |
public static BitmapSource GetScreen(Rectangle coordinates) { | |
int left = coordinates.Left; | |
int top = coordinates.Top; | |
int width = coordinates.Width; | |
int height = coordinates.Height; | |
using(var screenBmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { | |
using(var bmpGraphics = Graphics.FromImage(screenBmp)) { | |
bmpGraphics.CopyFromScreen(left, top, 0, 0, new System.Drawing.Size(width, height)); | |
IntPtr hBitmap = screenBmp.GetHbitmap(); | |
BitmapSource ret = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( | |
hBitmap, | |
IntPtr.Zero, | |
Int32Rect.Empty, | |
BitmapSizeOptions.FromEmptyOptions()); | |
DeleteObject(hBitmap); | |
return ret; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment