public static byte[] SaveInkCanvasToJpg(InkCanvas canvas, int quality) { canvas.UpdateLayout(); Size sizet = new Size(); canvas.Arrange(new Rect(sizet)); Size size = new Size(canvas.ActualWidth, canvas.ActualHeight); canvas.Margin = new Thickness(0, 0, 0, 0); canvas.Measure(size); canvas.Arrange(new Rect(size)); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.QualityLevel = quality; RenderTargetBitmap bitmapTarget = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default); bitmapTarget.Render(canvas); encoder.Frames.Add(BitmapFrame.Create(bitmapTarget)); MemoryStream ms = new MemoryStream(); encoder.Save(ms); return(ms.ToArray()); }
private static FormatConvertedBitmap ConvertToGrayScaleBitmap(InkCanvas surface) { var transform = surface.LayoutTransform; surface.LayoutTransform = null; var size = new Size(surface.Width, surface.Height); surface.Measure(size); surface.Arrange(new Rect(size)); var renderBitmap = new RenderTargetBitmap(28, 28, 96d, 96d, PixelFormats.Pbgra32); var visual = new DrawingVisual(); using (var context = visual.RenderOpen()) { var brush = new VisualBrush(surface); context.DrawRectangle(brush, null, new Rect(new Point(), new Size(surface.Width, surface.Height))); } visual.Transform = new ScaleTransform(28 / surface.ActualWidth, 28 / surface.ActualHeight); renderBitmap.Render(visual); var bitmapGreyscale = new FormatConvertedBitmap(renderBitmap, PixelFormats.Gray8, BitmapPalettes.Gray256, 0.0); surface.LayoutTransform = transform; return(bitmapGreyscale); }
public void saveCanvas(object sender, RoutedEventArgs e) { this.save_filename = this.save_filename ?? this.getSaveFilename(); if (save_filename != null) { var cantwo = new InkCanvas(); cantwo.Strokes.Clear(); cantwo.Strokes.Add(this.canvas.Strokes); cantwo.Background = this.canvas.Background; var size = new Size(this.canvas.ActualWidth, this.canvas.ActualHeight); cantwo.Height = size.Height; cantwo.Width = size.Width; cantwo.Measure(size); cantwo.Arrange(new Rect(size)); var transform = this.canvas.LayoutTransform; var rtb = new RenderTargetBitmap((int)this.canvas.ActualWidth, (int)this.canvas.ActualHeight, dpiX, dpiY, PixelFormats.Default); rtb.Render(cantwo); try { using (var fs = File.Open(this.save_filename, FileMode.Create)){ var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); encoder.Save(fs); } } catch (IOException) { MessageBox.Show("Failed to save image", "ERROR: Save Failed"); } // Restore transformation if there was one. this.canvas.LayoutTransform = transform; } }
// Canvas を画像ファイルとして保存する。 public static RenderTargetBitmap toImage(this InkCanvas canvas, string path, BitmapEncoder encoder = null) { // レイアウトを再計算させる var size = new Size(canvas.Width, canvas.Height); canvas.Measure(size); canvas.Arrange(new Rect(size)); // VisualObjectをBitmapに変換する var renderBitmap = new RenderTargetBitmap((int)size.Width, // 画像の幅 (int)size.Height, // 画像の高さ 96.0d, // 横96.0DPI 96.0d, // 縦96.0DPI PixelFormats.Pbgra32); // 32bit(RGBA各8bit) renderBitmap.Render(canvas); // 出力用の FileStream を作成する using (var os = new FileStream(path, FileMode.Create)) { // 変換したBitmapをエンコードしてFileStreamに保存する。 // BitmapEncoder が指定されなかった場合は、PNG形式とする。 encoder = encoder ?? new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); encoder.Save(os); } return(renderBitmap); }
public static void ExportToPng(string path, InkCanvas canvas) { if (path == null) { return; } var size = new Size(canvas.ActualWidth, canvas.ActualWidth); // Measure the canvas canvas.Measure(size); // Create a render bitmap and push the canvas to it var renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32); renderBitmap.Render(canvas); // Create a file stream for saving image using (var outStream = new FileStream(path, FileMode.Create)) { // Use png encoder for our data var encoder = new PngBitmapEncoder(); // push the rendered bitmap to it encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); // save the data to the stream encoder.Save(outStream); } }
public static void SaveCanvas(Window window, InkCanvas canvas, int dpi, string filename) { Size availableSize = new Size(window.Width + 100.0, window.Height); canvas.Measure(availableSize); RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)window.Width, (int)window.Height - 200, dpi, dpi, PixelFormats.Pbgra32); renderTargetBitmap.Render(canvas); SaveRTBAsPNG(renderTargetBitmap, filename); }
RenderTargetBitmap CanvasToBitmap() { Size size = new Size(MyCanvas.ActualWidth, MyCanvas.ActualHeight); MyCanvas.Measure(size); Rect rect = new Rect(size); // Получаем ширину и высоту нашего будущего изображения(квадрата) MyCanvas.Arrange(rect); int dpi = 96; RenderTargetBitmap rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, dpi, dpi, System.Windows.Media.PixelFormats.Default); // через обьект класса RenderTargetBitmap будем преобразовывать canvas в растровое изображение rtb.Render(MyCanvas); return(rtb); }
public static void ExportToPng(Uri path, InkCanvas Surface) { if (path == null) { return; } // Save current canvas transform Transform transform = Surface.LayoutTransform; // reset current transform (in case it is scaled or rotated) Surface.LayoutTransform = null; // Get the size of canvas Size size = new Size(Surface.ActualWidth, Surface.ActualHeight); // Measure and arrange the surface // VERY IMPORTANT Surface.Measure(size); Surface.Arrange(new Rect(size)); // Create a render bitmap and push the surface to it RenderTargetBitmap renderBitmap = new RenderTargetBitmap( (int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32); renderBitmap.Render(Surface); // Create a file stream for saving image using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create)) { // Use png encoder for our data PngBitmapEncoder encoder = new PngBitmapEncoder(); // push the rendered bitmap to it encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); // save the data to the stream encoder.Save(outStream); } // Restore previously saved layout Surface.LayoutTransform = transform; }
public static void SaveCanvas(UserControl window, InkCanvas canvas, int dpi, string filename) { Size size = new Size(window.Width, window.Height); canvas.Measure(size); //canvas.Arrange(new Rect(size)); var rtb = new RenderTargetBitmap( (int)window.Width, //width (int)window.Height, //height dpi, //dpi x dpi, //dpi y PixelFormats.Pbgra32 // pixelformat ); rtb.Render(canvas); SaveRTBAsPNG(rtb, filename); }
public void EskportDoJPEG(InkCanvas obszar) { double x1 = obszar.Margin.Left, x2 = obszar.Margin.Top, x3 = obszar.Margin.Right, x4 = obszar.Margin.Bottom; obszar.Margin = new Thickness(0, 0, 0, 0); Size size = new Size(FormWidth() + 150, FormHeight() + 150); //Size size = new Size(obszar.Width, obszar.Height); obszar.Measure(size); obszar.Arrange(new Rect(size)); RenderTargetBitmap renderBitmap = new RenderTargetBitmap( (int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default); renderBitmap.Render(obszar); otworzOknoDialogoZapisz(); try { using (FileStream fs = File.Open(nazwaPliku, FileMode.Create)) { JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); encoder.Save(fs); } obszar.Margin = new Thickness(x1, x2, x3, x4); } catch (Exception e) { } }
public void ExportToJpeg(String path, InkCanvas surface) { double x1 = surface.Margin.Left, x2 = surface.Margin.Top, x3 = surface.Margin.Right, x4 = surface.Margin.Bottom; if (path == null) { return; } surface.Margin = new Thickness(0, 0, 0, 0); System.Windows.Size size = new System.Windows.Size(surface.Width, surface.Height); surface.Measure(size); surface.Arrange(new Rect(size)); RenderTargetBitmap renderBitmap = new RenderTargetBitmap( (int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default); renderBitmap.Render(surface); using (FileStream fs = File.Open(path, FileMode.Create)) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); encoder.Save(fs); } surface.Margin = new Thickness(x1, x2, x3, x4); }
internal FileStream CreatePNG(InkCanvas canvas, int resolution) { Transform transform = canvas.LayoutTransform; canvas.LayoutTransform = null; Size size = new Size(canvas.ActualWidth, canvas.ActualHeight); canvas.Measure(size); canvas.Arrange(new Rect(size)); int resolutionScaleFactor = resolution / 96; RenderTargetBitmap renderBitmap = new RenderTargetBitmap( resolutionScaleFactor * ((int)size.Width + 1), resolutionScaleFactor * ((int)size.Height + 1), resolutionScaleFactor * 96d, resolutionScaleFactor * 96d, PixelFormats.Default ); renderBitmap.Render(canvas); FileStream outStream = new FileStream(@"tester", FileMode.Create); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); encoder.Save(outStream); outStream.Position = 0; canvas.LayoutTransform = transform; return(outStream); }
internal BitmapSource GetBitmap(Orientation?orientation) { InkCanvas inkCanvas = new InkCanvas { Background = System.Windows.Media.Brushes.White }; if (this.StrokeData != null) { using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(this.StrokeData, 0, this.StrokeData.Length); memoryStream.Seek(0, SeekOrigin.Begin); StrokeCollection collection = new StrokeCollection(memoryStream); inkCanvas.Strokes = collection; } } foreach (Stroke stroke in inkCanvas.Strokes) { stroke.DrawingAttributes.Color = stroke.DrawingAttributes.Color == Colors.White ? Colors.Black : stroke.DrawingAttributes.Color == Colors.LightGray ? Colors.Black : stroke.DrawingAttributes.Color; } System.Windows.Point bottomRightCorner = new System.Windows.Point(1, 1); foreach (Stroke stroke in inkCanvas.Strokes) { Rect bounds = stroke.GetBounds(); if (bounds.Right > bottomRightCorner.X) { bottomRightCorner.X = bounds.Right + 1; } if (bounds.Bottom > bottomRightCorner.Y) { bottomRightCorner.Y = bounds.Bottom + 1; } } double scale = 3; inkCanvas.Width = bottomRightCorner.X * scale; inkCanvas.Height = bottomRightCorner.Y * scale; inkCanvas.Measure(inkCanvas.RenderSize); inkCanvas.Arrange(new Rect(0, 0, bottomRightCorner.X, bottomRightCorner.Y)); inkCanvas.UpdateLayout(); RenderTargetBitmap bitmap = new RenderTargetBitmap( (int)inkCanvas.RenderSize.Width, (int)inkCanvas.RenderSize.Height, 96 * scale, 96 * scale, PixelFormats.Pbgra32); bitmap.Render(inkCanvas); bitmap.Freeze(); if (bottomRightCorner.X > bottomRightCorner.Y && orientation.GetValueOrDefault(Orientation.Horizontal) == Orientation.Vertical || bottomRightCorner.Y > bottomRightCorner.X && orientation.GetValueOrDefault(Orientation.Horizontal) == Orientation.Horizontal) { return(new TransformedBitmap(bitmap, new RotateTransform(90))); } return(bitmap); }