C # GDI drawing double-buffer drawing analysis

Source: Internet
Author: User

Double-buffered plotting analysis

1. Principles of Windows Drawing
We see various elements in the Windows environment, such as menus, buttons, windows, images, which are basically "painted". At this point, the screen is equivalent to a blackboard, and the various GDI elements under Windows, such as brushes, brush, etc., are equivalent to colored chalk. When we hand-draw on the blackboard, it is a stroke, and so is the computer. It's just that the speed of the computer is much faster than the manual, so it looks as if all the graphic text appears at the same time.
2. Limitations of ordinary drawing methods
Let's call this drawing method a common drawing method. Although this approach can satisfy a considerable portion of the drawing needs, when the object to be drawn is too complex, especially if it contains bitmaps, the computer will be overwhelmed. At this time the screen will show very slow, for the movement of the picture, will give people "card" live feeling, in short a word: uncomfortable.
3, the solution: double buffering
The principle of double buffering can be interpreted in such a way that the computer screen is viewed as a blackboard. First, we create a "virtual" blackboard in the memory environment, and then draw complex graphics on the blackboard, and when all the graphics are drawn, once again, the graphics in memory are "copied" onto the other Blackboard (screen). This method can improve the drawing speed and greatly improve the drawing effect.

The implementation process is as follows:

1. Create a buffer in memory that is consistent with the canvas

2. Draw in Buffer

3. Copy the buffer bitmap to the current canvas

4. Free memory buffers

Drawing examples:

A new form is added to the WinForm application, and three timer is placed in the form using the original drawing mode,thebitmap Double slow drawing mode, BufferedGraphicsContext double buffer drawing mode, timer Inteval set to 10, the other three sets of buttons to control the timer switch;

Referenced namespaces:

Using System.Drawing;

Using System.Drawing.Drawing2D;

DateTime T1 =DateTime.Now; Graphics g= This.            CreateGraphics ();            LinearGradientBrush Brush; if(flag) {Brush=NewLinearGradientBrush (NewPointF (0.0f,0.0f),NewPointF (700.0f,300.0f), Color.Blue, color.red); Flag=false; }            Else{Brush=NewLinearGradientBrush (NewPointF (0.0f,0.0f),NewPointF (400.0f,300.0f), Color.Blue, color.red); Flag=true; }             for(inti =0; I < -; i++)            {                 for(intj =0; J < -; J + +) {G.fillellipse (brush, J*TenITen,Ten,Ten); }} DateTime T2=DateTime.Now; TimeSpan TS= T2-T1; floatper = +/Ts.milliseconds; Label1. Text="Speed:"+ per. ToString () +"frame/s";
Timer1 Basic Drawing Mode
DateTime T1 =DateTime.Now; Bitmap BMP=NewBitmap ( -, -); Graphics g=graphics.fromimage (BMP); G.smoothingmode= Smoothingmode.highquality;//quality of picture renderingG.pixeloffsetmode = pixeloffsetmode.highquality;//pixel offset ModeLinearGradientBrush Brush; if(flag) {Brush=NewLinearGradientBrush (NewPointF (0.0f,0.0f),NewPointF (700.0f,300.0f), Color.Blue, color.red); Flag=false; }            Else{Brush=NewLinearGradientBrush (NewPointF (0.0f,0.0f),NewPointF (400.0f,300.0f), Color.Blue, color.red); Flag=true; }             for(inti =0; I < -; i++)            {                 for(intj =0; J < -; J + +) {G.fillellipse (brush, J*TenITen,Ten,Ten); }            }             This. CreateGraphics (). DrawImage (BMP,0,0);//stick the canvas on the screenDateTime t2 =DateTime.Now; TimeSpan TS= T2-T1; floatper = +/Ts.milliseconds; Label2. Text="Speed:"+ per. ToString () +"frame/s";
Timer2 Bitmap Method Drawing

Drawing process:

1.Create a " virtual canvas "in memory:

Bitmap bmp = New Bitmap (600, 600);

2. Get the Graphics reference forthis memory canvas :

Graphics g = graphics.fromimage (BMP);

3.draw on this piece of memory canvas:

G.fillellipse (Brush, 100, 100, 100, 100);

4. Draw the memory canvas into the window

This. CreateGraphics (). DrawImage (BMP, 0, 0);

DateTime T1 =DateTime.Now; BufferedGraphicsContext Current= BufferedGraphicsManager.Current;//provides methods for creating a graphics bufferBufferedGraphics BG; BG= Current. Allocate ( This. CreateGraphics (), This.            DisplayRectangle); Graphics g=BG.            Graphics;            LinearGradientBrush Brush; if(flag) {Brush=NewLinearGradientBrush (NewPointF (0.0f,0.0f),NewPointF (700.0f,300.0f), Color.Blue, color.red); Flag=false; }            Else{Brush=NewLinearGradientBrush (NewPointF (0.0f,0.0f),NewPointF (400.0f,300.0f), Color.Blue, color.red); Flag=true; }             for(inti =0; I < -; i++)            {                 for(intj =0; J < -; J + +) {G.fillellipse (brush, J*TenITen,Ten,Ten); }} BG.            Render (); Bg.            Dispose (); DateTime T2=DateTime.Now; TimeSpan TS= T2-T1; floatper = +/Ts.milliseconds; Label3. Text="Speed:"+ per. ToString () +"frame/s";
Timer3 BufferedGraphicsContext Double-buffered drawing

Drawing process:

1. Obtain a reference to an instance of the BufferedGraphicsContext class.
2. Create an instance of the BufferedGraphics class by calling the BufferedGraphicsContext.Allocate method.

3. Draw the graph to the graphics buffer by setting the landlord BufferedGraphics.Graphics property.
4. When you finish drawing operations in all graphics buffers, you can call the
The BufferedGraphics.Render method renders the contents of the buffer to the drawing surface associated with the buffer or to the specified drawing surface.

5. After you finish rendering the drawing, call the Dispose method that frees the system resources for the BufferedGraphics instance.

Drawing effect:

As can be seen in the above example

The basic drawing mode will obviously feel the picture from the top to the bottom of the refresh, refresh speed of the frame/s image refresh rate is very slow

Use bitmap drawing mode to see the screen is constantly flashing, refresh speed for the 13~14 frame/s image refresh rate much faster

Use BufferedGraphicsContext double buffering mode to see the screen blink faster, refresh speed to 27~28 frame/s image Refresh fastest

Other drawing note points:

>>graphicspath connecting adjacent points into lines

Graphics g = This. CreateGraphics (); System.Drawing.Drawing2D.GraphicsPath Path=NewSystem.Drawing.Drawing2D.GraphicsPath ();  for(inti =0; I < Lstpoint.count-1; i++) {path. AddLine (NewPoint (Lstpoint[i], Dicpoint[lstpoint[i]]),NewPoint (Lstpoint[i +1], Dicpoint[lstpoint[i +1]]));//The example adds the X-and y-coordinates of the front and back two points} SolidBrush Brush=NewSolidBrush (C_line); G.drawpath (NewPen (brush), path);
GraphicsPath

>>graphics.drawcurve can smooth the connecting parts of two straight lines

this new point[lstpoint.count];                for (int0; i < lstpoint.count; i++)            {                = lstpoint[i];                 = Dicpoint[lstpoint[i]];            }            SolidBrush Brush=new  SolidBrush (c_line);            G.drawcurve (new Pen (brush), array_point);    
DrawCurve

C # GDI drawing double-buffer drawing analysis

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.