public static BitmapSource GetBitmapSource(int x, int y, int width, int height) { var bounds = ScreenHelper.GetPhysicalDisplaySize(); var screenWidth = bounds.Width; var screenHeight = bounds.Height; var scaleWidth = (screenWidth * 1.0) / SystemParameters.PrimaryScreenWidth; var scaleHeight = (screenHeight * 1.0) / SystemParameters.PrimaryScreenHeight; var w = (int)(width * scaleWidth); var h = (int)(height * scaleHeight); var l = (int)(x * scaleWidth); var t = (int)(y * scaleHeight); using (var bm = new Bitmap(w, h, PixelFormat.Format32bppArgb)) { using (var g = Graphics.FromImage(bm)) { g.CopyFromScreen(l, t, 0, 0, bm.Size); return(Imaging.CreateBitmapSourceFromHBitmap( bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } } }
public static BitmapSource GetFullBitmapSource() { var bounds = ScreenHelper.GetPhysicalDisplaySize(); _Bitmap = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var bmpGraphics = Graphics.FromImage(_Bitmap); bmpGraphics.CopyFromScreen(0, 0, 0, 0, _Bitmap.Size); return(Imaging.CreateBitmapSourceFromHBitmap( _Bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); }