Control.CreateGraphics Method

Creates the Graphics for the control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in

Public Function CreateGraphics As Graphics
Dim instance As Control
Dim returnValue As Graphics

returnValue = instance.CreateGraphics
public Graphics CreateGraphics ()
public function CreateGraphics () : Graphics
Not applicable.

Return Value

The Graphics for the control.

The returned Graphics must be disposed through a call to its Dispose method when it is no longer needed. The Graphics is only valid for the duration of the current window's message.

By design, CreateGraphics sets ownership to the calling thread, and fails if it is called on other threads.


In addition to the InvokeRequired property, there are four methods on a control that are thread safe: Invoke, BeginInvoke, EndInvoke, and CreateGraphics if the handle for the control has already been created. Calling CreateGraphics before the control's handle has been created on a background thread can cause illegal cross thread calls. For all other method calls, you should use one of the invoke methods to marshal the call to the control's thread.

The following code example resizes the specified control so the control will accommodate its formatted text. The formatted text is the Text property with the control's assigned Font applied to the text. The AutoSizeControl method in this example also has a textPadding parameter that represents the padding to apply to all edges of the control. To make the padding appear equal, align the text with the MiddleCenter value of System.Drawing.ContentAlignment if your control supports it.

Private Sub AutoSizeControl(control As Control, textPadding As Integer)
   ' Create a Graphics object for the Control.
   Dim g As Graphics = control.CreateGraphics()
   ' Get the Size needed to accommodate the formatted Text.
   Dim preferredSize As Size = g.MeasureString( _
     control.Text, control.Font).ToSize()
   ' Pad the text and resize the control.
   control.ClientSize = New Size( _
     preferredSize.Width + textPadding * 2, _
     preferredSize.Height + textPadding * 2)
   ' Clean up the Graphics object.
End Sub

private void AutoSizeControl(Control control, int textPadding)
    // Create a Graphics object for the Control.
    Graphics g = control.CreateGraphics();
    // Get the Size needed to accommodate the formatted Text.
    Size preferredSize = g.MeasureString(control.get_Text(), control.
    // Pad the text and resize the control.
    control.set_ClientSize(new Size(preferredSize.get_Width() 
        + textPadding * 2, preferredSize.get_Height() + textPadding * 2));
    // Clean up the Graphics object.
} //AutoSizeControl

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0