Use double buffering technology for plotting under. NET Framework

Source: Internet
Author: User

This article mainly introduces the Basic Drawing Technology of the. Net Framework. The advantages and disadvantages of Drawing Technology and other precautions are discussed through brief introduction and example programs.

Introduction

Fortunately, when writing a typical Windows form program, you do not need to consider the drawing and effect operations of forms and controls. Why? Because by using. net Framework, developers can drag a series of controls to the form, write some simple code associated with the event, and then press F5 In the IDE, a complete Form program was born! All controls draw their own, and the size and scaling of the form or control can be adjusted freely. It is often used here, and the control effect should be noted. For games, custom chart controls, and Screen Saver Programming, the programmer needs to write additional code to respond to the Paint event.

This article targets those Windows forms developers and helps them use simple plotting techniques during application preparation. First, we will discuss some basic plotting concepts. Who is responsible for drawing? How does a Windows form program know when to draw? Where exactly are the drawing codes placed? Next, we will introduce the dual Buffer technology for Image Rendering. You will see how it works and how to use a method to achieve the alternation between the cache and the actually displayed image. Finally, we will discuss "smart invalid areas". Actually, we just re-paint or clear invalid parts of the application form to speed up the display and response of the program. We hope that these concepts and technologies will guide readers through this article and help them develop Windows Forms programs more quickly and effectively.

Windows Forms use the GDI + Image Engine. All the drawing code in this article involves using the managed. Net Framework to manipulate and use the Windows GDI + Image Engine.

Although this article is used for basic form drawing operations, it also provides fast, effective techniques and methods that help improve program performance. Therefore, before reading this article, we recommend that you have a basic understanding of the. Net Framework, including Windows form event processing, simple GDI + objects such as Line, Pen, and Brush. Familiar with Visual Basic. Net or C # programming language.

Concept

Windows applications are drawn by themselves. When a form is "not clean", that is, the size of the form is changed, or some of the forms are overwritten by other programs, or restored from the minimized state, the program will receive the information to be drawn. In Windows, this "not clean" status is called "invalid (Invalidated)". We understand that re-painting is required, when the Windows form program needs to redraw the form, it obtains the drawn information from the Windows message queue. This information is encapsulated by the. Net Framework and then transmitted to the PaintBackground and painting events of the form. In the above events, you can properly write the code specifically for drawing.

A simple drawing example is as follows:

Using System;
Using System. Drawing;
Using System. Windows. Forms;
Public class BasicX: Form {

Public BasicX (){
InitializeComponent ();
}

Private void BasicX_Paint (object sender, PaintEventArgs e ){
Graphics g = e. Graphics;
Pen p = new Pen (Color. Red );
Int width = ClientRectangle. Width;
Int height = ClientRectangle. Height;
G. DrawLine (p, 0, 0, width, height );
G. DrawLine (p, 0, height, width, 0 );
P. Dispose ();
}

Private void InitializeComponent (){
This. SetStyle (ControlStyles. ResizeRedraw, true );
This. ClientSize = new System. Drawing. Size (300,300 );
This. Text = "BasicX ";
This. Paint + = new PaintEventHandler (this. BasicX_Paint );
}

[System. STAThreadAttribute ()]
Public static void Main (){
Application. Run (new BasicX ());
}
}

The above code is divided into two basic steps to create a sample program. First, the InitializeComponent method contains some attribute settings and the processing process of adding a form Paint event. Note that the style of the control is also set in the method. Setting the style of the control is also an effective way to customize Windows Forms and control behaviors. For example: the "ResizeRedraw" attribute of the control indicates that the form must be completely re-painted when the size of the form changes. That is to say, the customer area of the entire form must always be re-painted. The "customer region" of a form refers to all the form regions except the title bar and border. You can perform an interesting experiment to cancel the property of the control and then run the program. We can clearly see why this property is often set, because the invalid area after the form size is adjusted is not repainted at all.

Well, we need to pay attention to the BasicX_Paint method. As mentioned earlier, the Paint event is activated when the program needs to be repainted, the program form uses the Paint event to respond to the system message to be repainted. The BasicX_Paint method call requires an object sender and a PaintEventArgs type variable, the PaintEventArgs class instance or variable e encapsulates two important data. The first is the Graphics object of the form, this object indicates the surface of a form that can be drawn, also known as a canvas, used to draw lines, text, and images. The second data is ClipRectangle. This Rectangle object indicates an invalid rectangular range on the form, that is, the area where the form needs to be repainted. Remember, after the ResizeRedDraw of the form is set, the size of the ClipRectangle is equal to the size of the entire customer area of the form, or the part of the cut area covered by other program forms. We will describe the usefulness of some cut areas in more detail in the intelligent re-painting chapter.

