10 basic skills of GDI + programming

Source: Internet
Author: User
Create a drawing surface 

There are two common methods to create a drawing surface. Next, we try to obtain the plotting surface of picturebox.

Private void form1_load (Object sender, system. eventargs E)

{

// Obtain the drawing surface of picturebox1

Graphics G = This. picturebox1.creategraphics ();

}

Private void pictureboxappspaint (Object sender, system. Windows. Forms. painteventargs E)

{

// Obtain the drawing surface of picturebox1

Graphics G = E. graphics;

}

You can use the graphics object to draw various graphics. Both the paint event and the onpaint method of the control can be used to plot. In the onpaint method, the graphics attribute must be obtained from parameter E. The following are two examples.

Protected override void onpaint (painteventargs E)

{

E. Graphics. Clear (color. White );

Float X, Y, W, h;

X = This. Left + 2;

Y = This. Top + 2;

W = This. Width-4;

H = This. Height-4;

Pen = new pen (color. Red, 2 );

E. Graphics. drawrectangle (pen, X, Y, W, H );


Base. onpaint (E );

}

Private void pictureboxii_resize (Object sender, eventargs E)

{

This. invalidate ();

}

Private void button#click (Object sender, system. eventargs E)

{

This. pictureboxii1.creategraphics (). fillellipse (

Brushes. Blue, 10, 20, 50,100 );

}

Three types related to text:

Fontfamily-defines a group of words with similar basic designs but some differences in form. This class cannot be inherited.

Font -- defines a specific text format, including font, font size, and font attributes. This class cannot be inherited.

Stringformat-encapsulate text layout information (such as alignment and line spacing), display operations (such as ellipsis insertion and replacement of national numeric bits), and OpenType functions. This class cannot be inherited.

The following program displays a piece of text.

Private void button2_click (Object sender, system. eventargs E)

{

Graphics G = This. pictureboxii1.creategraphics ();

G. fillrectangle (brushes. White, this. pictureboxii1.clientrectangle );

String S = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ";

Fontfamily fm = new fontfamily ("& euml; & icirc; & igrave; & aring ;");

Font F = new font (FM, 20, fontstyle. Bold, graphicsunit. Point );

Rectanglef rectf = new rectanglef (30, 20,180,205 );

Stringformat Sf = new stringformat ();

Solidbrush sbrush = new solidbrush (color. fromargb (255, 0, 0,255 ));

SF. linealignment = stringalignment. Center;

SF. formatflags = stringformatflags. directionvertical;

G. drawstring (S, F, sbrush, rectf, SF );

}

Path of GDI + -- graphicspath class

The graphicspath class provides a series of attributes and methods that can be used to obtain key points in a path and add geometric elements such as line segments and circles. You can obtain the surrounded rectangle and perform a pick-up test. How to use these functions, please take a closer look.

Private void button3_click (Object sender, system. eventargs E)

{

// Drawing surface

Graphics G = This. pictureboxii1.creategraphics ();

// White

G. fillrectangle (brushes. White, this. clientrectangle );

// Get a drawing Path & para;

Graphicspath Gp = new graphicspath ();

// Add some set Images

GP. addellipse (20, 20,300,200 );

GP. addpie( 50,100,300,100, 45,200 );

GP. addrectangle (New rectangle (100, 30,100, 80 ));

// Draw the drawing path on the drawing surface

G. drawpath (pens. Blue, GP );

// Translation

G. translatetransform (200, 20 );

// Fill in the drawing Path & para;

G. fillpath (brushes. greenyellow, GP );

GP. Dispose ();

}
Region -- region class

You can create a region from an existing rectangle and path. Use the graphics. fillregion method to draw the region. This class indicates the interior of a rectangle and a graph shape composed of paths. This class cannot be inherited.

Gradient Filling

Two brushes are required:

Lineargradientbrush)

Pathguadientbrush)

Private void button4_click (Object sender, system. eventargs E)

