GDI + programming 10 basic skills!

xiaoxiao2021-04-09  497

GDI programming 10 basic skills

Create a drawing surface

There are two common methods for creating a drawing surface. Try Now to get the drawing surface of the Picturebox.

Private Void Form1_Load (Object Sender, System.EventArgs E)

{

/ / Get drawing surface of Picturebox1

Graphics g = this.picturebox1.creategraphics ();

}

Private void PictureBox1_paint (Object Sender, System.Windows E), PRAINTERGS E)

{

/ / Get drawing surface of Picturebox1

Graphics g = E.Graphics;

}

Various graphics patterns can be drawn using Graphics objects. The Paint events of the control and the onpaint method can be drawn. The pattern is drawn in the onPaint method must have a Graphics property from the parameter e. Here 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 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 Button1_Click (Object Sender, System.Eventargs E)

{

This.PictureBoxii1.createGraphics (). Fillellipse

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

}

Three classes related to text:

FontFamily - defines a similar basic design but has some of the forms of differences in the form. Unable to inherit this class.

Font - defines a specific text format, including fonts, font numbers, and glyph properties. Unable to inherit this class.

StringFormat - Package text layout information (such as alignment and line spacing), display operation (such as omitted number insertion and national standard) and OpenType functionality. Unable to inherit this class.

The following program shows a paragraph.

Private void button2_click (Object Sender, System.Eventargs E)

{

Graphics g = this.pictureboxi1.creategraphics ();

g.fillRectangle (Brushes.White, this.PictureBoxii1.clientRectangle);

String s = "aaaaaaaaaaaaaaaaaaaaaaaaa";

Fontfamily fm = new fontfamily ("ëîìå");

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);

}

GDI path - GraphicsPath class

The GraphicsPath class provides a range of attributes and methods that use it to get the key points on the path, which can add a straight segment, round and other geometric elements. Enclosed rectangles can be obtained to pick up test. How to use these features, take a closer look.

Private void button3_click (Object Sender, System.Eventargs E)

{

// Draw surface

Graphics g = this.pictureboxi1.creategraphics ();

// Fill in white

g.fillRectangle (Brushes.White, this.clientRectangle);

// Get a drawing path

Graphicspath gp = new graphicspath ();

// Add some collection graphics

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

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

GP.AddRectangle (New Rectangle (100, 30, 100, 80));

// Draw a drawing path on the drawing surface

g.drawpath (Pens.Blue, GP);

// Translation

G.TranslateTransform (200, 20);

// Fill the drawing path

g.fillpath (brushes.greenyellow, gp);

gp.dispose ();

}

Area-Agion class

You can create region from existing rectangles and paths. Draw the Region using the Graphics.FillRegion method. This class indicates the inside of the graphical shape made of rectangles and by the path. Unable to inherit this class.

Gradient filling

Two brushes need to be used:

Linear gradient brush (lineargradientbrush)

Path Gradient Brush (PathguadientBrush)

Private void button4_click (Object Sender, System.Eventargs E)

{

// Draw surface

Graphics g = this.pictureboxi1.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 Pen = New Pen (LGBRUSH);

// Draw a straight segment and fill a rectangle with a pen brush gradient effect

g.drawline (Pen, 10, 130, 500, 130);

G.FillRectangle (LGBrush, 10, 150, 370, 30);

/ / Define the path and add an ellipse

Graphicspath gp = new graphicspath ();

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

// Define the path gradient brush with this path

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 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 outside the ellipse

G.FillRectangle (Brush, 0, 0, 210, 110);

// Define the second path gradient brush with the path added to the ellipse

Graphicspath gp2 = new graphicspath ();

GP2.Addellipse (300, 0, 200, 100);

PathGradientbrush brush2 = new pathgradientbrush (gp2);

/ / Set the center point 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.ssurroundcolors = color2;

// Fill the ellipse with the second gradient

g.fillellipse (Brush2, 300, 0, 200, 100);

}

GDI coordinate system

