PC Review


Reply
Thread Tools Rate Thread

DrawIcon - sloower and sloooower

 
 
=?Utf-8?B?SmFrdWJT?=
Guest
Posts: n/a
 
      15th Nov 2006
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.
 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      15th Nov 2006
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.



"JakubS" <(E-Mail Removed)> wrote in message
news:EC6CE427-3834-40D7-A4FD-(E-Mail Removed)...
> 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.



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: XP Suddenly Runs Sloooower.. SC Tom Windows XP Performance 0 9th Jan 2010 11:22 PM
DrawIcon API Phoenix Microsoft VB .NET 2 11th May 2007 06:42 AM
DrawIcon API Phoenix Microsoft Excel Programming 1 11th May 2007 06:12 AM
DrawIcon API Phoenix Microsoft Access 1 10th May 2007 05:54 PM
DrawIcon API Phoenix Microsoft VB .NET 3 10th May 2007 04:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:13 PM.