更快地显示图片框 [英] Display picture box faster

查看:21
本文介绍了更快地显示图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像快速加载到图片框中并在其上绘图.我将位图分配给图片框和它显示之间有 0.13 秒的延迟.每当我执行 picturebox.refresh() 时,调用paint方法之前的延迟都是相同的0.13-0.15秒.有什么办法可以摆脱这种延迟吗?

I am trying to load images quickly into a picturebox and draw on them. I have a .13 second delay between the time I assign a bitmap to the picture box and when it shows up. And whenever I do a picturebox.refresh(), it is the same delay of .13 - .15 seconds before the paint method is called. Is there any way to get rid of this delay?

我在 Visual Studio 2010 中使用 C#.我使用 FreeImage 库加载图像.

I am using C# in Visual Studio 2010. I load the images using FreeImage library.

这是我的pictureBox_MouseMove事件中的代码:

Here is the code in my pictureBox_MouseMove event:

if (IsMouseDown || DrawLine.Checked || IsMovingBox)  
{  
  Tracing.Trace("Update Picture Box");  
  pictureBox.Refresh();  
} 

然后,当我的绘画事件被调用时,我会画一条线.延迟在两条走线之间.

Then I trace out a line when my paint event is called. The delay is between the two trace lines.

如果我使用 117kb 的双色调 tiff 图像,则延迟为 0.13 秒.将此图像加载到内存中需要 0.04 秒.用这个位图替换我的图片框位图需要 0.01 秒.

If I use a bitonal tiff image at 117kb the delay is .13 seconds. To load this image into memory takes .04 seconds. To replace my picturebox bitmap with this bitmap takes .01 seconds.

如果我使用 1125kb 的灰度 jpg 图像,延迟为 0.14 秒.将此图像加载到内存中需要 0.26 秒.用这个位图替换我的图片框位图需要 0.03 秒.

If I use a gray scale jpg image at 1125kb the delay is .14 seconds. To load this image into memory takes .26 seconds. To replace my picturebox bitmap with this bitmap takes .03 seconds.

推荐答案

假设您的代码中没有其他延迟会阻止 UI 线程重新进入消息循环,以便可以调用 OnPaint() 方法:您的 Paint 事件处理程序在 PictureBox 绘制图像之后被调用 .尚不可见,PB 使用双缓冲.

Assuming there are no other delays in your code that would prevent the UI thread from re-entering the message loop so that the OnPaint() method can be called: your Paint event handler gets called after PictureBox has drawn the Image. It isn't yet visible, PB uses double-buffering.

当必须调整大小以适合 PB 的客户区时,绘制该图像的成本会很高.这在您的情况下很有可能,因为您的图像非常大.它使用高质量的双三次过滤器使调整大小的图像看起来很好.尽管结果不错,但还是相当昂贵.

That image gets expensive to draw when it has to be resized to fit the PB's client area. Which is very likely in your case because your images are pretty large. It uses a high-quality bi-cubic filter to make the resized image look good. That's pretty expensive, albeit that the result is good.

为避免该费用,请在将图像分配给 Image 属性之前自行调整图像大小.使其与 PB 的 ClientSize 一样大.

To avoid that expense, resize the image yourself before assigning it to the Image property. Make it just as large as the PB's ClientSize.

这本身就会产生很大的不同.您可以做的下一件事是使用 32bppPArgb 像素格式创建缩放位图.这种格式比任何其他格式快 10 倍,因为它与大多数机器上的视频适配器匹配,因此不需要像素格式转换.

That's going to make a big difference in itself. The next thing you can do is to create the scaled bitmap with the 32bppPArgb pixel format. It's the format that's about 10 times faster then any other because it matches the video adapter on most machines so no pixel format conversions are necessary.

一些代码:

    private void loadImage(string path) {
        using (var srce = new Bitmap(path)) {
            var dest = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            using (var gr = Graphics.FromImage(dest)) {
                gr.DrawImage(srce, new Rectangle(Point.Empty, dest.Size));
            }
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
            pictureBox1.Image = dest;
        }
    }

您可能需要对此进行修改,以便图像保持其纵横比.首先按原样尝试,以确保您确实获得了性能改进.

You'll probably want to tinker with this so the image preserves its aspect ratio. Try it first as-is to make sure you do get the perf improvement.

这篇关于更快地显示图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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