更新图片框会导致性能问题

时间:2012-11-01 20:29:32

标签: c# winforms streaming

我有一个应用程序,其中包含一个图片框,每次将新图像加载到相机缓冲区时,该图片框都会更新来自实时摄像头的图像。我的问题是,无论何时我得到这个实时源,整个应用程序变得非常缓慢(有时)没有响应。我有一个单独的线程,基本上完成所有的成像步骤,然后将新图像放在图片框中。我有点坚持如何解决问题,我想知道是否有人有任何想法?我不确定你需要什么样的代码,但这里是ImageUpdated事件,它获取图像并将其粘贴在PictureBox上。谢谢你的帮助!

void CurrentCamera_ImageUpdated(object sender, EventArgs e)
{
    try
    {
        lock (CurrentCamera.image)
        {
            if (CurrentCamera != null && CurrentCamera.image != null && !changeCam)
            {
                videoImage = CurrentCamera.videoImage;
                if (CurrentCamera.videoImage != null && this.IsHandleCreated)
                {
                    Bitmap tmp = new Bitmap(CurrentCamera.image.Width, CurrentCamera.image.Height);
                    //Creates a crosshair on the image
                    using (Graphics g = Graphics.FromImage(tmp))
                    {
                        g.DrawImage(CurrentCamera.image, new Point(0, 0));
                        g.DrawLine(crosshairPen, new Point(CurrentCamera.image.Width / 2, 0), new Point(CurrentCamera.image.Width / 2, (CurrentCamera.image.Height)));
                        g.DrawLine(crosshairPen, new Point(0, CurrentCamera.image.Height / 2), new Point((CurrentCamera.image.Width), CurrentCamera.image.Height / 2));
                        g.DrawEllipse(crosshairPen, (CurrentCamera.image.Width / 2) - crosshairRadius, (CurrentCamera.image.Height / 2) - crosshairRadius, crosshairRadius * 2, crosshairRadius * 2);
                    }
                    pictureBox1.BeginInvoke((MethodInvoker)delegate
                    {
                        pictureBox1.Image = tmp;
                    });
                }
            }
        }
    }
    catch { }
}

1 个答案:

答案 0 :(得分:0)

除了评论之外,我认为这可能会加速一点点。首先,如果没有在pictureBox1上设置图像,则不要设置另一个。其次,由于使用了pictureBox,它被优化为不闪烁,因此您可以在PictureBox1的PaintEvent方法处理程序中绘制十字准线。这是一个稍微改进的代码,避免了双位图绘制,那么你应该加速尽可能CurrentCamera_ImageUpdated方法执行,因为新图像来得非常快,C#代码即使在高位也无法处理那么多的绘图性能机器。从这里开始,继续改进。

    public Form1()
    {
        InitializeComponent();
        pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
    }

    void  pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (pictureBox1.Image != null)
        {
            int width = pictureBox1.Image.Width;
            int height = pictureBox1.Image.Height;
            e.Graphics.DrawLine(crosshairPen, new Point(width / 2, 0), new Point(width / 2, height));
            e.Graphics.DrawLine(crosshairPen, new Point(0, pictureBox1.Image.Height / 2), new Point(width, height / 2));
            e.Graphics.DrawEllipse(crosshairPen, (width / 2) - crosshairRadius, (height / 2) - crosshairRadius, crosshairRadius * 2, crosshairRadius * 2);
        }
    } 
    IAsyncResult result = null;
    void CurrentCamera_ImageUpdated(object sender, EventArgs e)
    {
        try
        {
            lock (CurrentCamera.image)
            {
                if (CurrentCamera != null && CurrentCamera.image != null && !changeCam)
                {
                    videoImage = CurrentCamera.videoImage;
                    if (CurrentCamera.videoImage != null && this.IsHandleCreated)
                    {
                        if (result != null && result.IsCompleted)
                        {
                            result = pictureBox1.BeginInvoke((MethodInvoker)delegate
                            {
                                pictureBox1.Image = CurrentCamera.videoImage;
                            });
                        }
                    }
                }
            }
        }
        catch { }
    }