WinForms performance mystery: derived PictureBox control slower than the original?

流过昼夜 提交于 2019-12-10 18:25:22

问题


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. 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.

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/2151456/winforms-performance-mystery-derived-picturebox-control-slower-than-the-origina

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!