c# - 使用不同 DPI 捕获控件的屏幕截图
问题描述
我可以捕获正常 100% DPI 的控制屏幕截图。但是当 DPI 更改为 125% 时,屏幕截图不正确。请建议方法,以便可以使用任何 DPI 捕获屏幕截图。以下是代码
// Get absolute location on screen of upper left corner of button
System.Windows.Point locationFromScreen = this.sv.PointToScreen(new System.Windows.Point(0, 0));
// Transform screen point to WPF device independent point
PresentationSource source = PresentationSource.FromVisual(this);
System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);
var bmpScreenshot = new Bitmap((int)sv.ActualWidth,
(int)sv.ActualHeight,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen((int)targetPoints.X,
(int)targetPoints.Y,
0,
0,
new System.Drawing.Size((int)sv.ActualWidth, (int)sv.ActualHeight),
CopyPixelOperation.SourceCopy);
byte[] data;
using (var stream = new System.IO.MemoryStream())
{
bmpScreenshot.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
data = stream.ToArray();
}
var result = Convert.ToBase64String(data);
winObj = new Window1();
MemoryStream ms = new MemoryStream();
((System.Drawing.Bitmap)bmpScreenshot).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
winObj.imageViewer.Source = image;
winObj.ShowDialog();
bmpScreenshot.Dispose();
解决方案
推荐阅读
- python - 如何让 Tkinter 和 Selenium 一起工作
- azure-devops - Azure DevOps 多阶段管道,通过特定工件更改触发特定阶段
- amazon-web-services - 跟进 AWS Lex 中的问题
- python - 我的代码流没有进入 django views.py 中的 if 循环
- php - PHP 语言切换器需要在 URL 中更新
- tensorflow - bert-for-tf2:albert 分类错误(从 GPU 复制输入张量以运行身份失败:GPU 同步失败 [Op:Identity])
- r - 根据 R 中的 NA 合并重复的行
- wordpress - 带有主题文件夹卷的 Docker Wordpress 设置
- javascript - React 的 map 函数问题与示例
- websphere-liberty - 使用 json-b api 检查 json 字段