Baca dalam bahasa Inggris

Bagikan melalui


BufferedGraphics Kelas

Definisi

Menyediakan buffer grafis untuk buffering ganda.

public sealed class BufferedGraphics : IDisposable
Warisan
BufferedGraphics
Penerapan

Contoh

Contoh kode berikut menunjukkan penggunaan BufferedGraphics objek untuk menggambar grafik menggunakan beberapa jenis implementasi buffering. Mengklik formulir secara bergantian memulai dan menghentikan timer yang menyebabkan menggambar pembaruan. Pembaruan gambar memungkinkan Anda mengamati efek buffering ganda. Mengklik kanan siklus formulir melalui mode menggambar berikut:

Di setiap mode, teks digambar yang mengidentifikasi mode saat ini dan menjelaskan perilaku yang terjadi ketika setiap tombol mouse ditekan.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace BufferingExample
{
    public class BufferingExample : Form
    {
        private BufferedGraphicsContext context;
        private BufferedGraphics grafx;

    private byte bufferingMode;
    private string[] bufferingModeStrings =
        { "Draw to Form without OptimizedDoubleBufferring control style",
          "Draw to Form using OptimizedDoubleBuffering control style",
          "Draw to HDC for form" };

    private System.Windows.Forms.Timer timer1;
    private byte count;

        public BufferingExample() : base()
        {
            // Configure the Form for this example.
            this.Text = "User double buffering";
            this.MouseDown += new MouseEventHandler(this.MouseDownHandler);
            this.Resize += new EventHandler(this.OnResize);
            this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true );

            // Configure a timer to draw graphics updates.
        timer1 = new System.Windows.Forms.Timer();
        timer1.Interval = 200;
        timer1.Tick += new EventHandler(this.OnTimer);

        bufferingMode = 2;
        count = 0;

            // Retrieves the BufferedGraphicsContext for the
            // current application domain.
            context = BufferedGraphicsManager.Current;

            // Sets the maximum size for the primary graphics buffer
            // of the buffered graphics context for the application
            // domain.  Any allocation requests for a buffer larger
            // than this will create a temporary buffered graphics
            // context to host the graphics buffer.
            context.MaximumBuffer = new Size(this.Width+1, this.Height+1);

            // Allocates a graphics buffer the size of this form
            // using the pixel format of the Graphics created by
            // the Form.CreateGraphics() method, which returns a
            // Graphics object that matches the pixel format of the form.
            grafx = context.Allocate(this.CreateGraphics(),
                 new Rectangle( 0, 0, this.Width, this.Height ));

        // Draw the first frame to the buffer.
        DrawToBuffer(grafx.Graphics);
        }

    private void MouseDownHandler(object sender, MouseEventArgs e)
        {
        if( e.Button == MouseButtons.Right )
        {
                 // Cycle the buffering mode.
             if( ++bufferingMode > 2 )
                     bufferingMode = 0;

                 // If the previous buffering mode used
                 // the OptimizedDoubleBuffering ControlStyle,
                 // disable the control style.
                 if( bufferingMode == 1 )
                     this.SetStyle( ControlStyles.OptimizedDoubleBuffer, true );

                 // If the current buffering mode uses
                 // the OptimizedDoubleBuffering ControlStyle,
                 // enabke the control style.
                 if( bufferingMode == 2 )
                     this.SetStyle( ControlStyles.OptimizedDoubleBuffer, false );

                 // Cause the background to be cleared and redraw.
                 count = 6;
                 DrawToBuffer(grafx.Graphics);
         this.Refresh();
        }
        else
        {
                // Toggle whether the redraw timer is active.
        if( timer1.Enabled )
            timer1.Stop();
        else
            timer1.Start();
        }
        }

    private void OnTimer(object sender, EventArgs e)
    {
            // Draw randomly positioned ellipses to the buffer.
        DrawToBuffer(grafx.Graphics);

            // If in bufferingMode 2, draw to the form's HDC.
        if( bufferingMode == 2 )
                    // Render the graphics buffer to the form's HDC.
            grafx.Render(Graphics.FromHwnd(this.Handle));
            // If in bufferingMode 0 or 1, draw in the paint method.
        else
            this.Refresh();
    }

        private void OnResize(object sender, EventArgs e)
        {
           // Re-create the graphics buffer for a new window size.
           context.MaximumBuffer = new Size(this.Width+1, this.Height+1);
           if( grafx != null )
           {
           grafx.Dispose();
               grafx = null;
           }
           grafx = context.Allocate(this.CreateGraphics(),
               new Rectangle( 0, 0, this.Width, this.Height ));

           // Cause the background to be cleared and redraw.
           count = 6;
           DrawToBuffer(grafx.Graphics);
       this.Refresh();
        }	

    private void DrawToBuffer(Graphics g)
    {
            // Clear the graphics buffer every five updates.
        if( ++count > 5 )
            {
                count = 0;
                grafx.Graphics.FillRectangle(Brushes.Black, 0, 0, this.Width, this.Height);
            }

            // Draw randomly positioned and colored ellipses.
        Random rnd = new Random();
        for( int i=0; i<20; i++ )
        {
        int px = rnd.Next(20,this.Width-40);
        int py = rnd.Next(20,this.Height-40);
        g.DrawEllipse(new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0,255), rnd.Next(0,255)), 1),
            px, py, px+rnd.Next(0, this.Width-px-20), py+rnd.Next(0, this.Height-py-20)); 	    			
        }

            // Draw information strings.
        g.DrawString("Buffering Mode: "+bufferingModeStrings[bufferingMode], new Font("Arial", 8), Brushes.White, 10, 10);
            g.DrawString("Right-click to cycle buffering mode", new Font("Arial", 8), Brushes.White, 10, 22);
            g.DrawString("Left-click to toggle timed display refresh", new Font("Arial", 8), Brushes.White, 10, 34);
    }

    protected override void OnPaint(PaintEventArgs e)
        {
        grafx.Render(e.Graphics);
    }

        [STAThread]
        public static void Main(string[] args)
        {
        Application.Run(new BufferingExample());
        }
    }
}