{

// Drawing surface

Graphics G = This. pictureboxii1.creategraphics ();

G. fillrectangle (brushes. White, this. pictureboxii1.clientrectangle );

// Define a linear gradient brush

Lineargradientbrush lgbrush =

New lineargradientbrush (

New Point (0, 10 ),

New Point (150, 10 ),

Color. fromargb (255, 0, 0 ),

Color. fromargb (0,255, 0 ));

Pen = new pen (lgbrush );

// Draw a straight line segment and fill a rectangle with the pen with the linear paint gradient effect

G. drawline (pen, 10,130,500,130 );

G. fillrectangle (lgbrush, 10,150,370, 30 );

// Define the path and add an elliptic

Graphicspath Gp = new graphicspath ();

GP. addellipse (10, 10,200,100 );

// Use this path to define the path gradient brush

Pathgradientbrush brush =

New pathgradientbrush (GP );

// Color array

Color [] colors = {

Color. fromargb (255, 0, 0 ),

Color. fromargb (100,100,100 ),

Color. fromargb (0,255, 0 ),

Color. fromargb (0, 0,255 )};

// Define the color gradient Ratio

Float [] r = {0.0f, 0.3f, 0.6f, 1.0f };

Colorblend blend = new colorblend ();

Blend. colors = colors;

Blend. Positions = R;

Brush. interpolationcolors = blend;

// Fill a rectangle out of the ellipse

G. fillrectangle (brush, 0, 0,210,110 );

// Define the second path gradient brush with the path added with an elliptical shape

Graphicspath gp2 = new graphicspath ();

Gp2.addellipse (300, 0,200,100 );

Pathgradientbrush brush2 = new pathgradientbrush (gp2 );

// Set the center position and color

Brush2.centerpoint = new pointf (450, 50 );

Brush2.centercolor = color. fromargb (0,255, 0 );

// Set the border color

Color [] color2 = {color. fromargb (255, 0, 0 )};

Brush2.surroundcolors = color2;

// Fill the ellipse with the second gradient brush

G. fillellipse (brush2, 300, 0,200,100 );

}

GDI + Coordinate System

General coordinate system-User-Defined coordinate system.

Page coordinate system-virtual coordinate system.

Device coordinate system-screen coordinate system.

When the page coordinate system and the device coordinate system are in pixels, they are the same.

Private void button10_click (Object sender, system. eventargs E)

{

Graphics G = This. pictureboxii1.creategraphics ();

G. Clear (color. White );

This. Draw (g );

}
Private void draw (Graphics g)

{

G. drawline (pens. Black, 10, 10,100,100 );

G. drawellipse (pens. Black, 50, 50,200,100 );

G. drawarc (pens. Black, 100, 10,100,100, 20,160 );

G. drawrectangle (pens. green, 50,200,150,100 );

}

Private void button5_click (Object sender, system. eventargs E)

{

// Move left

Graphics G = This. pictureboxii1.creategraphics ();

G. Clear (color. White );

G. translatetransform (-50, 0 );

This. Draw (g );

}

Private void button6_click (Object sender, system. eventargs E)

{

// Shift right

Graphics G = This. pictureboxii1.creategraphics ();

G. Clear (color. White );

G. translatetransform (50, 0 );

This. Draw (g );

}

Private void button7_click (Object sender, system. eventargs E)

{

// Rotate

Graphics G = This. pictureboxii1.creategraphics ();

G. Clear (color. White );

G. rotatetransform (-30 );

This. Draw (g );

}

Private void button8_click (Object sender, system. eventargs E)

{

// Zoom in

Graphics G = This. pictureboxii1.creategraphics ();

G. Clear (color. White );

G. scaletransform (1.2f, 1.2f );

This. Draw (g );

}

Private void button9_click (Object sender, system. eventargs E)

{

// Zoom out

Graphics G = This. pictureboxii1.creategraphics ();

G. Clear (color. White );

G. scaletransform (0.8f, 0.8f );

This. Draw (g );

}

Global coordinate-transformation affects every element on the drawing surface. It is usually used to set the universal coordinate system.

The program will be moved to the control center, and the Y axis is forward up.

// Draw a circle first

Graphics G = E. graphics;

G. fillrectangle (brushes. White, this. clientrectangle );

G. drawellipse (pens. Black,-100,-100,200,200 );

// Make the Y axis forward up. The image relative to the X axis must be created.

// The transformation matrix is [, 0,-, 0].

Matrix MAT = new matrix (1, 0, 0,-1, 0, 0 );

G. Transform = MAT;

Rectangle rect = This. clientrectangle;

Int W = rect. width;

Int H = rect. height;

G. translatetransform (W/2,-H/2 );

// Centered on the origin, create a circle with a radius of 100

G. drawellipse (pens. Red,-100,-100,200,200 );

G. translatetransform (100,100 );

G. drawellipse (pens. Green,-100,-100,200,200 );

G. scaletransform (2, 2 );

