views:

261

answers:

1

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  A: 

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.

Hans Passant
Do you mean to clip the image? Or to resize the image as in scaling? Either way, I can't figure out why an empty derived class can cause such different behaviors. Thanks for the tips though, I will definitely give them a try.
Dan7
No, resize it, the same way PB does. This problem is definitely *not* caused by the derived class, that's just not the way WF works.
Hans Passant
In my example I set SizeMode to Normal so only the upper-left corner of the picture is shown. But I followed your advice and set Image property to a pre-clipped bitmap and it did improve! Still why is the difference between the base control and the does-nothing-new derived control in the first place?
Dan7