如何采取一个WPF控件的屏幕截图?控件、截图、屏幕、WPF

2023-09-02 10:53:12 作者:国服直男第一人!

我在使用Bing地图WPF控件的WPF应用程序。 我希望能够截图只Bing地图的控制。

I created an WPF application using the bing maps wpf control. I would like to be able to screenshot only the bing maps control.

时使用code,以使截图:

Is use this code in order to make the screenshot :

// Store the size of the map control 
int Width = (int)MyMap.RenderSize.Width;
int Height = (int)MyMap.RenderSize.Height;
System.Windows.Point relativePoint = MyMap.TransformToAncestor(Application.Current.MainWindow).Transform(new System.Windows.Point(0, 0));
int X = (int)relativePoint.X;
int Y = (int)relativePoint.Y;

Bitmap Screenshot = new Bitmap(Width, Height);
Graphics G = Graphics.FromImage(Screenshot);
// snip wanted area
G.CopyFromScreen(X, Y, 0, 0, new System.Drawing.Size(Width, Height), CopyPixelOperation.SourceCopy);

string fileName = "C:\myCapture.bmp";
System.IO.FileStream fs = System.IO.File.Open(fileName, System.IO.FileMode.OpenOrCreate);
Screenshot.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
fs.Close();

我的问题:

宽度和嗨似乎是坏的(falses值)。 生产的截图显示使用坏的坐标。

The Width and Heigh appear to be bad ( falses values). The screenshot produced appear to use bad coordinates.

这是我的截图:

这里是我等待:

任何人有一些想法,为什么我勒阿弗尔这样的结果? 我想在发布MOD,并且没有Visual Studio中,结果是一样的。

Anyone have some idea why i havre this result ? I tried in Release mod, and without Visual studio, result is the same.

多谢:)

推荐答案

一个截图是一个镜头的画面...的所有的屏幕上。你想要的是保存图像从一个单一的的UIElement ,你可以做到这一点使用RenderTargetBitmap.Render方法。这个方法有一个视觉输入参数,幸运的是,这是基类的所有的UIElement 一张。因此,假如你想保存.png文件,你可以这样做:

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);
}