Created
June 12, 2018 15:59
-
-
Save hassan-theitguy/e3b3674e3be0733f37d709eed101d3b2 to your computer and use it in GitHub Desktop.
Render InkCanvas to image file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *************************** | |
// 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