Small essay on Graphics. Have your say.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Another way of looking at .net graphics.
The Two Layered graphics concept:- please try to follow this example conceptually:-<br/>
You have a form or panal. You want to express animations and dynamic graphics on this form.<br/>
1)Create a bitmap the same size as the forms clientsize, and set this as the backgroundimage of the form.(Me.BackgroundImage = .....)<br/>
2) Create a graphics object to manipulate this bitmap(use ...... = Graphics.FromImage(....))<br/>
Now this we will call your INTERIOR graphics object.<br/>
3) Create a graphics object to manipulate the surface of your form(use ..... = Me.CreateGraphics)<br/>
Now this we will call your EXTERIOR graphics object.<br/>
Understand that success depends on your correct understanding and juggling of these two different objects which both share and express themselves on the same set of pixels on your form.
Test 1) In some event(button or mouse - not load or paint) - draw something to your INTERIOR. You will notice that nothing appears on the screen? What? Now mouse grab the title bar of your form and drag the body of the form off the screen - then back onto
the screen - and hey presto - there it is!
Test 2) In some event(button or mouse - not load or paint) - draw something to your EXTERIOR. You will notice that there it is - large as life - right there on your screen. Good. Now, mouse grab the title bar of your form and drag the body of the form right
off the screen - then back onto the screen . . .- and hey? the drawing has dissappeared!!
Ok, I realise a lot of folks would like to hasten in now and say "Well, that is why you should always draw in the paint_event so the drawing appears initially and remains.!" But I am asking you to bear with me and stick with the Two Layered concept a little
longer, - because once you understand it - a lot of different options open up to you.
If you are cognizant of what has happened in the above tests, you may well with me draw a few simplistic conclusions:<br/>
1) If you have some permanent static graphics like logos, border lines, labels, background pictures or wallpaper: - draw these to your INTERIOR only in the load event. And thats it - they will always be there and show unless you draw over them.
2) If you need to make a small screen update in real time like say the score board reading or a clock: - draw these to BOTH INTERIOR and EXTERIOR in a localized fashion (no need to refresh the whole screen).
3) If you have to build a complex item like a chart for instance - involving lots of lines and rectangles and text - do all the building work on the INTERIOR - then simply refresh the EXTERIOR by casting the "interior" bitmap onto the EXTERIOR.
4) If you have some "fire and forget" graphics - like when you mouse hover over items they flash, pulsate, breathe, or respond in some other real time way- just draw these to the EXTERIOR - no need to make them permanent by drawing them to the interior.
5) If you have some "transitional" graphics - like say a scroll effect from one position to another - just draw the transitions on the EXTERIOR - then draw the final position on BOTH.


I wrote this for beginners in .net graphics. It took some time for the penny to drop for me, - and there was little written about this in my on board documentation. Naturally the above is just a conceptual example - I am not preaching it as the only and
best way to deal with graphics - as opposed to the me.invalidate/paint event approach.
<
Leon C Stanley - - A dinky di VBer - -<br/>

View the full article
 
Back
Top