This documentation is archived and is not being maintained.

Graphics.GetHalftonePalette Method

Gets a handle to the current Windows halftone palette.

[Visual Basic]
Public Shared Function GetHalftonePalette() As IntPtr
public static IntPtr GetHalftonePalette();
public: static IntPtr GetHalftonePalette();
public static function GetHalftonePalette() : IntPtr;

Return Value

Internal pointer that specifies the handle to the palette.


The purpose of the GetHalftonePalette method is to enable GDI+ to produce a better quality halftone when the display uses 8 bits per pixel. To display an image using the halftone palette, use the following procedure.


[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 actions:

  • Defines interoperability DllImportAttribute attributes for the Windows DLL file gdi32.dll, which contains the necessary GDI functions.
  • Defines the SelectPalette and RealizePalette functions in that DLL as external.
  • Creates an image from an existing image file SampImag.jpg (which must be in the same folder as the example code file) and draws the image to the screen.
  • Creates internal pointer type variables and sets their values to the handle to the e . Graphics object and to the current Windows halftone palette, respectively.
  • Selects and realizes the halftone palette.
  • Creates a new graphics object using the hdc parameter.
  • Draws the image again.
  • Releases the handle to the device context.

[Visual Basic, C#] The result is two renderings of the sample image: one with the 16-bit palette and one with the 8-bit palette.

[Visual Basic] 
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")>  _
Private Shared Function SelectPalette(hdc As IntPtr, _
htPalette As IntPtr, bForceBackground As Boolean) As IntPtr
End Function
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")>  _
Private Shared Function RealizePalette(hdc As IntPtr) As Integer
End Function
Public Sub GetHalftonePaletteVoid(e As PaintEventArgs)
' Create and draw image.
Dim imageFile As Image = Image.FromFile("SampImag.jpg")
e.Graphics.DrawImage(imageFile, New Point(0, 0))
' Get handle to device context.
Dim hdc As IntPtr = e.Graphics.GetHdc()
' Get handle to halftone palette.
Dim htPalette As IntPtr = Graphics.GetHalftonePalette()
' Select and realize new palette.
SelectPalette(hdc, htPalette, True)
' Create new graphics object.
Dim newGraphics As Graphics = Graphics.FromHdc(hdc)
' Draw image with new palette.
newGraphics.DrawImage(imageFile, 300, 0)
' Release handle to device context.
End Sub
private static extern IntPtr SelectPalette(
IntPtr hdc,
IntPtr htPalette,
bool bForceBackground);
private static extern int RealizePalette(IntPtr hdc);
public void GetHalftonePaletteVoid(PaintEventArgs e)
// Create and draw image.
Image imageFile = Image.FromFile("SampImag.jpg");
e.Graphics.DrawImage(imageFile, new Point(0, 0));
// Get handle to device context.
IntPtr hdc = e.Graphics.GetHdc();
// Get handle to halftone palette.
IntPtr htPalette = Graphics.GetHalftonePalette();
// Select and realize new palette.
SelectPalette(hdc, htPalette, true);
// Create new graphics object.
Graphics newGraphics = Graphics.FromHdc(hdc);
// Draw image with new palette.
newGraphics.DrawImage(imageFile, 300, 0);
// Release handle to device context.

[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.


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