【发布时间】:2009-05-19 03:47:40
【问题描述】:
我想在我的应用程序中为用户提供他们桌面的缩小屏幕截图。
有没有办法截取当前用户的 Windows 桌面?
我用 C# 编写,但如果有更好的解决方案用另一种语言,我愿意接受。
为了澄清,我需要一张 Windows 桌面的屏幕截图——那只是壁纸和图标;没有应用程序或任何有焦点的东西。
【问题讨论】:
标签: c# windows desktop screenshot
我想在我的应用程序中为用户提供他们桌面的缩小屏幕截图。
有没有办法截取当前用户的 Windows 桌面?
我用 C# 编写,但如果有更好的解决方案用另一种语言,我愿意接受。
为了澄清,我需要一张 Windows 桌面的屏幕截图——那只是壁纸和图标;没有应用程序或任何有焦点的东西。
【问题讨论】:
标签: c# windows desktop screenshot
您正在寻找Graphics.CopyFromScreen。创建一个大小合适的新 Bitmap,并将 Bitmap 的 Graphics 对象屏幕坐标传递给要复制的区域。
还有an article 描述了如何以编程方式拍摄快照。
对编辑的回应:我误解了您所说的“桌面”是什么意思。如果您想为桌面拍照,您必须:
MIN_ALL消息)MIN_ALL_UNDO 消息)。更好的方法是不要干扰其他窗口,而是直接从桌面窗口复制图像。 GetDesktopWindow in User32 将返回一个句柄到桌面。获得窗口句柄后,获取它的设备上下文并将图像复制到新的位图。
有an excellent example on CodeProject 说明如何从中复制图像。在“捕获窗口内容”部分中查找有关获取和创建设备上下文的示例代码。
【讨论】:
GetDesktopWindow,但它捕获了桌面前面的窗口
我的印象是,您拍摄实际桌面的照片(带有壁纸和图标),仅此而已。
1) 使用 COM 在 Shell32 中调用 ToggleDesktop()
2) 使用 Graphics.CopyFromScreen 复制当前桌面区域
3) 调用 ToggleDesktop() 恢复之前的桌面状态
编辑:是的,调用 MinimizeAll() 是好战的。
这是我整理的更新版本:
/// <summary>
/// Minimizes all running applications and captures desktop as image
/// Note: Requires reference to "Microsoft Shell Controls and Automation"
/// </summary>
/// <returns>Image of desktop</returns>
private Image CaptureDesktopImage() {
//May want to play around with the delay.
TimeSpan ToggleDesktopDelay = new TimeSpan(0, 0, 0, 0, 150);
Shell32.ShellClass ShellReference = null;
Bitmap WorkingImage = null;
Graphics WorkingGraphics = null;
Rectangle TargetArea = Screen.PrimaryScreen.WorkingArea;
Image ReturnImage = null;
try
{
ShellReference = new Shell32.ShellClass();
ShellReference.ToggleDesktop();
System.Threading.Thread.Sleep(ToggleDesktopDelay);
WorkingImage = new Bitmap(TargetArea.Width,
TargetArea.Height);
WorkingGraphics = Graphics.FromImage(WorkingImage);
WorkingGraphics.CopyFromScreen(TargetArea.X, TargetArea.X, 0, 0, TargetArea.Size);
System.Threading.Thread.Sleep(ToggleDesktopDelay);
ShellReference.ToggleDesktop();
ReturnImage = (Image)WorkingImage.Clone();
}
catch
{
System.Diagnostics.Debugger.Break();
//...
}
finally
{
WorkingGraphics.Dispose();
WorkingImage.Dispose();
}
return ReturnImage;
}
调整以适应多个监视器场景(尽管听起来这对您的应用程序来说应该可以正常工作)。
【讨论】:
您可以通过 p/invoke 查看使用GetDesktopWindow,然后获取设备上下文并拍摄快照。有很详细的教程here。这可能比扰乱现有的窗口更好。
【讨论】:
您始终可以在应用程序运行之前在 Windows 启动时截取屏幕截图 或通过桌面切换使用其他桌面。看看这里: http://www.codeproject.com/Articles/7666/Desktop-Switching 切换到其他桌面,拍照并记住你不需要显示它只需创建它“桌面”
【讨论】:
所有以前的答案都是完全错误的(最小化应用程序是荒谬的)
只需使用 SHW api :一行 代码(桌面位图被缓存,api 简单地将其传送到您的 HDC...)
【讨论】: