GDI + graphic Image Technology 1

Source: Internet
Author: User

The System.Drawing namespace provides access to GDI + basic graphics features, which provide more advanced functionality in some of the child namespaces.

GDI +, developed by GDI, is a bridge between the Windows graphical display program and the actual physical device.

GDI + is an enhanced version of GDI that provides the following three types of services:

1) Two-dimensional vector graphics: GDI + provides classes (or structs) that store the information of the graphical primitives themselves, classes that store information about how the graphics primitives are plotted, and classes that are actually drawn.

2) Image processing: Provide bitmap, image and other classes.

3) Text display: Supports the use of various fonts, font size and style to display text.

GDI interfaces are function-based, and GDI + is an object-based application programming interface for C + + classes, making it easier to use than GDI.

The Graphics object represents the GDI + drawing surface, which is the object used to create the graphic image.

There are generally three ways to create a Graphics object:

1) The Graphics object in the PaintEventArgs of the Paint event

2) Create a Graphics object with the CreateGraphics method

3) Create a Graphics object from image

namespaceWindowsFormsApplication2 { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_paint (Objectsender, PaintEventArgs e) {Graphics G1= E.graphics;//The first wayBitmap IMG1 =NewBitmap ( -, -); Graphics G2= Graphics.fromimage (IMG1);//The second wayGraphics g3 = This. CreateGraphics ();//The Third WayMessageBox.Show ("Create success! "); }    }}

Create a Pen object: Pen

namespaceWindowsFormsApplication2 { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_paint (Objectsender, PaintEventArgs e) {Graphics G1=E.graphics; Pen P=NewPen (Color.Blue,1);//blue with a width of 1 pixels wideG1. DrawEllipse (P,0,0, -, -);//Draw an ellipseG1.        Dispose (); }    }}

Create a Brush object (brush)

1) SolidBrush Solid color Fill Graphics

2) TextureBrush fill graphics with raster-based images (bitmaps, JPG, etc.)

3) LinearGradientBrush fill the graphic with a color gradient

4) PathGradientBrush fills the shape with a gradient direction that points to the center of the graphic from a path-defined graphic boundary

5) HatchBrush fill graphics with various patterns

Private void Form1_paint (object  sender, PaintEventArgs e) {            = e.graphics;             New SolidBrush (Color.Blue);      // Define a blue brush (a solid color fill shape)            0 0  -  $ );            G1. Dispose ();        }
Private voidForm1_paint (Objectsender, PaintEventArgs e) {            stringPath =@"e:\hello\1.jpg"; Graphics G1=E.graphics;            Bitmap img; if(System.IO.File.Exists (path)) {img=NewBitmap (path); Brush BR=NewTextureBrush (IMG); G1. FillEllipse (BR,0,0, $, $); Br.            Dispose (); } Else{MessageBox.Show ("picture file does not exist! "); } G1.        Dispose (); }

LinearGradientBrush to introduce the System.Drawing.Drawing2D namespace before use

GDI + provides a linear gradient in horizontal, vertical, and diagonal directions. By default, the colors in the linear gradient change evenly. You can also customize the gradient to make the color non-uniform changes.

Private voidForm1_paint (Objectsender, PaintEventArgs e) {Graphics G1=E.graphics; LinearGradientBrush LGB=NewLinearGradientBrush (NewPoint (0,0),//defining the starting point                    NewPoint ( -, -),//Define end pointColor.FromArgb (255,0,0,255),//Start ColorColor.FromArgb (255,0,255,0)//End Color                ); G1. FillEllipse (LGB,0,0, $, $); G1.        Dispose (); }

In GDI +, a path is a series of lines and curves maintained by the GraphicsPath object.

Private voidForm1_paint (Objectsender, PaintEventArgs e) {GraphicsPath GP=NewGraphicsPath (); Gp. AddEllipse ( -,0, $, -); PathGradientBrush PGB=NewPathGradientBrush (GP); Pgb. Centercolor= Color.FromArgb (255,0,0,255); color[] Colors= {Color.FromArgb (255,0,255,255) }; Pgb. Surroundcolors=colors; E.graphics.fillellipse (PGB, -,0, $, -); }

The shadow pattern consists of two colors: the background color, the color of the line that forms the pattern on the background. To fill a closed shape with a hatch pattern, you use the HatchBrush class object.

Private void Form1_paint (object  sender, PaintEventArgs e) {            // three parameters: Shadow Style, hatch color, background color            New HatchBrush (hatchstyle.largecheckerboard,color.red,color.yellow);                 0 0  -  - );        }

Draw a rectangle

Private void Form1_paint (object  sender, PaintEventArgs e) {            = e.graphics;             New 2 );             Ten Ten 260 );       // Brush, upper-left coordinate, lower-right corner coordinate             p.dispose ();            G.dispose ();        }

ARC, Fan

Private voidForm1_paint (Objectsender, PaintEventArgs e) {Graphics g=E.graphics; Pen P=NewPen (Color.Red,2); G.drawarc (P, -, -, $, $, -, Max);//ArcG.drawpie (P,0, $, -, $, -, Max);//FanP.dispose ();        G.dispose (); }

Draw polygons

Private voidForm1_paint (Objectsender, PaintEventArgs e) {Graphics g=E.graphics; Pen P=NewPen (Color.Red,2); Point[] PG={                           NewPoint (0,0),                           NewPoint ( the, -),                           NewPoint (260,260),                           NewPoint ( the, the),                           NewPoint ( the,260)                       };            G.drawpolygon (P, PG);            P.dispose ();        G.dispose (); }

ColorDialog Controls: Palette

Private void button1_click (object  sender, EventArgs e) {            if (colordialog1.showdialog () = = DialogResult.OK)                {this. BackColor = colordialog1.color;            }        }

FontDialog Controls: Font selection Board

Private void button1_click (object  sender, EventArgs e) {            if (Fontdialog1.showdialog ( ) = = DialogResult.OK)                {= fontdialog1.font;            }        }

GDI + graphic Image Technology 1

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.