Home  >  Article  >  Backend Development  >  10 basic skills for GDI+ programming in C# 2

10 basic skills for GDI+ programming in C# 2

高洛峰
高洛峰Original
2016-12-17 09:53:121025browse

5. Gradient color filling

You need to use two brushes:

LinearGradientBrush(LinearGradientBrush)

PathGuadientBrush(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 pen = new Pen(lgbrush);


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

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

//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 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 a 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.SurroundColors = color2;

//Use the second gradient brush to fill the ellipse

g.FillEllipse(brush2, 300, 0, 200, 100);

}


6, GDI+ Coordinate system


Universal coordinate system - user-defined coordinate system.

Page coordinate system - virtual coordinate system.
Device coordinate system - screen coordinate system.


When the units of the page coordinate system and the device coordinate system are both pixels, they are the same.

private void button10_Click(object sender, System.EventArgse)

{

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)

{

//Shift 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)

{

//Enlarge

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)

{

//Reduce

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.ScaleTransform(0.8f, 0.8f);

this.Draw(g) ;

}


7. Global coordinates - transformation will have an impact on every primitive on the drawing surface. Usually used to set a universal coordinate system.

The following program moves the original fixed point to the center of the control, and the Y-axis is facing upward.

//First draw a circle

Graphics g = e.Graphics;

g.FillRectangle(Brushes.White, this.ClientRectangle);

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


//To make the y-axis point forward, it must be mirrored relative to the x-axis

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


//With the origin as the center, make 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 );

8. Local coordinate system - only certain graphics are transformed, while other graphic elements remain 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 is facing 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 transform it in the local coordinate system

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse(-100, -100, 200, 200);

Matrix mat2 = new Matrix() ;

//Translation

mat2.Translate(150, 150);

//Rotation

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

}


9. Alpha blending


The A of Color.FromArgb() is Alpha. Alpha values ​​range from 0 to 255. 0 means completely transparent and 255 means 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 filled rectangle

SolidBrush brush = new SolidBrush(Color.BlueViolet);

g.FillRectangle(brush, 180, 70, 200, 150);

//Create a bitmap between two bitmaps Has transparency effect

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 effect between the two bitmaps

Bitmap bm2 = new Bitmap( 200, 100); aGraphics BG2 = Graphics.fromimage (BM2); 0, 170); gBg2.fillrectangle ( greenBrush, 30, 30, 150, 70);

g.CompositingQuality = CompositingQuality.GammaCorrected;

g.DrawImage(bm2, 300, 200);

//base.OnPaint (e);

}

10. Anti-aliasing


protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//Enlarge 8 times


g.ScaleTransform(8, 8);

// No anti-aliasing graphics and text

Draw(g);

//Set anti-aliasing

g.SmoothingMode = SmoothingMode.AntiAlias;

//Shift right by 40

g.TranslateTransform(40, 0) ;

//Drawing is after anti-aliasing

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 = "Anti-aliasing test" ;

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

SolidBrush brush = new SolidBrush(Color.Gray);

g.DrawString(s, font, brush, 10, 40);

}


For more 10 basic skills of GDI+ programming in C# 2. For related articles, please pay attention to PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn