Skip to content

Commit ec57b79

Browse files
author
Ron Petrusha
authored
Merge pull request dotnet#2111 from vbjay/patch-2
Cache Font
2 parents a652a58 + 6c3eb88 commit ec57b79

File tree

2 files changed

+13
-3
lines changed
  • samples/snippets
    • csharp/VS_Snippets_Winforms/Control.Paint/CS
    • visualbasic/VS_Snippets_Winforms/Control.Paint/VB

2 files changed

+13
-3
lines changed

samples/snippets/csharp/VS_Snippets_Winforms/Control.Paint/CS/form1.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ protected override void Dispose( bool disposing )
3232
{
3333
if( disposing )
3434
{
35+
if (fnt != null)
36+
{
37+
fnt.Dispose();
38+
}
3539
if (components != null)
3640
{
3741
components.Dispose();
@@ -73,6 +77,8 @@ static void Main()
7377
// connected to the Load event of the form.
7478

7579
private PictureBox pictureBox1 = new PictureBox();
80+
// Cache font instead of recreating font objects each time we paint.
81+
private Font fnt = new Font("Arial",10);
7682
private void Form1_Load(object sender, System.EventArgs e)
7783
{
7884
// Dock the PictureBox to the form and set its background to white.
@@ -92,7 +98,7 @@ private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArg
9298

9399
// Draw a string on the PictureBox.
94100
g.DrawString("This is a diagonal line drawn on the control",
95-
new Font("Arial",10), System.Drawing.Brushes.Blue, new Point(30,30));
101+
fnt, System.Drawing.Brushes.Blue, new Point(30,30));
96102
// Draw a line in the PictureBox.
97103
g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, pictureBox1.Top,
98104
pictureBox1.Right, pictureBox1.Bottom);

samples/snippets/visualbasic/VS_Snippets_Winforms/Control.Paint/VB/form1.vb

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ Public Class Form1
3131
'/ </summary>
3232
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
3333
If disposing Then
34-
If (components IsNot Nothing) Then
34+
If fnt IsNot Nothing Then
35+
fnt.Dispose()
36+
End If
37+
If components IsNot Nothing Then
3538
components.Dispose()
3639
End If
3740
End If
@@ -65,6 +68,7 @@ Public Class Form1
6568
' This example assumes that the Form_Load event handler method is connected
6669
' to the Load event of the form.
6770
Private pictureBox1 As New PictureBox()
71+
Private fnt as New Font("Arial", 10)
6872

6973
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
7074
' Dock the PictureBox to the form and set its background to white.
@@ -84,7 +88,7 @@ Public Class Form1
8488

8589
' Draw a string on the PictureBox.
8690
g.DrawString("This is a diagonal line drawn on the control", _
87-
New Font("Arial", 10), Brushes.Red, New PointF(30.0F, 30.0F))
91+
fnt, Brushes.Red, New PointF(30.0F, 30.0F))
8892
' Draw a line in the PictureBox.
8993
g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, _
9094
pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)

0 commit comments

Comments
 (0)