1

我有一个应用程序,其中包含一个图片框,每次将新图像加载到相机缓冲区时,它都会更新来自实时相机的图像。我的问题是,每当我收到这个实时提要时,整个应用程序都会变得非常缓慢并且(有时)没有响应。我有一个单独的线程,它基本上完成所有成像步骤,然后将新图像放入图片框中。我有点坚持如何解决这个问题,我想知道是否有人有任何想法?我不确定您需要什么样的代码,但这是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 { }
}
4

1 回答 1

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 { }
    }
于 2012-11-01T23:40:18.363 回答