C # GDI + drawing

Source: Internet
Author: User

1 coordinate system

(1) Coordinate origin: in the upper-left corner of the form or control, the coordinates are (0,0)

(2) Positive direction: the positive direction of the x-axis is horizontal to the right, the y-axis positive direction is vertical downward

(3) Units: In the setting, generally in pixels, pixels (Pixel) are composed of images (picture) and elements (element), is used to calculate the digital image of a unit.

By enlarging the image several times, you will find that these successive tones are actually composed of many small square dots of similar color, which are the smallest units of pixels that make up the image.

The quality of the graph is determined by the pixel, the larger the pixel, the greater the resolution.

2 Namespace---system.drawing

(1) System.Drawing provides access to the basic graphics features of GDI +

(2) Common basic class and structure of System.Drawing

Class

Description

Bitmap

An object that is used to process images with pixel data definitions.

Brush

Defines the inner object used to fill the shape of a graphic.

Font

Defines a specific text format.

Graphics

Encapsulates a GDI + drawing and cannot inherit this class.

Pen

Objects that are used to draw lines and curves cannot inherit this class.

Region

Indicates the interior of a shape that is composed of rectangles and paths that cannot inherit this class.

Color

Represents the RGB color.

Point

Defines a point defined in a two-dimensional plane.

Rectangle

Stores a set of 4 integers, representing the position and size of a rectangle.

Size

Stores an ordered integer pair, usually the width and height of a rectangle.

3 Graphics class

The graphics class encapsulates a GDI + drawing interface that provides methods for drawing objects to the display interface. When you create a graphical image using GDI +, you need to create a graphics object, that is, where to draw.

There are 3 different types of drawing interfaces:

(1) Forms and controls

(2) Printer

(3) Bitmap for memory

To create a graphical object in the 3 method:

(1) OnPaint () method parameter of the control class PaintEventArgs get the Graphics object

(2) The CreateGraphics () method in the form class or control class obtains the Graphics object

(3) Produce a graphics object from a Bitmap object (BITMAP)

Common methods of the Graphics class

Name

Description

Dispose

Releases all resources used by the graphics.

DrawEllipse

Draws an ellipse, with height, width, and pair of coordinates.

DrawArc

Draw arcs.

DrawLine

Draws a line, specified by 2 points.

DrawPolygon

Draws a polygon defined by a set of point structures.

DrawRectangle

Draws a rectangle.

Drawpie

Draw a fan.

Drawcurse

Draws the curve, specified by the parameter point array.

FillEllipse

Fills the interior of the ellipse defined by the bounding rectangle.

FillRegion

Fills the interior of the region.

ScaleTransform

Applies the scaled action you have made to the secondary graphics.

Tanslatetransform

Pan changes the origin of the coordinate system.

4 Drawing Tool Classes

Class name

Description

Pen

Sets the brush's color, line weight, and line style (solid and dashed).

Brush

Used to fill the shape, set the brush's style, color, and the thickness of the line.

5 derived classes of the brush class

Name

Description

ImageBrush

The image drawing area.

LinearGradientBrush

Linear gradient drawing area.

RadialGradientBrush

The radial gradient drawing area, where the focus defines the beginning of the gradient, and the ellipse defines the end point of the gradient.

SolidColorBrush

A monochrome paint area.

VideoBrush

The area where the video content is drawn.

6 Cases Free http://download.csdn.net/detail/taoerit/8350869


