0

I have a form that contains a spinner and a blank area where I'll be drawing images with the scale set on the slider. The image paths are created when the form loads and won't change. Currently I'm using something like this to display the images:

    void drawImages(void)
    {
        //...

        clearImages();
        for each(String ^image in images)
        {
            box = gcnew PictureBox;
            box->Name = "pictureBox" + i.ToString(); //Used when deleting.
            // setting properties snipped
            box->Image = Image::FromFile(image);
            Controls->Add(box);
         }
    }

And here's the clearing code, wired to 'value changed' event of the spinner and the 'resized' event of the form:

    void clearImages(void)
    {
        for(int i=0; i<images->Count; ++i)
            Controls->RemoveByKey("pictureBox" + i.ToString());
    }

The problem is that they pictures take awfully long to reload and flicker because that. Is there a way to remedy that? The one improvement I know I want to make is to redraw the images when the user lifts the mouse from the spinner, not redraw it every single time it changes.

3
  • It flickers because you remove and add controls. It takes a long time because you reload the images. This is certainly not something you want to do when you resize the form. Resize the picture boxes, if necessary, don't re-create them. No idea what the spinner does. Mar 20, 2011 at 21:24
  • It's for setting the scale of the images. When you increase the scale, the images get larger and are redrawn in a different layout. Mar 20, 2011 at 21:44
  • Yes, resize the control, don't re-create it. Easy to do with the Anchor property. Did I mention using OnPaint instead before? Mar 20, 2011 at 21:55

1 Answer 1

0

So, I wrote an app once that had to adjust various parameters in an image based on the value of a slider and there are a couple of things you can try.

First, cache your images. You are loading them from disk each time which will be much slower than retrieving them from memory. Only load from disk when you have to.

Second, don't add and remove controls, instead you can just use existing controls to display the images and change the image/resize the control(s) as needed.

Third, double buffer your display logic. It may not help you much in this case because you are blitting them to the screen in one shot anyway, but it's worth a try.

Fourth, you could use a timer instead of the value changed event of the spinner. When the user drags the slider start the timer and, in the timer Tick event perform your logic. When the user lets go of the slider stop the timer. This way you can control the maximum update speed.

In the end I had to get right at the image data directly to perform my image manipulations because GDI+ was just a bit too slow. Again though, you're not actually changing pixel data, so a bit block transfer (blt) should be fast enough.

I hope some of that helps.

2
  • Just keeping the controls did the trick even with images far larger than the application is supposed to handle. Mar 20, 2011 at 21:43
  • Yeah, creating, adding, and removing those controls is expensive in a case like this. Also, they use some of the techniques above (namely 1 and possibly 3 and 5), so you get that for free. Mar 20, 2011 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.