【问题标题】:pictureBox image dispose exceptionpictureBox 图像处理异常
【发布时间】:2016-10-22 17:46:40
【问题描述】:

我最近想尝试AForge.NET,因为我发现它非常简单,所以我决定使用 Video.FFMPEG 命名空间来制作一些简单的视频播放,这样我就可以将每一帧直接放在图片框上。仅此一项效果很好,但我想在不重要之后处理每张图像,因为它无缘无故地占用了大约 1.5GB 的内存。这就是我的问题开始的地方。出于某种原因,它有时会抛出这个异常(通常在调整窗口大小时)。我不确定它可能是由什么引起的。也许这真的是一个愚蠢的错误。我的猜测是它可能是由计时器引起的,但我可能犯了一个完全不同的错误,只是看不到它。这是我不断收到的异常:

    ************** 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)

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

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;
}

【问题讨论】:

    标签: c# ffmpeg


    【解决方案1】:

    我想我在定时器方面漏掉了一些东西,它们似乎在这种情况下不是最好的。对于想要使用 AForge.NET 进行回放的人来说,这可能是一个解决方案。我推迟了计时器并使用了带有 Stopwatch 的 backgroundWorker,到目前为止没有出现任何问题。

        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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多