public static BitmapFrame Resize(this BitmapFrame frame, int width, int height, BitmapScalingMode mode = BitmapScalingMode.NearestNeighbor) { if (frame.IsNullOrEmpty() || width <= 0 || height <= 0) { return(null); } if (frame.Width == width && frame.Height == height) { return((BitmapFrame)frame.Clone()); } DrawingGroup group = new DrawingGroup(); RenderOptions.SetBitmapScalingMode(group, mode); group.Children.Add(new ImageDrawing(frame, new Rect(0, 0, width, height))); DrawingVisual visual = new DrawingVisual(); DrawingContext context = visual.RenderOpen(); context.DrawDrawing(group); RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default); context.Close(); rtb.Render(visual); BitmapFrame result = BitmapFrame.Create(rtb); return(result); }
//not working public static BitmapFrame Copy(this BitmapFrame frame) { if (frame.IsNullOrEmpty()) { return(null); } return(frame.Clone().ToBitmapFrame()); }