Using perfmon to analyze the performance and code of GDI +

Source: Internet
Author: User

How many times have the paint event been triggered? In a simple way, we can create a counter that can be used by perfmon. ViewCode:

1 Private   Void Button#click ( Object Sender, eventargs E)
2 {
3
4 If ( ! Performancecountercategory. exists ( " GDI + Monitor " ))
5 {
6 Countercreationdatacollection CCDC =   New Countercreationdatacollection ();
7
8 Countercreationdata totalpaint =   New Countercreationdata ();
9 Totalpaint. countername =   " # Operator executed " ;
10 Totalpaint. counterhelp =   " Counts of onpaint events called " ;
11 Totalpaint. countertype = Performancecountertype. numberofitems32;
12
13 CCDC. Add (totalpaint );
14
15 Performancecountercategory. Create ( " GDI + Monitor " , " Some counters for GDI + objects " , Performancecountercategorytype. multiinstance, CCDC );
16 }
17 }

Okay, click button1 and we can see the category of GDI + Monitor in perfmon. Then there is only one counter, that is, # Operator executed.

Then add the following section to winform: Paintcall =   New Performancecounter ();
Paintcall. categoryname =   " GDI + Monitor " ;
Paintcall. countername =   " # Operator executed " ;
Paintcall. machinename =   " . " ;
Paintcall. readonly =   False ;

Okay, you can use the paintcall variable in the code. Our current requirement is to monitor how many times the paint is triggered, so you can write it in the Code: 1 Graphics G2 = E. graphics;
2
3 Bitmap BMP =   New Bitmap ( This . Width, This . Height );
4 Graphics g = Graphics. fromimage (BMP );
5
6 Paintcall. increment ();
7
8 Rectangle R =   New Rectangle ( 0 , 0 , This . Width, This . Height );
9 G. fillrectangle ( New Lineargradientbrush (R, color. Red, color. Blue, lineargradientmode. backwarddiagonal), R );
10
11 G2.drawimage (BMP, New Point ( 0 , 0 ));
12
13 G. Dispose ();
14 G =   Null ;
15
16 BMP. Dispose ();
17 BMP =   Null ;

Look at the above 6th rows. This sentence will add a paint call. Of course, there are many other methods. I will not write them here.
Run perfmon and add the newly added counter. Well, we can see that when the window is invalid, the counter is increased.

There is a small detail. When the window size is slightly changed, you will find that the paint has been called many times, which is really depressing. So, do something: 1 Private   Bool Resize =   False ;
2 Private   Void Form1_resizeend ( Object Sender, eventargs E)
3 {
4Resize= False;
5This. Invalidate ();
6}
7
8 Private   Void Form1_resizebegin ( Object Sender, eventargs E)
9 {
10Resize= True;
11}

Then let's modify the code of the paint event as follows: 1 Private   Void Form1_paint ( Object Sender, painteventargs E)
2 {
3 If ( True   = Resize) Return ;
4
5 Graphics G2 = E. graphics;
6
7 Bitmap BMP =   New Bitmap ( This . Width, This . Height );
8 Graphics g = Graphics. fromimage (BMP );
9
10 Paintcall. increment ();
11
12 Rectangle R =   New Rectangle ( 0 , 0 , This . Width, This . Height );
13 G. fillrectangle ( New Lineargradientbrush (R, color. Red, color. Blue, lineargradientmode. backwarddiagonal), R );
14
15 G2.drawimage (BMP, New Point ( 0 , 0 ));
16
17 G. Dispose ();
18 G =   Null ;
19
20 BMP. Dispose ();
21 BMP =   Null ;
22 }

Note that in the third row, we determine that if the resize process is used, the system returns the result directly. In this way, although the interface is like a whiteboard, but a lot of painting operations are missing, the performance will be much better.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.