DrawIcon - sloower and sloooower

G

Guest

Recently I've been investigating performance problems with painting in one of
my controls. I've ended up with a trivial sample which illustrates strange
behavior of Graphics.DrawIcon(...).

I made a simple form and tried to draw an icon multiple times:

private void button1_Click(object sender, EventArgs e)
{
Icon icon = Resource1.MyIcon;
Rectangle rect = new Rectangle(10, 10, 16, 16);

using (Graphics g = this.CreateGraphics())
{
for (int loop = 0; loop < 10; loop++)
{
DateTime dt1 = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
g.DrawIcon(icon, rect);
}
DateTime dt2 = DateTime.Now;
TimeSpan elapsed = dt2.Subtract(dt1);
Trace.WriteLine(elapsed.TotalMilliseconds.ToString());
}
}
}

So, I draw the icon 10 000 times, measuring the time needed for each
thousand. Here is what I typically get from Trace:

578,0584
874,8992
999,8848
1171,74
1390,4648
1515,4504
1624,8128
1859,1608
2015,3928
2109,132

It seems that each new thousand of DrawIcon() operations is done noticeably
slower! If I repeat it more times, I get even worse results...
Sounds like a bug to me (maybe poor caching of something, so each time a
lookup over a bigger set of things is performed). I've also noticed that if a
make a long-running loop like this, the consumed memory slowly increases.

Any explanations?
JakubS

PS: Currently I worked around the problem by doing icon.ToBitmap() and using
DrawImage(...) instead - it is much faster.
 
B

Bob Powell [MVP]

This could be an unmanaged resource problem. I think icons have a leak
somewhere.

Anyway, if your having problems it serves you right for drawing in a
CreateGraphics call ;-).

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top