Graphics.GetHalftonePalette メソッド

定義

現在の Windows ハーフトーン パレットを識別するハンドルを取得します。

public:
 static IntPtr GetHalftonePalette();
public static IntPtr GetHalftonePalette ();
static member GetHalftonePalette : unit -> nativeint
Public Shared Function GetHalftonePalette () As IntPtr

戻り値

IntPtr

nativeint

パレットを識別するハンドルを指定する内部ポインター。

次のコード例は、Windows フォームで使用するように設計されており、イベント ハンドラーのPaintパラメーターである が必要PaintEventArgseです。 コードは、次のアクションを実行します。

  • 必要な GDI 関数を含む Windows DLL ファイル gdi32.dllの相互運用性 DllImportAttribute 属性を定義します。

  • その DLL 内の SelectPalette および RealizePalette 関数を外部として定義します。

  • 既存のイメージ ファイル SampImag.jpg (コード ファイルの例と同じフォルダーにある必要があります) からイメージを作成し、画面にイメージを描画します。

  • 内部ポインター型変数を作成し、その値をそれぞれグラフィックス オブジェクトと現在の Windows ハーフトーン パレットのハンドルに設定します。

  • ハーフトーンパレットを選択して実現します。

  • パラメーターを使用して新しいグラフィックス オブジェクトを hdc 作成します。

  • イメージをもう一度描画します。

  • ハンドルをデバイス コンテキストに解放します。

結果は、サンプル イメージの 2 つのレンダリングです。1 つは 16 ビット パレット、もう 1 つは 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 目的は、ディスプレイで 1 ピクセルあたり 8 ビットを使用する場合に、GDI+ が高品質のハーフトーンを生成できるようにすることです。 ハーフトーン パレットを使用して画像を表示するには、次の手順に従います。

適用対象