Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GDI+ and Speed 2

Status
Not open for further replies.

JurkMonkey

Programmer
Nov 23, 2004
1,731
CA
When i deal with drawing in GDI+, I find i create a lot of variables during each paint. I'm constantly creating new brushes, rectangles, and pens. I try to keep some of those pens as variables in the class but it's not always possible.

Drawing sometimes becomes slow because of this.

Does anyone have any suggestions when it comes to GDI+?
 
I dont create many brushes, but when I do, I use the static system brushes as much as possible.

I mostly render images with the draw/fill polygon methods, and I have had almost no delay in drawing images with 30 or so polygons, with as many as 300 points each.

Do you render/refresh your image in a mousemove or similar event?

I allow users the "drag" certain objects across the image, and the image has to be totally redrawn in the mouse move event. At first it was very choppy, especially when I had a lot of points and objects to draw. So i added a DateTime to my form called "LastRender", and when the mouse event fired, I only redrew the image if LastRender was more than 3 milliseconds ago. The dragging is now VERY smooth. You can adjust the milliseconds depending on the system/application, but I found 3-5 ms to be best for me.

The best thing to do would be to time how long it takes to render your image, in milliseconds, and use that as a base to prevent renderings from piling up on eachother.
 
MouseMove has always been a pain. I like your solution to that!

When you say static brushes - where do I find those?
 
say for example I am drawing a plain black polygon, I would do this: (assuming I have a graphics object called g)

g.FillPolygon(Brushes.Black,myPointArray);

the Brushes.Black does not have to be instantiated, it's just there, I dont ask it where it came from, It just works ;)

There are 879 Static Brushes (all just solid colors, as far as I know) in the Brushes class
 
To clarify, its System.Drawing.Brushes

Theres also System.Drawing.Pens, which are static pens available with the same 879 colors.

Upon further review I just noticed theres also System.Drawing.SystemBrushes, and System.Drawing.SystemPens, each with a collection of static brushes and pens. Could come in handy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top