我希望在我的应用程序中向用户提供一个缩小的桌面屏幕截图。
有没有一种方法可以截取当前用户Windows桌面的屏幕截图?
我正在使用C#编写,但如果有其他语言的更好解决方案,我也愿意尝试。
需要澄清的是,我需要的是Windows桌面的屏幕截图 - 只包括壁纸和图标; 没有应用程序或任何被聚焦的东西。
我希望在我的应用程序中向用户提供一个缩小的桌面屏幕截图。
有没有一种方法可以截取当前用户Windows桌面的屏幕截图?
我正在使用C#编写,但如果有其他语言的更好解决方案,我也愿意尝试。
需要澄清的是,我需要的是Windows桌面的屏幕截图 - 只包括壁纸和图标; 没有应用程序或任何被聚焦的东西。
MIN_ALL
消息)MIN_ALL_UNDO
消息)。我认为你的目标是拍摄实际桌面(包括壁纸和图标),而不是其他任何内容。
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;
}
根据不同的多显示器情况进行调整(虽然听起来对于你的应用程序应该可以正常工作)。
在应用程序运行之前,您可以始终在Windows启动时捕获屏幕截图或使用桌面切换来使用其他桌面。请看这里: http://www.codeproject.com/Articles/7666/Desktop-Switching 切换到其他桌面,拍照并记住您不需要显示它,只需创建“DESKTOP”即可。
GetDesktopWindow
,但它捕获了在桌面前面的窗口。 - Aivan Monceller