Bitmap bufferring or clipping? Which is better?

Aug 17 2007 4:26 AM
Hello,

This is actually a continuation from my previous post dealing with the transparency issue using Graphics.Clear(Color.Transparent). In that post Richard and I had an interesting discussion on clipping with the Graphics object. I feel that I needed create a new post for this topic as it may deviate into a different direction.

The problem I have is mainly with performance. I'm writing a GIS app to render many thousands of polygons to a screen, The user may select polygons by using a selection box drawn as a rectangle by a graphics object. I "kinda" tried clipping and don't think it will solve my performance problem (If I'm not mistaken). If I use clipping it means that I need to redraw all objects within the selection rectangle each time the size of the rectangle changes. This is all well but what if the rectangle covered many thousands of polygons? This would mean the redrawing of thousands of polygons each time I change the size of the selection rectangle. So I tried Bitpmap buffering.

What I do in Bitmap buffering is I create two global Bitmaps. baseBitmap; topBitmap. baseBitmap receives it's image from the SharpMap Map.GetMap() method.  topBitmap is used to draw custom shapes and the selection rectangle. All the code to draw using these Graphics and Bitmap objects is contained within one function, which continuously gets called to redraw all that needs to be redrawn on a mouseEvent e.t.c. topBitmap has a respective Graphics object: topGraphics. Once the map is loaded into the global baseBitmap, when I click mouse down and move simultaneously topGraphics clears topBitmap, Draws  baseBitmap as Unscaled and then draws the selection rectangle (as calculated according to the mouse position). topBitmap is then drawn to the map with the Graphics object created by this.CreateGraphics(). Basically the code:

Graphics topGraphics = Graphics.FromImage(topBitmap)
topgraphics.clear(Color.White)
topGraphics.DrawImageUnscaled(baseBitmap,0,0)
topGraphics.DrawRectangle(....);

Graphics g = this.CreateGraphics();
g.DrawImageUnscaled(topBitmap,0,0);

So you can see how the final Bitmap drawn is the last one (topBitmap). So baseBitmap is drawn to topBitmap and the selection Rectangle drawn to topBitmap. topBitmap is the final result Bitmap, eventually drawn to the map as an unscaled image. This is quite a simple explanation of doing this. Performance wise, everything is not too promising. Things get slightly worse when I have a third Bitmap in the equation. Is this the correct way of doing this?

Maby I'm just not understanding the proper way of doing clipping. But the only way I can see doing this is with Bitmap buffering. Any help on this would be well appreciated.

Thanks
Regards,
Nathanael

Answers (2)