views:

136

answers:

1

Hi Guys,

I want to ask for suggestions on how to optimize a repaint in Compact Framework? GetHashCode() didn't help because it always return a different hash code.

Anyway, I have a program which you can drag and resize an object in run time. This object is a transparent object and it has a PNG image which also dynamically resize relative to object client size.

Though I noticed, (e.g. I have 4 transparent object and I'm dragging or resizing one) all 4 of them triggers OnPaintBackground even if the 3 are not moving. Another one when am just tapping on the one object .. it sill triggers onPaintBacground(). Anyway, I don't have a problem when this events get triggered.

What I like to do is optimization and that means I only have to repaint the object when it's necessary.

Can you guys please give a suggestions?

here's my pseudo C# code

Bitmap _backBuff;
onResize() {
   if(_backBuff != null) _backBuff.Dispose();
   _backBuff = new Bitmap(ClientSize.Width, ClientSize.Height);
   Invalidate();
}

onPaintBackground(e) /*have to use onPaintBackground because MSDN said it's faster*/ {
   using(Graphics g = Graphics.FromImage(_backBuff)) {
      g.Clear(Color.Black);

      // draw background
      ....some interface calling here
      ....and paint the background

      // draw alpha PNG
      .. get hDc
      .. paint PNG
      .. release hDc
   }

   e.Graphics.DrawImage(_backBuff,0,0);
}

Thanks in advance.

+1  A: 

Got an idea

I have to check for new location or new size differences between the old ones. If one of them are new, then do a repaint .. else .. paint the _backBuff (which acts as a cache image).

I implemented it and it looks good so far about repainting or painting the cache.

Nullstr1ng
Ye, a bit of dirty checking should do the job as you have just done. The other possibility is to perform the painting of all items within one object as opposed to having several smaller objects all drawing away themselves. This would help you control the painting better at the cost of taking some time to setup in the first place.
Quibblesome

related questions