Graphics.GetHalftonePalette 方法

定義

取得目前 Windows 半色調調色盤的句柄。

public static IntPtr GetHalftonePalette();

傳回

IntPtr

指定調色盤句柄的內部指標。

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 定義 Windows DLL 檔案 gdi32.dll的互操作性 DllImportAttribute 屬性,其中包含必要的 GDI 函式。

  • 將該 DLL 中的 SelectPaletteRealizePalette 函式定義為外部。

  • 從現有的圖像檔 SampImag.jpg 建立影像(必須與範例程式代碼檔案位於相同的資料夾中),並將影像繪製到畫面。

  • 建立內部指標類型變數,並將其值分別設定為圖形物件的句柄和目前的 Windows 半色調調色盤。

  • 選取並實現半色調調色盤。

  • 使用 hdc 參數建立新的圖形物件。

  • 再次繪製影像。

  • 釋放裝置內容的句柄。

結果是範例影像的兩個轉譯:一個是16位調色盤,另一個是8位調色盤。

[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);
}

備註

GetHalftonePalette 方法的目的是讓 GDI+ 在顯示器每圖元使用 8 位時產生更好的品質半色調。 若要使用半色調調色盤來顯示影像,請使用下列程式。

適用於

產品 版本
.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