Graphics.GetHalftonePalette 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 Windows 半色调调色板的句柄。
public:
 static IntPtr GetHalftonePalette();public static IntPtr GetHalftonePalette();static member GetHalftonePalette : unit -> nativeintPublic Shared Function GetHalftonePalette () As IntPtr返回
nativeint
指定调色板句柄的内部指针。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
- 定义 Windows DLL 文件 gdi32.dll的互操作性 DllImportAttribute 属性,其中包含必要的 GDI 函数。 
- 将该 DLL 中的 - SelectPalette和- RealizePalette函数定义为外部函数。
- 从现有图像文件创建图像 SampImag.jpg(该文件必须与示例代码文件位于同一文件夹中),并将图像绘制到屏幕。 
- 创建内部指针类型变量并将其值分别设置为图形对象的句柄和当前 Windows 半色调调色板。 
- 选择并实现半色调调色板。 
- 使用 - hdc参数创建新的图形对象。
- 再次绘制图像。 
- 释放设备上下文的句柄。 
结果是示例图像的两个呈现:一个具有 16 位调色板,一个呈现 8 位调色板。
private:
   [System::Runtime::InteropServices::DllImportAttribute("gdi32.dll")]
   static IntPtr SelectPalette( IntPtr hdc, IntPtr htPalette, bool bForceBackground );
   [System::Runtime::InteropServices::DllImportAttribute("gdi32.dll")]
   static int RealizePalette( IntPtr hdc );
public:
   void GetHalftonePaletteVoid( PaintEventArgs^ e )
   {
      // Create and draw image.
      Image^ imageFile = Image::FromFile( "SampImag.jpg" );
      e->Graphics->DrawImage( imageFile, 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 );
   }
[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);
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);
}
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
Private Shared Function SelectPalette(ByVal hdc As IntPtr, _
ByVal htPalette As IntPtr, ByVal bForceBackground As Boolean) As IntPtr
End Function
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
Private Shared Function RealizePalette(ByVal hdc As IntPtr) As Integer
End Function
<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub GetHalftonePaletteVoid(ByVal 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)
    RealizePalette(hdc)
    ' 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.
    e.Graphics.ReleaseHdc(hdc)
End Sub
注解
GetHalftonePalette 方法的目的是使 GDI+ 在显示器使用每像素 8 位时产生更好的质量半色调。 若要使用半色调调色板显示图像,请使用以下过程。