Skip to content

Commit 6c3eb88

Browse files
authored
Merge pull request dotnet#1 from vbjay/patch-1
Cache Font object
2 parents 67ebd71 + cd53499 commit 6c3eb88

File tree

1 file changed

+7
-1
lines changed
  • samples/snippets/csharp/VS_Snippets_Winforms/Control.Paint/CS

1 file changed

+7
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
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);

0 commit comments

Comments
 (0)