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()); }
public void Clear() { canvasSignaturePad.Strokes.Clear(); canvasSignaturePad.UpdateLayout(); lStroke.Clear(); }
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); }