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

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

问题描述

我试图快速加载图像到一个PictureBox和借鉴他们。我有我分配一个位图图片框,当它出现的时间之间的0.13秒延迟。每当我做了 picturebox.refresh(),这是.13相同的延迟 - 被称为paint方法之前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?

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

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

下面是我pictureBox_MouseMove事件中的code:

Here is the code in my pictureBox_MouseMove event:

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

然后我描绘出一条线时,叫我Paint事件。延迟是两个迹线之间。

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秒。要更换我的PictureBox的位图与此位图需要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秒。要更换我的PictureBox的位图与此位图需要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.

推荐答案

假设有在code无其他延迟,将prevent重新进入消息循环UI线程,这样的的OnPaint( )方法可以称为:您的Paint事件处理程序被调用的 的图片框已制定后的图像。现在尚不可见,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的客户区。这是你的情况很可能是因为您的图片pretty大。它采用了高品质的双三次过滤器,使调整后的图像好看。这是pretty昂贵的,虽然这个结果还是不错的。

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.

要避免这种情况的费用,将其分配给图像属性之前调整自己的形象。让它只是大如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.

有些code:

    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.Dispose();
            pictureBox1.Image = dest;
        }
    }

您可能会想这个鼓捣因此图像preserves长宽比。第一次尝试,因为,是确保你得到了改善PERF

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天全站免登陆