Graphics.GetHalftonePalette Metodo

Definizione

Ottiene un handle per la tavolozza dei mezzitoni di Windows corrente.

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

Restituisce

IntPtr

nativeint

Puntatore interno che specifica l'handle per la tavolozza.

Esempio

L'esempio di codice seguente è progettato per l'uso con Windows Forms e richiede PaintEventArgse, che è un parametro del Paint gestore eventi. Il codice esegue le azioni seguenti:

  • Definisce gli attributi di interoperabilità DllImportAttribute per il file DLL di Windows gdi32.dll, che contiene le funzioni GDI necessarie.

  • Definisce le SelectPalette funzioni e RealizePalette in tale DLL come esterne.

  • Crea un'immagine da un file di immagine esistente SampImag.jpg (che deve trovarsi nella stessa cartella del file di codice di esempio) e disegna l'immagine sullo schermo.

  • Crea variabili di tipo puntatore interno e imposta i relativi valori sull'handle per l'oggetto grafico e sulla tavolozza mezzatona di Windows corrente, rispettivamente.

  • Seleziona e realizza la tavolozza halftone.

  • Crea un nuovo oggetto grafico usando il hdc parametro .

  • Disegna di nuovo l'immagine.

  • Rilascia l'handle al contesto di dispositivo.

Il risultato è costituito da due rendering dell'immagine di esempio: uno con la tavolozza a 16 bit e uno con la tavolozza a 8 bit.

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

Commenti

Lo scopo del GetHalftonePalette metodo è consentire a GDI+ di produrre una mezzatone di qualità migliore quando lo schermo usa 8 bit per pixel. Per visualizzare un'immagine usando la tavolozza halftone, seguire questa procedura.

Si applica a