search
HomeBackend DevelopmentC#.Net Tutorial10 basic skills for GDI+ programming in C# 2

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
C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)