public static ImageSource CompressToImageSource(this BitmapFrame bmf, int quality) { if (bmf.IsNullOrEmpty()) { return(null); } if (quality <= 0) { quality = 1; } else if (quality > 100) { quality = 100; } JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = quality; encoder.Frames.Add(bmf); using (MemoryStream stream = new MemoryStream()) { encoder.Save(stream); BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.StreamSource = new MemoryStream(stream.ToArray()); bmi.EndInit(); return(bmi); } }
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); }
public static Bitmap CompressToBitmap(this BitmapFrame bmf, int quality) { if (bmf.IsNullOrEmpty()) { return(null); } if (quality <= 0) { quality = 1; } else if (quality > 100) { quality = 100; } JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = quality; encoder.Frames.Add(bmf); MemoryStream stream = new MemoryStream(); encoder.Save(stream); return(stream.ToBitmap()); //return new Bitmap(stream); }
public static BitmapSource CompressToBitmapSource(this BitmapFrame frame, int quality) { if (frame.IsNullOrEmpty()) { return(null); } return((BitmapSource)frame.CompressToImageSource(quality)); }
//not working public static BitmapFrame Copy(this BitmapFrame frame) { if (frame.IsNullOrEmpty()) { return(null); } return(frame.Clone().ToBitmapFrame()); }
public static BitmapFrame Resize(this BitmapFrame frame, int percentage, BitmapScalingMode mode = BitmapScalingMode.NearestNeighbor) { if (frame.IsNullOrEmpty() || percentage <= 0) { return(null); } int width = (int)((decimal)frame.Width).FindValueByPercentages(100, percentage); int height = (int)((decimal)frame.Height).FindValueByPercentages(100, percentage); return(frame.Resize(width, height, mode)); }
public static Bitmap ToBitmap(this BitmapFrame frame) { if (frame.IsNullOrEmpty()) { return(null); } byte[] data = frame.ToByteArray(BitmapFrameFormatMode.Png); if (data.IsNullOrEmpty()) { return(null); } return(data.ToBitmap()); }
public static byte[] ToByteArray(this BitmapFrame frame, BitmapFrameFormatMode mode = BitmapFrameFormatMode.Png) { if (frame.IsNullOrEmpty()) { return(null); } byte[] result = null; using (MemoryStream stream = new MemoryStream()) { BitmapEncoder encoder = mode.GetBitmapEncoder(); if (encoder != null) { encoder.Frames.Add(frame); encoder.Save(stream); result = stream.ToArray(); } } return(result); }