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); }
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 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); }
public void ViewCommand() { try { PdfDocument document = new PdfDocument(); document.Info.Title = "Invisible Fence Contract"; PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("Times New Roman", 12, XFontStyle.Regular); XFont fontTitle = new XFont("Times New Roman", 18, XFontStyle.Bold); XFont smallFont = new XFont("Times New Roman", 8, XFontStyle.Regular); XFont smallItalFont = new XFont("Times New Roman", 8, XFontStyle.Italic); XFont boldFont = new XFont("Times New Roman", 12, XFontStyle.Bold); XFont smallBoldFont = new XFont("Times New Roman", 10, XFontStyle.Bold); XImage logo = XImage.FromFile(@"../../Images/logo.jpg"); gfx.DrawImage(logo, 10, 5, logo.PixelWidth - 20, logo.PixelHeight); gfx.DrawLine(XPens.CadetBlue, 10, 50, page.Width - 10, 50); //Customer Information gfx.DrawString("Name " + " Referred by", font, XBrushes.Black, 15, 70); gfx.DrawLine(XPens.Black, 15, 73, page.Width - 15, 73); gfx.DrawString("Address " + " City" + " State" + " ZIP", font, XBrushes.Black, 15, 90); gfx.DrawLine(XPens.Black, 15, 93, page.Width - 15, 93); gfx.DrawString("Home Phone " + " Cell Phone" + " Email", font, XBrushes.Black, 15, 110); gfx.DrawLine(XPens.Black, 15, 113, page.Width - 15, 113); gfx.DrawString("My Property contains unmarked underground: " + " Landscape Lighting" + " Sprinklers", font, XBrushes.Black, 15, 130); gfx.DrawLine(XPens.Black, 15, 133, page.Width - 15, 133); gfx.DrawString("Pet Name(s) " + " Breeds(s) " + " Age(s) " + " Pre-existing Sensitivities" + " No" + " Yes(if yes, please note below)", font, XBrushes.Black, 15, 150); gfx.DrawLine(XPens.Black, 15, 153, page.Width - 15, 153); //Solutions Totals gfx.DrawString("SOLUTIONS", fontTitle, XBrushes.Black, 15, 170); gfx.DrawString("SAFETY SOLUTION " + " Subtotal $", font, XBrushes.Black, 15, 190); gfx.DrawLine(XPens.Black, 15, 193, 400, 193); gfx.DrawString("PROTECTION SOLUTION " + " Subtotal $", font, XBrushes.Black, 15, 210); gfx.DrawLine(XPens.Black, 15, 213, 400, 213); gfx.DrawString("FREEDOM SOLUTION " + " Subtotal $", font, XBrushes.Black, 15, 230); gfx.DrawLine(XPens.Black, 15, 233, 400, 233); //Add-a-pet totals gfx.DrawString("ADD-A-PET", fontTitle, XBrushes.Black, 15, 260); gfx.DrawString("(includes Computer Collar Unit, Training, and 1-Year Power Cap Plan)", smallFont, XBrushes.Black, 120, 260); gfx.DrawString("SAFETY Wired Add-A-Pet " + "X $499" + " $", font, XBrushes.Black, 15, 280); gfx.DrawLine(XPens.Black, 15, 283, 400, 283); gfx.DrawString("GPS 2.0 Wire Free Add-A-Pet " + "X $699" + " $", font, XBrushes.Black, 15, 300); gfx.DrawLine(XPens.Black, 15, 303, 400, 303); gfx.DrawString("PROTECTION Add-A-Pet " + "X $" + " $", font, XBrushes.Black, 15, 320); gfx.DrawLine(XPens.Black, 15, 323, 400, 323); gfx.DrawString("FREEDOM Add-A-Pet " + "X $" + " ", font, XBrushes.Black, 15, 340); gfx.DrawLine(XPens.Black, 15, 343, 400, 343); //Professional Installation gfx.DrawString("PROFESSIONAL INSTALLATION", fontTitle, XBrushes.Black, 15, 370); gfx.DrawString("Outdoor Installation " + " $" + " $", font, XBrushes.Black, 15, 390); gfx.DrawLine(XPens.Black, 15, 393, 400, 393); gfx.DrawString("Indoor Extension Loop (Up to 100') " + " X $99" + " $", font, XBrushes.Black, 15, 410); gfx.DrawLine(XPens.Black, 15, 413, 400, 413); gfx.DrawString("Outdoor Extension Loop (Up to 100') " + " X $99" + " $", font, XBrushes.Black, 15, 430); gfx.DrawLine(XPens.Black, 15, 433, 400, 433); gfx.DrawString("Pet Door Installation " + " X $" + " $", font, XBrushes.Black, 15, 450); gfx.DrawLine(XPens.Black, 15, 453, 400, 453); //Training and totals gfx.DrawString("TRAINING", fontTitle, XBrushes.Black, 15, 480); gfx.DrawString("Single Refresher Train " + " X $129" + " $", font, XBrushes.Black, 15, 500); gfx.DrawLine(XPens.Black, 15, 503, 400, 503); gfx.DrawString("Installation Date: " + " ", font, XBrushes.Black, 15, 540); gfx.DrawLine(XPens.Black, 15, 543, 400, 543); gfx.DrawString("Solution Total " + " $", font, XBrushes.Black, 15, 560); gfx.DrawLine(XPens.Black, 15, 563, 400, 563); gfx.DrawString("Tax " + " $", font, XBrushes.Black, 15, 580); gfx.DrawLine(XPens.Black, 15, 583, 400, 583); gfx.DrawString("Labor/Install Total $", font, XBrushes.Black, 15, 600); gfx.DrawLine(XPens.Black, 15, 603, 400, 603); gfx.DrawString("Total $", boldFont, XBrushes.Black, 15, 625); gfx.DrawLine(XPens.Black, 15, 628, 400, 628); gfx.DrawString("Less Deposit $", font, XBrushes.Black, 15, 645); gfx.DrawLine(XPens.Black, 15, 648, 400, 648); gfx.DrawString("Balance $", boldFont, XBrushes.Black, 15, 665); gfx.DrawLine(XPens.Black, 15, 668, 400, 668); gfx.DrawString("Prices are valid for 10 days. Balance payable at time of delivery.", smallItalFont, XBrushes.Black, 15, 677); //Signature and fine print const string sigImageFileName = "signatureImg.png"; PngBitmapEncoder png = new PngBitmapEncoder(); gfx.DrawString("By signing here I agree to the terms and conditions listed on the back of this agreement:", smallBoldFont, XBrushes.Black, 15, 695); //Create image of customer signature to display in pdf try { using (FileStream fs = new FileStream("strokes.isf", FileMode.Open, FileAccess.Read)) { StrokeCollection sc = new StrokeCollection(fs); ink.Strokes = sc; fs.Close(); } ink.Arrange(new Rect(0, 0, 350, 40)); ink.Width = 350; ink.Height = 40; RenderTargetBitmap rtb = new RenderTargetBitmap(Convert.ToInt32(ink.Width), Convert.ToInt32(ink.Height), 96d, 96d, PixelFormats.Default); rtb.Render(ink); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); using (Stream fileStream = File.Create(sigImageFileName)) { encoder.Save(fileStream); } //Draw Signature to pdf XImage sigImg = XImage.FromFile(sigImageFileName); double x = (250 - sigImg.PixelWidth * 72 / sigImg.HorizontalResolution) / 2; gfx.DrawImage(sigImg, x, 718); } catch (Exception) { MessageBox.Show("Error creating image."); } gfx.DrawLine(XPens.Black, 15, 740, 400, 740); gfx.DrawString("Purchaser Signature Date", smallItalFont, XBrushes.Black, 15, 747); gfx.DrawString("Yes, I consent to be a referral source and authorize the disclosure of my contact information.", smallFont, XBrushes.Black, 15, 770); const string filename = "Contract.pdf"; document.Save(filename); Process.Start(filename); } catch (Exception e) { Console.WriteLine(e.Message); } }