7 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;using system.drawing.drawing2d;namespace GDI drawing {        Public partial class Maindialog:form {public Maindialog () {InitializeComponent (); private void Maindialog_load (object sender, EventArgs e) {} private void Linebutton_click (o Bject sender, EventArgs e) {//Draw straight Graphics gra = this.            CreateGraphics ();            Pen pen = new Pen (color.red); Pen.            Width = 2;            Point startPoint = new Point (20,20);            Point endPoint = new Point (70,20); Gra.                        DrawLine (Pen,startpoint,endpoint); Pen.            Dispose (); Gra.        Dispose (); private void Rectanglebutton_click (object sender, EventArgs e) {//Draw rectangle Graphics GRA = this. CreateGraphics ();            Pen pen = new Pen (color.red); Gra.            DrawRectangle (pen, 20, 50, 100,100); Pen.            Dispose (); Gra.        Dispose ();            } private void Cyliderbutton_click (object sender, EventArgs e) {//cylinder, with many ellipses having a bottom gradually stacked up, and the last fill color int height = this.            clientsize.height-150; int width = this.            clientsize.width-50;            int vheight = 200;            int vwidth = 100; Graphics gra = this.            CreateGraphics (); Gra.            Clear (Color.White);            Pen pen = new Pen (color.gray,2);            SolidBrush brush = new SolidBrush (Color.gainsboro); for (int i = HEIGHT/2; i > 0;i--) {gra.            DrawEllipse (Pen,width/2,i,vheight,vwidth); } gra.        FillEllipse (Brush,width/2,0,vheight,vwidth); private void Fillrectanglebutton_click (object sender, EventArgs e) {//Draw rectangle Graphics GRA = this.            CreateGraphics (); Pen PEn = new Pen (color.red,3); Brush brush = Pen.            Brush;            Rectangle rect = new Rectangle (20,50,100,100); Gra.            FillRectangle (Brush,rect); Gra.        Dispose (); private void Drawellispebutton_click (object sender, EventArgs e) {Graphics gra = this.            CreateGraphics ();            Rectangle rect = new Rectangle (0,0,200,100);            LinearGradientBrush brush = new LinearGradientBrush (rect,color.orange,color.purple,90); Gra.            FillEllipse (Brush,rect); Gra.        Dispose (); private void Fontbutton_click (object sender, EventArgs e) {Graphics gra = this.            CreateGraphics ();            Font font = new Font ("script", 24,fontstyle.italic);            Pen pen = new Pen (color.blue,3); Gra. DrawString ("Windows application Design", Font,pen.        BRUSH,10,100);  private void Ellispebutton_click (object sender, EventArgs e) {//draw round Graphics gra = This. CreateGraphics ();            Pen pen = new Pen (color.red); Gra.            DrawEllipse (pen, 0, 0, 200,100); Pen.            Dispose (); Gra.        Dispose (); private void Moveellispebutton_click (object sender, EventArgs e) {//move round Graphics GRA = this.            CreateGraphics ();            Pen pen = new Pen (color.red); Gra. TranslateTransform (10,10);//Change the coordinates (10,10) gra.            DrawEllipse (pen, 0, 0, 200, 100); Gra.        Dispose (); private void Scaleellispebutton_click (object sender, EventArgs e) {//zoom round float XS            Cale = 1.5F;            float Yscale = 2F; Graphics gra = this.            CreateGraphics ();            Pen pen = new Pen (color.red); Gra. ScaleTransform (XScale, Yscale);//x-axis magnification 1.5 times times, y-axis magnification twice times gra.            DrawEllipse (pen, 0, 0, 200, 100); Gra.        Dispose (); private void Curvebutton_click (object sender, EventArgs e) {//Draw curve GraphicsGRA = this.            CreateGraphics ();            Pen pen = new Pen (color.blue,3); Point oo1 = new Point (30,this.            CLIENTSIZE.HEIGHT-100); Point oo2 = new Point (this. CLIENTSIZE.WIDTH-50, this.            CLIENTSIZE.HEIGHT-100); Gra.            DrawLine (PEN,OO1,OO2);            Point Oo3 = new Point (30, 30); Gra.            DrawLine (pen, oo1, Oo3);            Font font = new System.Drawing.Font ("Arial", 12,fontstyle.bold); Gra. DrawString ("X", Font,pen.            BRUSH,OO2); Gra. DrawString ("Y", Font,pen.            brush,10,10);            int x1 = 0, x2 = 0;            Double A = 0; Double y1 = 0, y2 = this.            clientsize.height-100; for (x2 = 0; x2 < this. clientsize.width;x2++) {a = 2 * Math.PI * X2/(this).                Clientsize.width);                y2 = Math.sin (a); y2 = (1-y2) * (this.                CLIENTSIZE.HEIGHT-100)/2; Gra.                DrawLine (pen,x1 +30, (float) Y1, x2+30, (float) y2);                x1 = x2;        y1 = y2;    } gra.        Dispose (); } private void Piechartbutton_click (object sender, EventArgs e) {//pie chart Graphics GRA = This.            CreateGraphics ();            Pen pen = new Pen (color.blue, 3);            Rectangle rect = new Rectangle (50,50,200,100);            Brush brush = new SolidBrush (Color.Blue); Gra. FillPie (pen.            BRUSH,RECT,0,60); Gra.            FillPie (brush,rect,60,150);            Brush = new SolidBrush (color.yellow); Gra.        FillPie (brush,rect,210,150); }    }}






C # GDI + drawing

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.