public static BitmapSource CropScreen(BitmapSource fullScreenBtimap, CropViewBox viewBox) { try { var cropScreenbox = new Int32Rect( (int)Math.Round(viewBox.CropScreenbox.Left, 0), (int)Math.Round(viewBox.CropScreenbox.Top, 0), (int)Math.Round(viewBox.CropScreenbox.Width, 0), (int)Math.Round(viewBox.CropScreenbox.Height, 0)); var extendBitmap = ExtendFullScreenBitmap(fullScreenBtimap, ref cropScreenbox); var croppedBitmap = new CroppedBitmap(extendBitmap, cropScreenbox); if (viewBox.IsTransform) { //旋转、翻转变换 var drawingVisual = new DrawingVisual(); using (var context = drawingVisual.RenderOpen()) { var brush = new ImageBrush(croppedBitmap) { Stretch = Stretch.None, RelativeTransform = viewBox.RenderTransform, }; context.DrawRectangle(brush, null, new Rect(0, 0, viewBox.TransformViewbox.Width, viewBox.TransformViewbox.Height)); } var renderBitmap = new RenderTargetBitmap((int)viewBox.TransformViewbox.Width, (int)viewBox.TransformViewbox.Height, 96, 96, PixelFormats.Pbgra32); renderBitmap.Render(drawingVisual); croppedBitmap = new CroppedBitmap(renderBitmap, new Int32Rect((int)Math.Round(viewBox.CropViewbox.X, 0), (int)Math.Round(viewBox.CropViewbox.Y, 0), (int)Math.Round(viewBox.CropViewbox.Width, 0), (int)Math.Round(viewBox.CropViewbox.Height, 0))); } return(croppedBitmap); } catch (Exception ex) { Trace.WriteLine(ex.ToString()); return(null); } }
public static BitmapSource CropScreen(CropViewBox viewBox) { return(CropScreen(CopyScreen(), viewBox)); }