Files

39 lines
1.3 KiB
C#
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace DTS.Common.Utils
{
/// <summary>
/// this class holds PNG image functions
/// </summary>
public static class PNGImageUtil
{
/// <summary>
/// renders the given element as a png to the given filepath
/// </summary>
/// <param name="view"></param>
/// <param name="fileName"></param>
public static void SaveImage(FrameworkElement view, string fileName)
{
var size = new Size(view.ActualWidth, view.ActualHeight);
var rtb = new RenderTargetBitmap((int) size.Width, (int) size.Height, 96, 96, PixelFormats.Pbgra32);
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
context.DrawRectangle(new VisualBrush(view), null, new Rect(new Point(), size));
context.Close();
}
rtb.Render(visual);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
using (var stream = new System.IO.FileStream(fileName, System.IO.FileMode.CreateNew))
{
encoder.Save(stream);
}
}
}
}