Share via


Comment : dessiner des images hors écran

Mise à jour : novembre 2007

Vous pouvez réduire le scintillement lorsque vous dessinez de grandes images en utilisant un objet Graphics non associé au formulaire pour créer l'image hors écran. Dessinez ensuite l'image sur l'écran à l'aide d'un objet Graphics du formulaire.

Exemple

Cet exemple substitue la méthode OnPaint pour créer une grande bitmap hors écran à l'aide d'un objet Graphics dérivé de la bitmap. Il dessine ensuite la bitmap à l'écran à l'aide de l'objet Graphics retourné par la propriété Graphics du PaintEventArgs.

Une fois le formulaire chargé, quelques secondes peuvent s'écouler avant que l'image ne s'affiche.

Protected Overrides Sub OnPaint(e As PaintEventArgs)

    Dim bmp As Bitmap
    Dim gOff As Graphics

    ' Create a bitmap the size of the form.
    bmp = New Bitmap(ClientRectangle.Width, ClientRectangle.Height)

    Dim BlueBrush As New SolidBrush(Color.Blue)
    Dim WhitePen As New Pen(Color.White, 3)

    ' Create a Graphics object that is not on the form.
    gOff = Graphics.FromImage(bmp)
    gOff.FillRectangle(new SolidBrush(color.red), 0, 0, _
        bmp.Width, bmp.Height)

    ' Draw a complex bitmap of 1000 random rectangles. It will take a few
    ' seconds to draw.
    Dim z As Integer
    For z = 1 To 1000

        ' Generate a random number with
        ' seeds from the system clock.
        Thread.Sleep(1)
        Dim rx As New Random()
        Thread.Sleep(1)
        Dim ry As New Random()

        ' Create rectangles in the inner area of the form.
        Dim rect As New Rectangle(rx.Next(10,200), ry.Next(10,200), 10, 10)
        gOff.DrawRectangle(WhitePen, rect)
        gOff.FillRectangle(BlueBrush, rect)
    Next z

    ' Use the Graphics object from 
    ' PaintEventArgs to draw the bitmap onto the screen.
    e.Graphics.DrawImage(bmp, 0, 0, ClientRectangle, GraphicsUnit.Pixel)

    gOff.Dispose()

End Sub
protected override void OnPaint(PaintEventArgs e)
{

    Bitmap bmp;
    Graphics gOff;

    // Create a bitmap the size of the form.
    bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);

    SolidBrush BlueBrush = new SolidBrush(Color.Blue);
    Pen WhitePen = new Pen(Color.White,3);

    // Create a Graphics object that is not on the form.
    gOff = Graphics.FromImage(bmp);
    gOff.FillRectangle(new SolidBrush(Color.Red), 0, 0, 
        bmp.Width, bmp.Height);

    // Draw a complex bitmap of 1000 random rectangles. It will take a few
    // seconds to draw.
    for (int z = 1; z <= 1000; z++)
    {
        // Generate a random number with
        // seeds from the system clock.
        Thread.Sleep(1);
        Random rx = new Random();
        Thread.Sleep(1);
        Random ry = new Random();

        // Create rectangles in the inner area of the form.
        Rectangle rect = new Rectangle(rx.Next(10,200), ry.Next(10,200),
            10, 10);
        gOff.DrawRectangle(WhitePen, rect);
        gOff.FillRectangle(BlueBrush, rect);
    }

    // Use the Graphics object from 
    // PaintEventArgs to draw the bitmap onto the screen.
    e.Graphics.DrawImage(bmp, 0, 0, ClientRectangle, GraphicsUnit.Pixel);
    gOff.Dispose();
}

Compilation du code

Cet exemple nécessite des références aux espaces de noms suivants :

Programmation fiable

Notez que l'objet Graphics créé pour le dessin hors écran doit être supprimé. L'objet Graphics retourné par la propriété Graphics de l'objet PaintEventArgs est détruit par le garbage collector. Il est donc inutile de le supprimer explicitement.

Voir aussi

Autres ressources

Graphiques et dessins dans le .NET Compact Framework