Interface for running the BasicX sample program

Double buffer plot Technology

The dual-buffer technology allows the program to draw more quickly and smoothly, effectively reducing the image flickering during painting. The basic principle of this technology is to first draw an image to a canvas in the memory. Once all the painting operations are completed, push the canvas in the memory to the form or control surface to display it. After this operation, the program can make the user feel faster and more beautiful.

The example program provided below can clarify the concept and implementation method of dual buffer. This example has complete functions and can be used in practical applications. Later in this chapter, we will also mention that this technology should work with some property settings of controls to achieve better results.

Run the SpiderWeb sample program to learn the benefits of the dual-buffer plot technology. After the program starts and runs, adjust the window size. You will find that the efficiency of using this drawing algorithm is not high, and a large amount of flashes appear during the resizing process.

SpiderWeb sample programs without dual Buffer technology

Looking at the source code of the program, you will find that after the program Paint event is activated, the line is drawn by calling the LineDrawRoutine method. The LineDrawRoutine method has two parameters. The first is that the Graphics object is used to draw lines, and the second is the Pen object of the drawing tool used to draw lines. The code is quite simple, a loop statement, LINEFREQ constant, etc. The program is underlined from the lower left of the form surface to the upper right. Please note that the program uses floating point numbers to calculate the drawing position on the form. The advantage of this is that when the size of the form changes, the location data will be more accurate.

Private void LineDrawRoutine (Graphics g, Pen p ){
Float width = ClientRectangle. Width;
Float height = ClientRectangle. Height;
Float xDelta = width/LINEFREQ;
Float yDelta = height/LINEFREQ;

For (int I = 0; I <LINEFREQ; I ++ ){
G. DrawLine (p, 0, height-(yDelta * I), xDelta * I, 0 );
}
}

Write a simple code for responding to the Paint event SpiderWeb_Paint. As mentioned earlier, the Graphics object is the drawing surface of the form extracted from the PaintEventArgs object of the Paint event parameter. This Graphics object is passed together with the newly created Pen object to the LineDrawRoutine method to draw a line like a spider. The resources occupied by the Graphics object and the Pen object are released after the Graphics object and the Pen object are used, then the entire painting operation is complete.

Private void SpiderWeb_Paint (object sender, PaintEventArgs e ){
Graphics g = e. Graphics;
Pen redPen = new Pen (Color. Red );
// Call our isolated drawing routing
LineDrawRoutine (g, redPen );
RedPen. Dispose ();
G. Dispose ();
}

So what changes can be made to implement a simple dual Buffer technology for the SpiderWeb program above? In fact, the principle is quite simple, that is, the painting operation that should be painted on the form surface should be converted to the bitmap in the memory first, and LineDrawRoutine will execute the same spider network Painting Operation to the canvas hidden in the memory, call Graphics after drawing. the DrawImage method pushes the hidden content on the canvas to the surface of the form for display. Finally, a high-performance drawing form program is completed with some minor changes.

Compare the differences between the following dual-buffer plot events and the simple plot events described above:

Private void SpiderWeb_DblBuff_Paint (object sender, PaintEventArgs e ){
Graphics g = e. Graphics;
Pen bluePen = new Pen (Color. Blue );
// Create our offscreen bitmap
Bitmap localBitmap = new Bitmap (ClientRectangle. Width, ClientRectangle. Height );
Graphics bitmapGraphics = Graphics. FromImage (localBitmap );
// Call our isolated drawing routing
LineDrawRoutine (bitmapGraphics, bluePen );
// Push our bitmap forward to the screen
G. DrawImage (localBitmap, 0, 0 );
BitmapGraphics. Dispose ();

BluePen. Dispose ();
LocalBitmap. Dispose ();
G. Dispose ();
}
  

The above Sample Code creates a memory bitmap object, which is equal to the size of the form's customer region (that is, the drawing surface) by calling Graphics. fromImage transfers the reference of the bitmap in the memory to the Graphics object. That is to say, all subsequent operations on the Graphics object are actually performed on the bitmap in the memory, this operation is equivalent to copying the pointer of a bitmap object to a Graphics object in C ++. The two objects use the same memory address. The Graphics object represents a canvas at the back of the screen, which plays a crucial role in the dual Buffer technology. All line drawing operations are targeted at bitmap objects in the memory. The next step is to call the DrawImage
Article from: good love Learning Network (http://www.haoxiai.net) Web site: http://www.haoxiai.net/wangzhanzhizuo/aspnet/52357.html

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.