General coordinate system - User-defined coordinate system.

Page Coordinate System - Virtual Coordinate System.

Equipment coordinate system - screen coordinate system.

When the unit of the page coordinate system and device coordinate system is pixel, they are the same.

Private void button10_click (Object Sender, System.EventArgs E)

{

Graphics g = this.pictureboxi1.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)

{

// Left shift

Graphics g = this.pictureboxi1.creategraphics ();

g.clear (color.white);

G.TranslateTransform (-50, 0);

THIS.DRAW (g);

}

Private void button6_click (Object Sender, System.Eventargs E)

{

// right movement

Graphics g = this.pictureboxi1.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.pictureboxi1.creategraphics ();

g.clear (color.white);

G.RotateTransform (-30);

THIS.DRAW (g);

}

Private void button8_click (Object Sender, System.EventArgs E)

{

//enlarge

Graphics g = this.pictureboxi1.creategraphics ();

g.clear (color.white);

G.scaletransform (1.2F, 1.2F);

THIS.DRAW (g);

}

Private void button9_click (Object Sender, System.Eventargs E)

{

/ / Reduce

Graphics g = this.pictureboxi1.creategraphics ();

g.clear (color.white);

G.scaletransform (0.8F, 0.8F);

THIS.DRAW (g);

}

Global Coordinates - Transforms will have an impact on each of the drawings. Usually used to set a universal coordinate system.

The program moves to the center to the center of the control, and the Y axis is forwarded.

// Painting a circle

Graphics g = E.Graphics;

g.fillRectangle (Brushes.White, this.clientRectangle);

g.drawellipse (Pens.Black, -100, -100, 200, 200);

// Make the Y axis to the top, must do a relative to X-axis mirroring

/ / The transformation matrix is ​​[1, 0, 0, -1, 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);

/ / Take the origin as the center, make a circle having 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 - transforms only some graphics, while other graphical elements are unchanged.

Protected Override Void Onpaint (Painteventargs E)

{

Graphics g = E.Graphics;

/ / The client area is set to white

g.fillRectangle (Brushes.White, this.clientRectangle);

// Y axis

Matrix Mat = New Matrix (1, 0, 0, -1, 0, 0);

g.Transform = Mat;

// Mobile coordinate original to the center

Rectangle Rect = this.clientRectangle;

INT w = Rect.width;

INT H = Rect.height;

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

// Draw an ellipse under 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 then transform 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 mix

The color.fromargb () is Alpha. The value of Alpha is fully transparent from 0 to 255.0, and 255 is completely opaque.

Current color = foreground color × alpha / 255 background color × (255-alpha) / 255

Protected Override Void Onpaint (Painteventargs E)

{

Graphics g = E.Graphics;

// Create a fill rectangle

Solidbrush brush = new solidbrush (color.blueviolet);

G. FillRectangle (Brush, 180, 70, 200, 150);

// Create a bitmap, there is a transparent effect between 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, 25, 0));

BG1.FillRectangle (Redbrush, 0, 0, 150, 70);

BG1.FillRectangle (Greenbrush, 30, 30, 150, 70);

G. DrawImage (BM1, 100, 100);

// Create a bitmap, there is no transparent effect 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.gammacorred;

g.drawImage (BM2, 300, 200);

//base.onpaint (e);

}

Go away

Protected Override Void Onpaint (Painteventargs E)

{

Graphics g = E.Graphics;

// Zoom 8 times

G.scaletransform (8, 8);

/ / No refused to go to the graphics and text

DRAW (g);

// Set the reverse

g.smoothingmode = smoothingmode.antialias;

// Right shift 40

G.TranslateTransform (40, 0);

// Draw it again after it is afraid.

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 Walking Test";

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

Solidbrush Brush = New Solidbrush (Color.gray);

g.drawstring (S, Font, Brush, 10, 40);

}

Finished. Said to make so much first. It will be supplemented in the future.

转载请注明原文地址:https://www.9cbs.com/read-133055.html

New Post(0)