DotnetSaving a control to png in WPF (c#)
2015 · 10 · 15
1 min read
Paper
I was going to use System.Drawing like I did before..
turns out wpf already has this built in..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| // uiPage.ren
public void DoPageToPng(string fileName)
{
RenderTargetBitmap rtb = new RenderTargetBitmap((int)uiPage.ActualWidth, (int)uiPage.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(uiPage);
PngBitmapEncoder png2 = new PngBitmapEncoder();
png2.Frames.Add(BitmapFrame.Create(rtb));
using (MemoryStream stream = new MemoryStream())
{
png2.Save(stream);
using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
{
image.Save(fileName);
}
}
}
|