Home Backend Development C#.Net Tutorial 10 basic skills for GDI+ programming in C# 2

10 basic skills for GDI+ programming in C# 2

Dec 17, 2016 am 09:53 AM

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the role of char in C strings What is the role of char in C strings Apr 03, 2025 pm 03:15 PM

In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

How to handle special characters in C language How to handle special characters in C language Apr 03, 2025 pm 03:18 PM

In C language, special characters are processed through escape sequences, such as: \n represents line breaks. \t means tab character. Use escape sequences or character constants to represent special characters, such as char c = '\n'. Note that the backslash needs to be escaped twice. Different platforms and compilers may have different escape sequences, please consult the documentation.

How to use various symbols in C language How to use various symbols in C language Apr 03, 2025 pm 04:48 PM

The usage methods of symbols in C language cover arithmetic, assignment, conditions, logic, bit operators, etc. Arithmetic operators are used for basic mathematical operations, assignment operators are used for assignment and addition, subtraction, multiplication and division assignment, condition operators are used for different operations according to conditions, logical operators are used for logical operations, bit operators are used for bit-level operations, and special constants are used to represent null pointers, end-of-file markers, and non-numeric values.

The difference between char and wchar_t in C language The difference between char and wchar_t in C language Apr 03, 2025 pm 03:09 PM

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

How to convert char in C language How to convert char in C language Apr 03, 2025 pm 03:21 PM

In C language, char type conversion can be directly converted to another type by: casting: using casting characters. Automatic type conversion: When one type of data can accommodate another type of value, the compiler automatically converts it.

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

How to use char array in C language How to use char array in C language Apr 03, 2025 pm 03:24 PM

The char array stores character sequences in C language and is declared as char array_name[size]. The access element is passed through the subscript operator, and the element ends with the null terminator '\0', which represents the end point of the string. The C language provides a variety of string manipulation functions, such as strlen(), strcpy(), strcat() and strcmp().

See all articles