1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
/// <summary>
/// Mehod used to create a screenshot of a WPF control.
/// </summary>
/// <param name="visual">The control that will be used to create the screenshot.</param>
/// <param name="file">The file where to store the screenshot.</param>
private void CreateScreenShot(UIElement visual, string file)
{
double width = Convert.ToDouble(visual.GetValue(FrameworkElement.WidthProperty));
double height = Convert.ToDouble(visual.GetValue(FrameworkElement.HeightProperty));
if (double.IsNaN(width) || double.IsNaN(height))
{
throw new FormatException("You need to indicate the Width and Height values of the UIElement.");
}
RenderTargetBitmap render = new RenderTargetBitmap(
Convert.ToInt32(width),
Convert.ToInt32(visual.GetValue(FrameworkElement.HeightProperty)),
96,
96,
PixelFormats.Pbgra32);
// Indicate which control to render in the image
render.Render(visual);
using (FileStream stream = new FileStream(file, FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(render));
encoder.Save(stream);
}
} |
Partager