Render InkCanvas to image file
// *************************** | |
// SOURCE - https://github.com/hassan-theitguy/WpfDrawingApp/blob/1b21dcc4ef3da2fa15e26087d4087d135139f7be/WpfDrawingApp/MainWindow.xaml.cs | |
// *************************** | |
// If I render the canvas directly the result includes the left menu area (and is blank). | |
// Got the idea from online to copy to a DrawingVisual and then render that. | |
DrawingVisual Drawing = new DrawingVisual(); | |
// How come DrawingArea width is of type double? | |
// Does converting to int affect the render in any way? | |
int width = (int)DrawingArea.ActualWidth; | |
int height = (int)DrawingArea.ActualHeight; | |
using (DrawingContext DrawingContext = Drawing.RenderOpen()) | |
{ | |
VisualBrush Brush = new VisualBrush(); | |
Brush.AutoLayoutContent = true; | |
Brush.Visual = DrawingArea; | |
DrawingContext.DrawRectangle(Brush, null, new Rect(0, 0, width, height)); | |
} | |
// Read that "96 is the most common dpi for desktop displays." | |
RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); | |
bmp.Render(Drawing); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment