C # Use double buffering technology to solve the problem of drawing flash screen.

Source: Internet
Author: User

During this time, I made a small game and used some graphics when displaying the interface. All the images involved at the beginning were replaced by the background image of the control. In this way, a major problem occurs when the game runs: The game runs slowly. The team members are about to give up, and every Member is sad. I am very sad in my heart... I don't need to talk about it anymore. To improve the running performance of the game, the original control method is changed to the drawing method, that is, drawing using the DrawImage () method in C # can improve the slow running of the game. Then, test the use of the DrawImage () method. The picture is displayed and can be moved, but a fatal problem occurs. The problem occurs when the image is moved (essentially the re-painting of the image. To solve this problem, I found a lot of information on the Internet and referred to many books. The solution to this problem is to use a double buffer technology provided in C. However, no one on the Internet or in the book can use a simple example to let me know how to use the Double Buffer technology. After several hours, I finally figured it out. Share with you. At the same time, I also used this simple test example to learn how to quickly use the dual-buffer technology to solve the problem. 1. Create a project and add a button in the window. Click this button to move the image. 2. the method used for double buffering is: first create a virtual canvas and add SetStyle (ControlStyles) to the Form1 () method. userPaint, true); SetStyle (ControlStyles. allPaintingInWmPaint, true); // do not erase the background. setStyle (ControlStyles. doubleBuffer, true); copy the code private Graphics g; Bitmap bmp = new Bitmap (600,600); // create a canvas Graphics g1; private int x = 0; public Form1 () {SetStyle (ControlStyles. userPaint, true); SetStyle (ControlStyles. allPaintingInWmPaint, true );// Do not erase background. setStyle (ControlStyles. doubleBuffer, true); g1 = Graphics. fromImage (bmp); // a Graphics object is created on the canvas. To draw the image InitializeComponent ();} on the canvas, copy the code and click the button event, show the image on the canvas and copy the code to the window // button1 click to process the event private void button#click (object sender, EventArgs e) {// g1.DrawEllipse (new Pen (System. drawing. color. red), 10, 10,100,100); g1.DrawImage (Image. fromFile ("E:/down.png"), x, 10); // this is the drawing on the canvas this. createGr Aphics (). drawImage (bmp, 0, 0); // displays the image in the window. timer1.Enabled = true; // g. dispose () ;}copy the code and use the timer control to keep the image moving. The Code is as follows: copy the code private void timereffectick (object sender, EventArgs e) {x ++; // This is a global variable used to change the x-axis g1.Clear (Form1.DefaultBackColor) of the Image; // This is to clear the previous Image g1.DrawImage (Image. fromFile ("E:/down.png"), x, 10); // re-draw the new image. At this time, the position is 1 more than the previous position. createGraphics (). drawImage (bmp, 0, 0); // display it again in the window without flashing} copy the Code if SetStyl is not added E (ControlStyles. userPaint, true); SetStyle (ControlStyles. allPaintingInWmPaint, true); // do not erase the background. setStyle (ControlStyles. doubleBuffer, true); The Code will flash the screen. The complete code is as follows: copy the code using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace ttest {public partial class Form1: Form {private Graphics g; Bitmap bmp = new Bitmap (600,600); // create a canvas Graphics g1; private int x = 0; public Form1 () {SetStyle (ControlStyles. userPaint, true); SetStyle (ControlStyles. allPaintingInWmPaint, true); // do not erase the background. setStyle (ControlStyles. doubleBuffer, true); g1 = Graphics. fromImage (bmp); InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {// g1 = this. createGraphics ();} // button1 click the private void button1_Click (object sender, EventArgs e) {// g1.DrawEllipse (new Pen (System. drawing. color. red), 10, 10,100,100); g1.DrawImage (Image. fromFile ("E:/down.png"), x, 10); // this is the drawing on the canvas this. createGraphics (). drawImage (bmp, 0, 0); // displays the image in the window. timer1.Enabled = true; // g. dispose ();} private void timereffectick (object sender, EventArgs e) {x ++; // This is a global variable used to change the abscissa g1.Clear (Form1.DefaultBackColor) of an image ); // This is to clear the previous Image g1.DrawImage (Image. fromFile ("E:/down.png"), x, 10); // re-draw the new image. At this time, the position is 1 more than the previous position. createGraphics (). drawImage (bmp, 0, 0); // display it on the window again without flashing }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.