2023年6月12日

SaveVisualToPNG

作者 dikangqie

Draw some lines on it, the save the InkCanvas to disk, using the following code.  Both the background image and the lines drawn are being saved.

<InkCanvas Name="inkcanvas">

    <InkCanvas.Background>

        <ImageBrush ImageSource="c:\..\..\someimage.jpg" />

    </InkCanvas.Background>

</InkCanvas>
public static void SaveVisualToPNG(Visual visual, string filename)

{

    RenderTargetBitmap rtb = new RenderTargetBitmap(

        (int)bounds.Width,

        (int)bounds.Height,

        96, 96, PixelFormats.Default);

   

    rtb.Render(visual);

 

    PngBitmapEncoder pngEncoder = new PngBitmapEncoder();

    pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

 

    using (Stream stream = File.Create(filename))

    {

        pngEncoder.Save(stream);

    }

}