How to take a screenshot of a WPF control?

前端 未结 1 1160
感动是毒
感动是毒 2020-11-28 08:12

I created a WPF application using the Bing maps WPF control. I would like to be able to screenshot only the Bing maps control.

Is use this code to make the screensho

相关标签:
1条回答
  • 2020-11-28 08:34

    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 UIElements. 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);
    }
    
    0 讨论(0)
提交回复
热议问题