Export (0) Print
Expand All

Graphics.GetHalftonePalette Method

Gets a handle to the current Windows halftone palette.

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

public static IntPtr GetHalftonePalette ()
public static IntPtr GetHalftonePalette ()
public static function GetHalftonePalette () : IntPtr
Not applicable.

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.

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

The result is two renderings of the sample image: one with the 16-bit palette and one with the 8-bit palette.

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr SelectPalette(
    IntPtr hdc,
    IntPtr htPalette,
    bool bForceBackground);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern int RealizePalette(IntPtr hdc);

[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDemand, Flags = 
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]            
private 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);
    RealizePalette(hdc);

    // Create new graphics object.
    Graphics newGraphics = Graphics.FromHdc(hdc);

    // Draw image with new palette.
    newGraphics.DrawImage(imageFile, 300, 0);

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft