Graphics.ReleaseHdc Method

Definition

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

Overloads

ReleaseHdc()

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

ReleaseHdc(IntPtr)

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

ReleaseHdc()

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

C#
public void ReleaseHdc();

Implements

Remarks

GetHdc and ReleaseHdc are two methods that allow you to get and release the handle for a Windows device. You should always follow a call to GetHdc with a call to ReleaseHdc when you are finished with the Windows handle.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

ReleaseHdc(IntPtr)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

C#
public void ReleaseHdc(IntPtr hdc);

Parameters

hdc
IntPtr

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

Examples

The following code 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 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.

C#
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool Rectangle2(
    IntPtr hdc,
    int ulCornerX, int ulCornerY,
    int lrCornerX, int lrCornerY);

private void GetHdcForGDI2(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 = e.Graphics.GetHdc();

    // Draw rectangle with GDI using default pen.
    Rectangle2(hdc, 10, 70, 110, 120);

    // Release handle to device context.
    e.Graphics.ReleaseHdc(hdc);
}

Remarks

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.

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9