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
Loading
christian de wetPosted Aug 18, 2007, 6:21 AM
Thanks again for your help. I'll check that BitBlt stuff out. It's sounds pretty cool. All I need is something that can draw 2D graphics pretty fast. So this does look promising. I really appreciate sharing your knowledge about this topic. If there's one thing I've learnt about software development is that knowledge is power and a huge timesaver!
Thank you!
Regards,
Nathanael
Richard BlythePosted Aug 18, 2007, 1:03 AM
Hey man, no replies yet?
I understand your concern of drawing "thousands of polygons" and the performance hit the occurs. There is one other thing you might try regarding GDI+. Actually it is specific to the old GDI. It's called: BitBlt. "Bitmap Block Transfer" (often pronounced: Bit Blit) As it's name implies, it transfers a bitmap from one surface to another using the graphics card hardware. The benefit to you is that you can copy the pixels ALREADY drawn to the form, render your dragging polygon in that space, then re-instate the copied pixels back when the polygon is moved. The only drawbacks to using this is you have to enter the dark world of API. I would write this all out for you but this subject is pretty complex. Do a google search on: BitBlt SRCCOPY
This will certainly save you the time of learning DirectX.
Cheers!
Richard
(I will be away for a week so I wish you the best in the mean time)