我正在使用C#,。Net 4.5和Gtk#编写一个简单的应用程序,它捕获屏幕的一部分并将其加载到窗口中的图像上。几秒钟后,图像消失,我收到以下错误:
Glib-CRITICAL **:尝试删除时未找到源ID xxxx。
相关代码:
的Program.cs:
using System.Windows.Forms;
namespace Dota2Trainer
{
class MainClass
{
public static void Main(string[] args)
{
Gtk.Application.Init();
MiniMapOverlay myWin = new MiniMapOverlay();
myWin.KeepAbove = true;
//myWin.Decorated = false;
myWin.Resize(200, 200);
myWin.ShowAll();
ScreenCapturer screenCap = new ScreenCapturer(30, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, myWin);
screenCap.Start();
Gtk.Application.Run();
}
}
}
MiniMapOverlay.cs:
using System;
using System.Drawing;
using System.IO;
using System.Timers;
using Gtk;
namespace Dota2Trainer
{
public partial class MiniMapOverlay : Gtk.Window
{
Gtk.Image image = null;
public MiniMapOverlay() : base(Gtk.WindowType.Toplevel)
{
image = new Gtk.Image();
var buffer = System.IO.File.ReadAllBytes(".\\image0.jpeg");
var pixbuf = new Gdk.Pixbuf(buffer);
image.Pixbuf = pixbuf;
this.Add(image);
this.Build();
}
public void RefreshImage(byte[] buffer)
{
try
{
var pixbuf = new Gdk.Pixbuf(buffer);
image.Pixbuf = pixbuf;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
}
}
ScreenCapturer.cs:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Timers;
namespace Dota2Trainer
{
public delegate void OnSavedImage(byte[] screenCap);
public class ScreenCapturer
{
public System.Timers.Timer aTimer;
int interval= 30000;
readonly int screenWidth;
readonly int screenHeight;
Rectangle bounds;
const double miniMapWidthPercentage = 0.15;
const double miniMapHeightPercentage = 0.25;
Bitmap bitmap;
bool Busy = false;
ImageConverter converter = new ImageConverter();
public OnSavedImage onSaveImage;
public ScreenCapturer(int interval, int screenWidth, int screenHeight, MiniMapOverlay imageHolder)
{
this.screenHeight = screenHeight;
this.screenWidth = screenWidth;
this.interval = interval;
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = this.interval;
int miniMapWidth = (int)(screenWidth * miniMapWidthPercentage);
int miniMapHeight = (int)(screenHeight * miniMapHeightPercentage);
int miniMapStartWidth = (screenWidth - miniMapWidth);
int miniMapStartHeight = (screenHeight - miniMapHeight);
bounds = new Rectangle(miniMapStartWidth, miniMapStartHeight, miniMapWidth, miniMapHeight);
bitmap = new Bitmap(bounds.Width, bounds.Height);
onSaveImage += imageHolder.RefreshImage;
}
public void Start()
{
aTimer.Enabled = true;
}
public void OnTimedEvent(object sender, ElapsedEventArgs e)
{
try
{
if (Busy)
return;
Busy = true;
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
onSaveImage((byte[])converter.ConvertTo(bitmap, typeof(byte[])));
}
Busy = false;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
}
}
完整的源代码可以在这里找到:
https://github.com/oldtimerza/Dota2Trainer
它是如何工作的只是创建一个计时器,在一定时间后运行Screencapturer类的OnTimedEvent()方法,该方法具有一个将运行MiniMapOverlay RefreshImage()方法的委托。这段代码令人震惊,但我只想在重构之前准备好一个粗略的准备版本。
我一直在寻找年龄,为什么会发生这种情况,有什么建议吗?
答案 0 :(得分:1)
您应该使用Glib.Timeout
代替System.Timers
。 System.Timers
可能存在线程问题,并且计时器事件发生在与GTK应用程序不同的线程上。 Glib.Timeout
以一定的间隔在主GTK应用程序循环中对事件进行排队,并在同一个线程中运行。但是,它不适用于时间敏感函数,因为应用程序循环正在处理其他事件,例如鼠标点击。我已经看到了大约半秒到一秒的延迟,但这是在调试时的旧计算机上。这似乎不是您的应用程序的问题;仅供将来参考。此外,Glib.Timeout
将继续调用更新事件,直到您返回false。
uint timerId;
public void Start(uint interval)
{
timerId = GLib.Timeout.Add (interval, OnUpdateTimer);
}
protected bool OnUpdateTimer ()
{
try
{
if (Busy)
return;
Busy = true;
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
onSaveImage((byte[])converter.ConvertTo(bitmap, typeof(byte[])));
}
Busy = false;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
return true;
}