ReleaseHdc Method (IntPtr)
Collapse the table of content
Expand the table of content

Graphics.ReleaseHdc Method (IntPtr)

 

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

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

member ReleaseHdc : 
        hdc:nativeint -> unit

Parameters

hdc
Type: System.IntPtr

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

The device context is a Windows structure based on GDI 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 and ReleaseHdc method pair, you usually make calls only to GDI functions. Calls in that scope made to GDI+ methods of the Graphics that produced the hdc parameter fail with an ObjectBusy error. Also, GDI+ ignores any state changes made to the Graphics of the hdc parameter in subsequent operations.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, 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 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.

No code example is currently available or this language may not be supported.

SecurityPermission

for access to unmanaged code. Related enumeration: UnmanagedCode.

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft