How to take a screenshot of a WPF control?
A screenshot is a shot of the screen... everything on the screen. What you want is to save an image from a single UIElement
and you can do that using the RenderTargetBitmap.Render
Method. This method takes a Visual
input parameter and luckily, that is one of the base classes for all UIElement
s. So assuming that you want to save a .png file, you could do this:
RenderTargetBitmap renderTargetBitmap =
new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(yourMapControl);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream fileStream = File.Create(filePath))
{
pngImage.Save(fileStream);
}