Share via


方法 : Off-Screen イメージを描画します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

Graphics オブジェクトをいないを使用して、図面の大きなイメージ オフスクリーン イメージを作成するフォームに関連付けられたときに、ちらつき削減できます。 Graphics オブジェクト、フォームを使用して画面のイメージを描画します。

使用例

次の使用例は、OnPaint メソッド off-screen、ビットマップから派生した Graphics オブジェクトを使用して大きなビットマップを作成するをオーバーライドします。 Graphics Graphics プロパティから返された PaintEventArgs オブジェクトを使用して、画面に、ビットマップ、描画します。

を、フォームの読み込み後、イメージに数秒かかります。

                        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 AsNew SolidBrush(Color.Blue)
    Dim WhitePen AsNew 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 AsIntegerFor z = 1 To 1000

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

        ' Create rectangles in the inner area of the form.Dim rect AsNew 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()

EndSub
                        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();
}

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

堅牢性の高いプログラム

オフスクリーン描画用に作成 Graphics オブジェクトを破棄する必要があることに注意ください。 Graphics オブジェクトの Graphics プロパティによって返される PaintEventArgs オブジェクトはガベージ コレクターによって破棄されする明示的に破棄する必要はありません。

参照

その他の技術情報

グラフィックスと、.NET での描画の最適化フレームワーク