public static BitmapSource CreateImage(Controls.DesignerCanvas canvas, double dpiX, double dpiY) { var image = new RenderTargetBitmap((int)(canvas.ExtentWidth * dpiX / WpfDpi), (int)(canvas.ExtentHeight * dpiX / WpfDpi), dpiX, dpiY, PixelFormats.Pbgra32); canvas.RenderImage(image); return(image); }
public static void ExportImage(Controls.DesignerCanvas canvas, Stream s, BitmapEncoder encoder, double dpiX, double dpiY) { if (canvas == null) { throw new ArgumentNullException(nameof(canvas)); } if (s == null) { throw new ArgumentNullException(nameof(s)); } if (encoder == null) { throw new ArgumentNullException(nameof(encoder)); } var image = CreateImage(canvas, dpiX, dpiY); encoder.Frames.Add(BitmapFrame.Create(image)); encoder.Save(s); }
public static void ExportImage(Controls.DesignerCanvas canvas, string fileName, double dpiX, double dpiY) { if (canvas == null) { throw new ArgumentNullException(nameof(canvas)); } if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("Argument is null or empty", nameof(fileName)); } var encoder = EncoderFromFileName(fileName); if (encoder == null) { throw new NotSupportedException("Extension of specified fileName is not supported."); } using (var fs = File.OpenWrite(fileName)) { ExportImage(canvas, fs, encoder, dpiX, dpiY); } }