Export (0) Print
Expand All

Graphics.FromHdc Method

Creates a new Graphics object from the specified handle to a device context.

Overload List

Creates a new Graphics object from the specified handle to a device context.

[Visual Basic] Overloads Public Shared Function FromHdc(IntPtr) As Graphics
[C#] public static Graphics FromHdc(IntPtr);
[C++] public: static Graphics* FromHdc(IntPtr);
[JScript] public static function FromHdc(IntPtr) : Graphics;

Creates a new Graphics object from the specified handle to a device context and handle to a device.

[Visual Basic] Overloads Public Shared Function FromHdc(IntPtr, IntPtr) As Graphics
[C#] public static Graphics FromHdc(IntPtr, IntPtr);
[C++] public: static Graphics* FromHdc(IntPtr, IntPtr);
[JScript] public static function FromHdc(IntPtr, IntPtr) : Graphics;

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following action:

  • Creates an internal pointer type variable hdc and sets it to the handle to the device context of the graphics object of the form.
  • Creates a new graphics object using hdc.
  • Draws a rectangle with the new graphics object (on the screen).
  • Releases the new graphics object using hdc.
[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of FromHdc. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub FromHdcHdc(e As PaintEventArgs)
' Get handle to device context.
Dim hdc As IntPtr = e.Graphics.GetHdc()
' Create new graphics object using handle to device context.
Dim newGraphics As Graphics = Graphics.FromHdc(hdc)
' Draw rectangle to screen.
newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)
' Release handle to device context.
e.Graphics.ReleaseHdc(hdc)
End Sub
        
[C#] 
public void FromHdcHdc(PaintEventArgs e)
{
// Get handle to device context.
IntPtr hdc = e.Graphics.GetHdc();
// Create new graphics object using handle to device context.
Graphics newGraphics = Graphics.FromHdc(hdc);
// Draw rectangle to screen.
newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);
// Release handle to device context.
e.Graphics.ReleaseHdc(hdc);
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Graphics Class | Graphics Members | System.Drawing Namespace

Show:
© 2015 Microsoft