Bitmap.GetHbitmap Method

Definition

Creates a GDI bitmap object from a GDI+ Bitmap.

Overloads

GetHbitmap()

Creates a GDI bitmap object from this Bitmap.

GetHbitmap(Color)

Creates a GDI bitmap object from this Bitmap.

GetHbitmap()

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Creates a GDI bitmap object from this Bitmap.

C#
public IntPtr GetHbitmap();

Returns

IntPtr

A handle to the GDI bitmap object that this method creates.

Exceptions

The height or width of the bitmap is greater than Int16.MaxValue.

The operation failed.

Examples

The following code example demonstrates how to use the GetHbitmap method.

C#
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

private void DemonstrateGetHbitmap()
{
    Bitmap bm = new Bitmap("Picture.jpg");
    IntPtr hBitmap = bm.GetHbitmap();

    // Do something with hBitmap.
    DeleteObject(hBitmap);
}

Remarks

You are responsible for calling the GDI DeleteObject method to free the memory used by the GDI bitmap object. For more information about GDI bitmaps, see Bitmaps in the Windows GDI documentation.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

GetHbitmap(Color)

Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs
Source:
Bitmap.cs

Creates a GDI bitmap object from this Bitmap.

C#
public IntPtr GetHbitmap(System.Drawing.Color background);

Parameters

background
Color

A Color structure that specifies the background color. This parameter is ignored if the bitmap is totally opaque.

Returns

IntPtr

A handle to the GDI bitmap object that this method creates.

Exceptions

The height or width of the bitmap is greater than Int16.MaxValue.

The operation failed.

Examples

The following code example demonstrates how to use the GetHbitmap.method.

C#

private void DemonstrateGetHbitmapWithColor()
{
    Bitmap bm = new Bitmap("Picture.jpg");
    IntPtr hBitmap = bm.GetHbitmap(Color.Blue);

    // Do something with hBitmap.
    DeleteObject(hBitmap);
}

Remarks

You are responsible for calling the GDI DeleteObject method to free the memory used by the GDI bitmap object. For more information about GDI bitmaps, see Bitmaps in the Windows GDI documentation.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10