Condividi tramite


Procedura: Eseguire il rendering di immagini con GDI+

È possibile usare GDI+ per eseguire il rendering di immagini esistenti come file nelle applicazioni. A tale scopo, creare un nuovo oggetto di una classe Image (ad esempio Bitmap), creando un oggetto Graphics che fa riferimento alla superficie di disegno che si desidera utilizzare e chiamando il metodo DrawImage dell'oggetto Graphics. L'immagine verrà dipinta sulla superficie di disegno rappresentata dalla classe grafica. È possibile usare l'editor di immagini per creare e modificare i file di immagine in fase di progettazione ed eseguirne il rendering con GDI+ in fase di esecuzione. Per altre informazioni, vedere Editor di immagini per le icone.

Per eseguire il rendering di un'immagine con GDI+

  1. Creare un oggetto che rappresenta l'immagine da visualizzare. Questo oggetto deve essere un membro di una classe che eredita da Image, ad esempio Bitmap o Metafile. Viene illustrato un esempio:

    ' Uses the System.Environment.GetFolderPath to get the path to the
    ' current user's MyPictures folder.
    Dim myBitmap as New Bitmap _
       (System.Environment.GetFolderPath _
          (System.Environment.SpecialFolder.MyPictures))
    
    // Uses the System.Environment.GetFolderPath to get the path to the
    // current user's MyPictures folder.
    Bitmap myBitmap = new Bitmap
       (System.Environment.GetFolderPath
          (System.Environment.SpecialFolder.MyPictures));
    
    // Uses the System.Environment.GetFolderPath to get the path to the
    // current user's MyPictures folder.
    Bitmap^ myBitmap = gcnew Bitmap
       (System::Environment::GetFolderPath
          (System::Environment::SpecialFolder::MyPictures));
    
  2. Creare un oggetto Graphics che rappresenta la superficie di disegno che si desidera utilizzare. Per ulteriori informazioni, consultare Come creare oggetti grafici per il disegno.

    ' Creates a Graphics object that represents the drawing surface of
    ' Button1.
    Dim g as Graphics = Button1.CreateGraphics
    
    // Creates a Graphics object that represents the drawing surface of
    // Button1.
    Graphics g = Button1.CreateGraphics();
    
    // Creates a Graphics object that represents the drawing surface of
    // Button1.
    Graphics^ g = button1->CreateGraphics();
    
  3. Chiama il DrawImage del tuo oggetto grafico per renderizzare l'immagine. È necessario specificare sia l'immagine da disegnare che le coordinate in cui deve essere disegnata.

    g.DrawImage(myBitmap, 1, 1)
    
    g.DrawImage(myBitmap, 1, 1);
    
    g->DrawImage(myBitmap, 1, 1);
    

Vedere anche