How to take a screenshot with Mono C#?(如何使用 Mono C# 截屏?)
问题描述
我正在尝试使用使用代码在 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# 截屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Mono C# 截屏?


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01