Painting of custom control slow

A

Alien

I have a hex editor-type class that extends UserControl and paints its data
to a PictureBox. Basically the problem is that repainting it takes usually
between 60 and 80ms, which may seem pretty fast but is not good enough when
you have to repaint very frequently. For example, when you scroll the
control or select blocks of text quickly.

I have it paint its graphics to an offscreen Graphics instance, which I then
transfer to the PictureBox using g.DrawImage(Image m, int x, int y)...
Making it paint directly onto the PictureBox is (understandably) even
slower.

So, is there anything I can do to improve the situation or am I stuck? I'm
on a 1.4ghz Athlon by the way.


Thanks
 
J

Jon Davis

I've done only a small amount of System.Drawing work, but I think I can
still make a suggestion: Don't paint anything that hasn't changed. When a
form is updated for painting, find the region that is being repainted, then
repaint only that region. A little 32x32 square or even a skinny 2x500
rectangle will paint much faster than a massive 640x480 area.

Also, be sure you're double-buffering, to eliminate the flicker ...

public MyControl()
{

// Activates double buffering
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
//
// Required for Windows Form Designer support
//
InitializeComponent();

}


HTH,
Jon
 
A

Alien

Thanks for your help

I am double-buffering, though not using the SetStyle method. Painting only
the regions that have changed can get a bit tricky in a textbox, but I
should probably give it a try. However, it does not solve my main problem..
When the control is scrolled, everything changes, so I'll have to repaint
the entire control anyway.

I'm now trying to use GDI API functions instead of the wrappers (GDI+) .NET
provides, and in my experimenting they are a bit faster (repaint time down
to 40-50ms, and if I turn off double-buffering, 10-20ms).
 
A

Alien

Thanks for the link Lucean, I'll check it out.

Lucean Morningside said:
"Alien" <[email protected]> wrote in message

There's only one thing that you can do: profile your code to find all
performance bottlenecks. I ran into a similar performance problem once when
I was writing a fractal image compression and decompression program. I tried
to blindly optimize the code, but I never got any real performance increase. Not
until I ran my code through a profiler. Only then was I able to locate the exact
methods that needed optimization. You can use Compuware's DevPartner Profiler
Community Edition, which can be found at:
http://www.compuware.com/products/devpartner/profiler/default.asp

Hope this helps.

-LM
 
A

Alien

Heh, well.. I can get it to hover around 10ms (even drop to 7 or 8
sometimes) with double-buffering turned off and using raw GDI calls, but
with double-buffering it's still noticeably sluggish.

So it's a trade-off between flickering like hell or slow repaint.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top