pictureBox图像处理异常 [英] pictureBox image dispose exception

查看:61
本文介绍了pictureBox图像处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近想尝试 AForge.NET ,因为我发现它很简单,所以我决定使用Video.FFMPEG名称空间进行一些简单的视频播放,因此可以将每个帧直接放在pictureBox上.仅此一项效果很好,但是我想在不重要之后就丢弃每个图像,因为它没有明显的原因占用了约1.5GB的内存.那是我的问题开始的地方.由于某种原因,它有时只会抛出此异常(通常在调整窗口大小时).我不太确定这可能是由什么引起的.也许这确实是一个愚蠢的错误.我的猜测是它可能是由计时器引起的,但是我可能犯了一个完全不同的错误,只是看不到它.这是我不断得到的例外:

I recently wanted to try out AForge.NET as I found it to be quite simple, so I decided to make some simple video playback using Video.FFMPEG namespace from it, so I could put each frame directly on a pictureBox. That alone works well, but I wanted to dispose every image after not being important because it took about 1.5GB memory for no apparent reason. That's where my problem started. For some reason, it sometimes will just throw this exception (usually when resizing the window). I'm not really sure what it could be caused by. Maybe it's really a stupid mistake. My guess is that it could be caused by timer, but I could have done a completely different mistake and just cannot see it. This is the exception I keep getting:

    ************** Exception Text **************
System.ArgumentException: Parameter is not valid.
   at System.Drawing.Image.get_Width()
   at System.Drawing.Image.get_Size()
   at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

这是代码(我确实意识到公共变量不好,我只是在测试):

This is the code (I do realize that public variables are not good, I'm just testing of course):

public long i = 0;
public Bitmap img;
public VideoFileReader reader;
public System.Timers.Timer aTimer;

public void render(object source, ElapsedEventArgs e)
{
    if (img != null) img.Dispose();
    if (i < reader.FrameCount)
    {
        img = reader.ReadVideoFrame();
        pictureBox1.Image = img;
    }
    i++;
}

private void button1_Click(object sender, EventArgs e)
{
    reader = new VideoFileReader();
    aTimer = new System.Timers.Timer();
    reader.Open("d:\\result.avi");
    aTimer.Elapsed += new ElapsedEventHandler(render);
    aTimer.Interval = reader.FrameRate;
    aTimer.Enabled = true;
}

推荐答案

我想我在计时器方面错过了一些东西,在这种情况下,它们似乎工作得最好.对于想使用AForge.NET进行播放的人来说,这可能是一个解决方案.我推迟了计时器,将backgroundWorker与Stopwatch一起使用,到目前为止没有发生任何问题.

I guess I missed something when it comes to timers, they don't seem to work the best for this case. For people who would like to use AForge.NET for playback, this might be a solution. I put off the timers and used backgroundWorker with Stopwatch instead, no issues occuring so far.

    public Image img;
    public VideoFileReader reader;

    private void button1_Click(object sender, EventArgs e)
    {
        reader = new VideoFileReader();
        reader.Open("d:\\result.avi");
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Stopwatch watch = new Stopwatch();
        for (i=0;i<reader.FrameCount;i++)
        {
            img = pictureBox1.Image;
            pictureBox1.Image = reader.ReadVideoFrame();
            if (img != null) img.Dispose();
            watch.Start();
            while (watch.ElapsedMilliseconds < reader.FrameRate);
            watch.Stop();
            watch.Reset();
        }
    }

这篇关于pictureBox图像处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