Skip to content

Instantly share code, notes, and snippets.

@mrousavy
Created January 9, 2017 15:22
Show Gist options
  • Save mrousavy/f4d2f24a3f95993dfc4f94afadff8466 to your computer and use it in GitHub Desktop.
Save mrousavy/f4d2f24a3f95993dfc4f94afadff8466 to your computer and use it in GitHub Desktop.
Get Image from whole screen (BitmapSource)
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