WinForms性能之谜:派生的PictureBox控件比原始的慢吗? [英] WinForms performance mystery: derived PictureBox control slower than the original?

查看:127
本文介绍了WinForms性能之谜:派生的PictureBox控件比原始的慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.NET 2.0项目中,我创建了一个空的System.Windows.Forms.PictureBox派生类:

In my .NET 2.0 project, I made an empty derived class of System.Windows.Forms.PictureBox:

public class NewPictureBox : PictureBox
{
    //absolutely nothing 
}

然后我执行了以下操作:

Then I did the following:

  1. 将派生控件和基本控件的Image属性都设置为较大的图像(800x600),SizeModeNormal(仅显示左上部分);
  2. 捕获了NewPictureBoxPictureBox的几个事件,因此在表面上拖动鼠标时可以绘制选择框;
  3. 进行设置,以便选择框的属性(Width/Height)将在NumericUpDown控件上实时更新.
  1. set both the derived control's and the base control's Image property to a rather large image (800x600), SizeMode is Normal (only the upper-left portion is displayed);
  2. hooked up several of the NewPictureBox's and PictureBox's events so a selection box can be drawn when dragging the mouse on the surface;
  3. set it up so the selection box's properties (Width/Height) will be updated on NumericUpDown controls in real time.

问题在于,在派生的PB上快速快速地拖动鼠标时,与在基础PB上进行相同操作相比,存在很大的波动". Width/Height值不会实时更新.

The problem is when dragging the mouse real fast on the derived PB, there is considerable "choppiness" compared to doing the same on the base PB. The Width/Height values are not updated in real time.

有人知道为什么会这样吗?如何使用派生的控件获得相同的平滑度?谢谢!

Does anybody know why is it like this? How do I achieve the same smoothness with the derived control? Thanks!

对于希望通过以下问题检查最小样本项目的人:

For anyone who wishes to check out the minimal sample project with the problem described:

http://www.mediafire.com/?i2nq2tmmjzx

推荐答案

通过PB调整大小以适合控件的图像非常昂贵. GDI +具有非常好的过滤器,但它不是免费提供的.在将图像分配给Image属性之前,请先自行调整其大小,以便PB不必调整其大小.

Getting an image resized by PB to fit the control is very expensive. GDI+ has a very good filter but it doesn't come for free. Resize the image yourself before you assign it to the Image property so the PB doesn't have to resize it.

使用由Format32bppPArgb创建的位图也可以产生差异,它比任何其他格式快10倍.

Using a bitmap created with Format32bppPArgb can make a big difference too, it is 10 times faster than any other format.

这篇关于WinForms性能之谜:派生的PictureBox控件比原始的慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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