我使用Bing贴图WPF控件创建了WPF应用程序。我只想截图Bing地图控件。
正在使用此代码制作屏幕截图:
// 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();
我的问题:
在Width
和Height
似乎是坏的(假值)。生成的屏幕截图似乎使用了错误的坐标。
我的屏幕截图:
我的期望:
为什么会得到这个结果?我在发布模式下尝试过,没有Visual Studio,结果是一样的。
屏幕截图是屏幕快照……屏幕上的所有内容。您想要的是保存单个图像,UIElement
然后可以使用RenderTargetBitmap.Render
Method来完成。Visual
幸运的是,此方法采用一个输入参数,它是所有UIElement
s的基类之一。因此,假设您要保存一个.png文件,可以执行以下操作:
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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句