如何采取与单C#屏幕截图? [英] How to take a screenshot with Mono C#?
问题描述
我试图用使用代码获得单C#中的截图,但我得到一个System.NotImplementedException当我打电话的 CopyFromScreen 。我的代码工作与.NET,所以有越来越使用单截图的替代方法?
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(@"\tmp\screenshot.png", ImageFormat.Png);
我使用单声道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)
更新:单不能采取截图。伤心。跛。
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");
}
您也可以使用P\Invoke并直接调用GTK的功能。
you could also use P\Invoke and call GTK functions directly.
希望这可以帮助,至于
这篇关于如何采取与单C#屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!