当我们需要在Linux和OS X上使用Mono拍摄截屏时,我们遇到了同样的问题。
实际上,在Linux上可以使用CopyFromScreen
。但是您需要安装mono-complete软件包,其中包括System.Drawing。
对于OS X,最可靠的方法是启动screencapture
命令行工具。它默认存在。
对于Linux,为了使其更加可靠,可以使用ImageMagic中的import
命令行(在这种情况下,您需要使它成为依赖项),或强制依赖于包括System.Drawing的mono-complete
软件包。根据我们的测试,Gtk#方法不可靠,并且可能会提供空白屏幕或失败并出现异常(我们每天在各种机器上拍摄数千张截图)。
另一件事是,当您需要在一个截图中捕获多个显示器时,您必须在单独通过CopyFromScreen拍摄其后正确对齐单独的截图。
因此,我们目前有这个解决方案:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Pranas
{
public static class ScreenshotCapture
{
#region Public static methods
public static Image TakeScreenshot(bool onlyPrimaryScreen = false)
{
try
{
return WindowsCapture(onlyPrimaryScreen);
}
catch (Exception)
{
return OsXCapture(onlyPrimaryScreen);
}
}
#endregion
#region Private static methods
private static Image OsXCapture(bool onlyPrimaryScreen)
{
var data = ExecuteCaptureProcess(
"screencapture",
string.Format("{0} -T0 -tpng -S -x", onlyPrimaryScreen ? "-m" : ""));
return data;
}
private static Image ExecuteCaptureProcess(string execModule, string parameters)
{
var imageFileName = Path.Combine(Path.GetTempPath(), string.Format("screenshot_{0}.jpg", Guid.NewGuid()));
var process = Process.Start(execModule, string.Format("{0} {1}", parameters, imageFileName));
if (process == null)
{
throw new InvalidOperationException(string.Format("Executable of '{0}' was not found", execModule));
}
process.WaitForExit();
if (!File.Exists(imageFileName))
{
throw new InvalidOperationException(string.Format("Failed to capture screenshot using {0}", execModule));
}
try
{
return Image.FromFile(imageFileName);
}
finally
{
File.Delete(imageFileName);
}
}
private static Image WindowsCapture(bool onlyPrimaryScreen)
{
if (onlyPrimaryScreen) return ScreenCapture(Screen.PrimaryScreen);
var bitmaps = (Screen.AllScreens.OrderBy(s => s.Bounds.Left).Select(ScreenCapture)).ToArray();
return CombineBitmap(bitmaps);
}
private static Bitmap ScreenCapture(Screen screen)
{
var bitmap = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(
screen.Bounds.X,
screen.Bounds.Y,
0,
0,
screen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
return bitmap;
}
private static Image CombineBitmap(ICollection<Image> images)
{
Image finalImage = null;
try
{
var width = 0;
var height = 0;
foreach (var image in images)
{
width += image.Width;
height = image.Height > height ? image.Height : height;
}
finalImage = new Bitmap(width, height);
using (var g = Graphics.FromImage(finalImage))
{
g.Clear(Color.Black);
var offset = 0;
foreach (var image in images)
{
g.DrawImage(image,
new Rectangle(offset, 0, image.Width, image.Height));
offset += image.Width;
}
}
}
catch (Exception ex)
{
if (finalImage != null)
finalImage.Dispose();
throw ex;
}
finally
{
foreach (var image in images)
{
image.Dispose();
}
}
return finalImage;
}
#endregion
}
}
或者通过NuGet安装它(免责声明:我是作者):
PM> Install-Package Pranas.ScreenshotCapture
我们在产品中广泛使用它,并在许多设置上进行定期的代码改进,并将注释放入
博客中。