G. drawellipse (pens. Blue,-100,-100,200,200 );

Local Coordinate System-only transforms some images, while other graphic elements remain unchanged.

Protected override void onpaint (painteventargs E)

{

Graphics G = E. graphics;

// Set the customer area to white

G. fillrectangle (brushes. White, this. clientrectangle );

// Y axis up

Matrix MAT = new matrix (1, 0, 0,-1, 0, 0 );

G. Transform = MAT;

// Move the coordinate origin to the center of the form

Rectangle rect = This. clientrectangle;

Int W = rect. width;

Int H = rect. height;

G. translatetransform (W/2,-H/2 );

 

 

// Draw an ellipse in global coordinates

G. drawellipse (pens. Red,-100,-100,200,200 );

G. fillrectangle (brushes. Black,-108, 0, 8, 8 );

G. fillrectangle (brushes. Black, 100, 0, 8, 8 );

G. fillrectangle (brushes. Black, 0,100, 8, 8 );

G. fillrectangle (brushes. Black, 0,-108, 8, 8 );

// Create an ellipse and perform transformation in the Local Coordinate System

Graphicspath Gp = new graphicspath ();

GP. addellipse (-100,-100,200,200 );

Matrix mat2 = new matrix ();

// Translation

Mat2.translate (150,150 );

// Rotate

Mat2.rotate (30 );

GP. Transform (mat2 );

G. drawpath (pens. Blue, GP );

Pointf [] P = GP. pathpoints;

G. fillrectangle (brushes. Black, P [0]. X-2, P [0]. Y + 2, 4, 4 );

G. fillrectangle (brushes. Black, P [3]. X-2, P [3]. Y + 2, 4, 4 );

G. fillrectangle (brushes. Black, P [6]. X-4, P [6]. Y-4, 4, 4 );

G. fillrectangle (brushes. Black, P [9]. X-4, P [9]. Y-4, 4, 4 );

GP. Dispose ();

// Base. onpaint (E );

}

Alpha hybrid

A of color. fromargb () is Alpha. The Alpha value ranges from 0 to 255. 0 indicates completely transparent, and 255 indicates completely opaque.

Current color = foreground color x alpha/255 + background color x (255-alpha)/255

Protected override void onpaint (painteventargs E)

{

Graphics G = E. graphics;

// Create a filled rectangle

Solidbrush brush = new solidbrush (color. bluevilet );

G. fillrectangle (brush, 180, 70,200,150 );

// Create a bitmap with transparency between the two bitmaps

Bitmap bm1 = new Bitmap (200,100 );

Graphics Bg1 = graphics. fromimage (bm1 );

Solidbrush redbrush =

New solidbrush (color. fromargb (210,255, 0, 0 ));

Solidbrush greenbrush =

New solidbrush (color. fromargb (210, 0,255, 0 ));

Bg1.fillrectangle (redbrush, 0, 0,150, 70 );

Bg1.fillrectangle (greenbrush, 30, 30,150, 70 );

G. drawimage (bm1, 100,100 );

// Create a bitmap with no transparency between the two bitmaps

Bitmap bm2 = new Bitmap (200,100 );

Graphics bg2 = graphics. fromimage (bm2 );

Bg2.compositingmode = compositingmode. sourcecopy;

Bg2.fillrectangle (redbrush, 0, 0,150,170 );

Bg2.fillrectangle (greenbrush, 30, 30,150, 70 );

G. compositingquality = compositingquality. gammacorrected;

G. drawimage (bm2, 300,200 );

// Base. onpaint (E );

}

Reverse sample

Protected override void onpaint (painteventargs E)

{

Graphics G = E. graphics;

// Zoom in 8 times

G. scaletransform (8, 8 );

// No reverse image or text

Draw (g );

// Set the reverse sample

G. smoothingmode = smoothingmode. antialias;

// Shift 40 to the right

G. translatetransform (40, 0 );

// Draw the image again.

Draw (g );

// Base. onpaint (E );

}
Private void draw (Graphics g)

{

// Draw graphics and text

G. drawline (pens. Gray, 10, 10, 40, 20 );

G. drawellipse (pens. Gray, 20, 20, 30, 10 );

String S = "reverse sample test ";

Font font = new font ("", 5 );

Solidbrush brush = new solidbrush (color. Gray );

G. drawstring (S, Font, brush, 10, 40 );

}

10 basic skills of GDI + programming)

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.