Keterangan

Kelas ini BufferedGraphics memungkinkan Anda menerapkan buffering ganda kustom untuk grafik Anda. Ini menyediakan pembungkus untuk buffer grafis, bersama dengan metode yang dapat Anda gunakan untuk menulis ke buffer dan merender kontennya ke perangkat output.

Grafik yang menggunakan buffering ganda dapat mengurangi atau menghilangkan kedipan yang disebabkan oleh penggambaran ulang permukaan tampilan. Saat Anda menggunakan buffering ganda, grafik yang diperbarui pertama kali digambar ke buffer dalam memori, dan isi buffer ini kemudian dengan cepat ditulis ke beberapa atau semua permukaan yang ditampilkan. Timpa grafik yang ditampilkan secara relatif singkat ini biasanya mengurangi atau menghilangkan kedipan yang terkadang terjadi ketika grafik diperbarui.

Catatan

Dalam .NET 6 dan versi yang lebih baru, paket System.Drawing.Common, yang mencakup jenis ini, hanya didukung pada sistem operasi Windows. Penggunaan jenis ini dalam aplikasi lintas platform menyebabkan peringatan waktu kompilasi dan pengecualian run-time. Untuk informasi selengkapnya, lihat System.Drawing.Common hanya didukung di Windows.

Catatan

Cara paling sederhana untuk menggunakan buffering ganda adalah dengan mengatur OptimizedDoubleBuffer bendera gaya kontrol pada kontrol menggunakan SetStyle metode . OptimizedDoubleBuffer Mengatur bendera untuk kontrol mengalihkan semua lukisan untuk kontrol melalui buffer grafis default, tanpa memerlukan kode tambahan apa pun. Bendera ini diatur ke true secara default.

Kelas BufferedGraphics tidak memiliki konstruktor publik dan harus dibuat oleh BufferedGraphicsContext untuk domain aplikasi menggunakan metodenya Allocate . Anda dapat mengambil BufferedGraphicsContext untuk domain aplikasi saat ini dari properti statis BufferedGraphicsManager.Current .

Properti Graphics dapat digunakan untuk menggambar ke buffer grafis. Properti ini menyediakan akses ke Graphics objek yang menggambar ke buffer grafis yang dialokasikan untuk objek ini BufferedGraphics .

Metode Render tanpa argumen menggambar konten buffer grafis ke permukaan yang ditentukan ketika buffer dialokasikan. Kelebihan beban Render metode lainnya memungkinkan Anda menentukan Graphics objek atau IntPtr objek yang menunjuk ke konteks perangkat untuk menggambar konten buffer grafis.

Untuk informasi selengkapnya tentang menggambar grafik buffer ganda, lihat Grafik Buffered Ganda.

Properti

Graphics

Graphics Mendapatkan objek yang menghasilkan buffer grafis.

Metode

Dispose()

Merilis semua sumber daya yang BufferedGraphics digunakan oleh objek .

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
Finalize()

Memungkinkan objek untuk mencoba membebaskan sumber daya dan melakukan operasi pembersihan lainnya sebelum diklaim kembali oleh pengumpulan sampah.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
Render()

Menulis isi buffer grafis ke perangkat default.

Render(Graphics)

Menulis isi buffer grafis ke objek yang ditentukan Graphics .

Render(IntPtr)

Menulis konten buffer grafis ke konteks perangkat yang terkait dengan handel yang ditentukan IntPtr .

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk

Produk Versi
.NET Framework 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
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Keamanan Thread

Kelas BufferedGraphics ini tidak aman untuk utas. Saat mengakses buffer grafis dari utas terpisah, penting untuk menggunakan mekanisme kontrol akses utas untuk mencegah konflik.

Lihat juga