C #-GDI drawing, dual-buffer drawing, triggering of paint events --- shinepans

Source: Internet
Author: User
When using the GDI technology to draw images, you may find that the graphic lines are not smooth enough, or the windows may flash constantly when you change the window size. (use doublebuffer to solve it !) 1. lines are not smooth: There is a time difference between the re-painting of the window itself and the re-painting of the image, resulting in inconsistency between the two image display 2. changing the window size is not smooth: the background color of the image changes frequently during re-painting, causing visual flashes.

Here, we use four graphical examples to solve the problem: the besell curve, the circle, the rectangle, and the irregular figure.

 

Idea: first create a bitmap instance using the construction method of the bitmap class, then call the fromimage method of the graphics class to create a canvas object, and finally call the drawimage method of the grapgics class to create a graph in the current window.

Public Bitmap (INT width, int height); // width defines the width and height of a bitmap.

Bitmap lacalbitmap = new Bitmap (cilentrectangle. Width, cilentrectangle. Height); // create a bitmap instance of the same size as the window Workspace


Public static graphics fromimage (image); // image: instance reference of a subclass of the image class

Bitmap localbitmap = new Bitmap (cilentrectangle. Width, cilentrectangle. Height) // create a bitmap instance


Public void drawimage (image, int X, int y); // image: image to be drawn X: X coordinate of the upper left corner of the drawn image y coordinate of the upper left corner

Graphics G = E. graphics; // obtain the window canvas

G. drawimage (localbitmap,); // draw out the memory image in the window



Implementation: Because the paint is. net hide, we need to add our own paint event in the window code to draw the window this. paint + = new system. windows. forms. painteventhandler (this. form1_paint );

Private void initializecomponent ()
{
This. suspendlayout ();
//
// Form1
//
This. autoscaledimensions = new system. Drawing. sizef (6f, 12f );
This. autoscalemode = system. Windows. Forms. autoscalemode. Font;
This. clientsize = new system. Drawing. Size (388,325 );
This. maximizebox = false;
This. minimizebox = false;
This. Name = "form1 ";
This. Text = "Double Buffer technology drawing ";
This. Paint + = new system. Windows. Forms. painteventhandler (this. form1_paint );

This. resumelayout (false );
}

 

This. Paint + = new system. Windows. Forms. painteventhandler (this. form1_paint );
Source code: form1.cs:
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; using system. drawing. drawing2d; namespace doublebuffer {public partial class form1: FORM {public form1 () {initializecomponent ();} private void form1_paint (Object sender, painteventargs E) {bitmap localbitmap = new Bitmap (clientrectangle. width, clientrectangle. height); // create a bitmap instance graphics bitmapgraphics = graphics. fromimage (localbitmap); bitmapgraphics. clear (backcolor); bitmapgraphics. smoothingmode = smoothingmode. antialias; paintimage (bitmapgraphics); graphics G = E. graphics; // obtain the window canvas G. drawimage (localbitmap, 0, 0); // draw the image bitmapgraphics in the memory in the canvas of the window. dispose (); localbitmap. dispose (); G. dispose ();} private void paintimage (Graphics g) {// drawing graphicspath Path = new graphicspath (new point [] {New Point (350,200), new point ), new Point (105,225), new point (190, clientrectangle. bottom), new point (50, clientrectangle. bottom), new point (50,180)}, new byte [] {(byte) pathpointtype. start, (byte) pathpointtype. betiller, (byte) pathpointtype. betiller, (byte) pathpointtype. betiller, (byte) pathpointtype. line, (byte) pathpointtype. line}); pathgradientbrush pgb = new pathgradientbrush (PATH); pgb. surroundcolors = new color [] {color. green, color. yellow, color. red, color. blue, color. orange, color. lightblue}; G. fillpath (pgb, PATH); G. drawstring ("Double Buffer drawing", new font ("", 18, fontstyle. bold), new solidbrush (color. red), new pointf (110, 20); G. drawbeziers (new pen (New solidbrush (color. green), 2), new point [] {New Point (120,100), new point (120,120), new point (120,100), new point (120,150)}); G. drawarc (new pen (New solidbrush (color. blue), 5), new rectangle (new point (120,170), new size (60, 60), 0,360); G. drawrectangle (new pen (New solidbrush (color. orange), 3), new rectangle (new point (240,260), new size (90, 50 )));}}}

Form1 design:
Namespace doublebuffer {partial class form1 {// <summary> /// a required designer variable. /// </Summary> private system. componentmodel. icontainer components = NULL; // <summary> /// clear all resources in use. /// </Summary> /// <Param name = "disposing"> If the managed resource should be released, the value true; otherwise, the value false. </Param> protected override void dispose (bool disposing) {If (disposing & (components! = NULL) {components. dispose ();} base. dispose (disposing );} # region windows window designer generated code // <summary> // The designer supports the required methods-do not // use the code editor to modify the content of this method. /// </Summary> private void initializecomponent () {This. suspendlayout (); // form1 // This. autoscaledimensions = new system. drawing. sizef (6f, 12f); this. autoscalemode = system. windows. forms. autoscalemode. font; this. clientsize = new system. drawing. size (388,325); this. maximizebox = false; this. minimizebox = false; this. name = "form1"; this. TEXT = "Double Buffer technology drawing"; this. paint + = new system. windows. forms. painteventhandler (this. form1_paint); this. resumelayout (false) ;}# endregion }}


Program. CS:

Using system; using system. collections. generic; using system. LINQ; using system. windows. forms; namespace doublebuffer {static class program {/// <summary> /// main entry point of the application. /// </Summary> [stathread] Static void main () {application. enablevisualstyles (); application. setcompatibletextrenderingdefault (false); application. run (New form1 ());}}}




When the window is changed, the image will be deformed. You can set resizeredrae in the window attribute to true.


=

C #-GDI drawing, dual-buffer drawing, triggering of paint events --- shinepans

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.