This documentation is archived and is not being maintained.

Graphics.ReleaseHdc Method

Releases a device context handle obtained by a previous call to the GetHdc method of this Graphics object.

[Visual Basic]
Public Sub ReleaseHdc( _
   ByVal hdc As IntPtr _
)
[C#]
public void ReleaseHdc(
 IntPtr hdc
);
[C++]
public: void ReleaseHdc(
 IntPtr hdc
);
[JScript]
public function ReleaseHdc(
   hdc : IntPtr
);

Parameters

hdc
Handle to a device context obtained by a previous call to the GetHdc method of this Graphics object.

Return Value

This method does not return a value.

Remarks

The device context is a Windows GDI-based structure that defines a set of graphical objects and their associated attributes, as well as the graphical modes that affect output.

Calls to the GetHdc and ReleaseHdc methods must appear in pairs. During the scope of a GetHdc- ReleaseHdc method pair, you usually make only calls to GDI functions. Calls in that scope made to GDI+ methods of the Graphics object that produced the hdc parameter fail with an ObjectBusy error. Also, GDI+ ignores any state changes made to the Graphics object of the hdc parameter in subsequent operations.

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 example illustrates calling a Windows GDI function to perform the same task as a GDI+ Graphics object method. The code performs the following actions:

  • Defines the interoperability DllImportAttribute attribute for the Windows DLL file gdi32.dll. This DLL contains the desired GDI function, and it defines the Rectangle function in that DLL as external.
  • Creates a red pen.
  • With the pen, draws a rectangle to the screen using the GDI+ DrawRectangle method.
  • Defines an internal pointer type variable hdc and sets its value to the handle to the device context of the form.
  • Draws a rectangle to the screen using the GDI Rectangle function.
  • Releases the device context represented by the hdc parameter.
[Visual Basic] 
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")>  _
Private Shared Function Rectangle(hdc As IntPtr, _
ulCornerX As Integer, ulCornerY As Integer, lrCornerX As Integer, _
lrCornerY As Integer) As Boolean
End Function
Public Sub GetHdcForGDI(e As PaintEventArgs)
' Create pen.
Dim redPen As New Pen(Color.Red, 1)
' Draw rectangle with GDI+.
e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50)
' Get handle to device context.
Dim hdc As IntPtr = e.Graphics.GetHdc()
' Draw rectangle with GDI using default pen.
Rectangle(hdc, 10, 70, 110, 120)
' Release handle to device context.
e.Graphics.ReleaseHdc(hdc)
End Sub
        
[C#] 
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool Rectangle(
IntPtr hdc,
int ulCornerX, int ulCornerY,
int lrCornerX, int lrCornerY);
public void GetHdcForGDI(PaintEventArgs e)
{
// Create pen.
Pen redPen = new Pen(Color.Red, 1);
// Draw rectangle with GDI+.
e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50);
// Get handle to device context.
IntPtr hdc = new IntPtr();
hdc = e.Graphics.GetHdc();
// Draw rectangle with GDI using default pen.
Rectangle(hdc, 10, 70, 110, 120);
// 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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Graphics Class | Graphics Members | System.Drawing Namespace

Show: