c# - 无法捕获全屏图像??(带有任务栏和任何其他打开的窗口)
问题描述
我对这段代码有疑问:
我需要截取我看到的所有东西(任务栏,任何打开的东西)的全屏。
我的代码只是给了我一个窗口的裁剪图片
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save("D://Changes.jpg", ImageFormat.Jpeg);
解决方案
您的显示设置设置为 125%(或更高)缩放。
您的应用程序不支持 DPI。您可以通过更新应用程序的 manifest来纠正它。
如果这对您不起作用(或者您不想使用清单),您可以 pinvoke GetDeviceCaps
API 以获得正确的宽度和高度CopyFromScreen
。
以下是您的本地定义:
private static class Win32Native
{
public const int DESKTOPVERTRES = 0x75;
public const int DESKTOPHORZRES = 0x76;
[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hDC, int index);
}
你会这样称呼它:
int width, height;
using(var g = Graphics.FromHwnd(IntPtr.Zero))
{
var hDC = g.GetHdc();
width = Win32Native.GetDeviceCaps(hDC, Win32Native.DESKTOPHORZRES);
height = Win32Native.GetDeviceCaps(hDC, Win32Native.DESKTOPVERTRES);
g.ReleaseHdc(hDC);
}
using (var img = new Bitmap(width, height))
{
using (var g = Graphics.FromImage(img))
{
g.CopyFromScreen(0, 0, 0, 0, img.Size);
}
img.Save(@"C:\users\andy\desktop\test.jpg", ImageFormat.Jpeg);
}
推荐阅读
- javascript - 如何通过多次单击按钮来解决 React.js 状态?
- python - 如何在 pandas/matplotlib 中绘制索引列?
- angular - TS 是否接受 Protractor 配置中定义的全局变量?
- python - 如何在 Python 中使用 Gtk.StyleContext.remove_provider()?
- swift - 使用相对谓词对项目进行排序的算法
- javascript - 不保证迭代顺序是什么意思?
- javascript - 创建一个指定长度的零数组javascript
- java - 使用循环双链表的双向队列中的 ClassCastException
- java - 如何根据布尔变量决定导航抽屉片段的视图?
- python - 使用 .loc 进行 Pandas 索引的意外行为