Export FrameworkElement And Its Children As Image Such JPG Or PNG | Saatody | Amit Padhiyar
In this post, I will show you an algorithm than can convert any FrameworkElement and its children component to PNG and JPG such image formats.
Follow below algorithm as a WPF C# function.
public void ExportToPng(Uri path, FrameworkElement element)
{
if (path == null) return;
// Save current canvas transform
Transform transform = element.LayoutTransform;
// reset current transform (in case it is scaled or rotated)
element.LayoutTransform = null;
// Get the size of canvas
Size size = new Size(element.Width, element.Height);
// Measure and arrange the surface
// VERY IMPORTANT
element.Measure(size);
element.Arrange(new Rect(size));
// Create a render bitmap and push the surface to it
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32);
renderBitmap.Render(element);
// Create a file stream for saving image
using (System.IO.FileStream outStream = new System.IO.FileStream(path.LocalPath, System.IO.FileMode.Create))
{
// Use png encoder for our data
PngBitmapEncoder encoder = new PngBitmapEncoder();
// push the rendered bitmap to it
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// save the data to the stream
encoder.Save(outStream);
}
// Restore previously saved layout
element.LayoutTransform = transform;
}
Comments
Post a Comment