问题描述
我正在尝试使用使用代码在 Mono C# 中获取屏幕截图,但是当我调用 CopyFromScreen 时,我得到了 System.NotImplementedException.我的代码适用于 .NET,那么是否有另一种使用 Mono 获取屏幕截图的方法?
I'm trying to use use code get a screenshot in Mono C# but I'm getting a System.NotImplementedException when I call CopyFromScreen. My code works with .NET, so is there an alternate way of getting a screenshot using Mono?
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);
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
bitmap.Save(memoryStream, imageFormat);
bitmap.Save(@" mpscreenshot.png", ImageFormat.Png);
我使用的是 Mono JIT 编译器版本 2.4.2.3 (Debian 2.4.2.3+dfsg-2)
I am using Mono JIT compiler version 2.4.2.3 (Debian 2.4.2.3+dfsg-2)
更新:Mono 无法截取屏幕截图.伤心.跛脚.
UPDATE: Mono cannot take screenshots. Sad. Lame.
推荐答案
我想另一种方法是使用 gtk# 来获取屏幕截图.您需要创建一个支持 GTK# 的单声道项目,然后执行以下代码:
I guess an alternative would be using gtk# to get a screenshot. You would need to create a mono project with GTK# support, after that following code should execute:
Gdk.Window window = Gdk.Global.DefaultRootWindow;
if (window!=null)
{
Gdk.Pixbuf pixBuf = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, false, 8,
window.Screen.Width, window.Screen.Height);
pixBuf.GetFromDrawable(window, Gdk.Colormap.System, 0, 0, 0, 0,
window.Screen.Width, window.Screen.Height);
pixBuf.ScaleSimple(400, 300, Gdk.InterpType.Bilinear);
pixBuf.Save("screenshot0.jpeg", "jpeg");
}
您也可以使用 PInvoke 并直接调用 GTK 函数.
you could also use PInvoke and call GTK functions directly.
希望这会有所帮助,问候
hope this helps, regards
这篇关于如何使用 Mono C# 截屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!