C # GDI + programming 10 basic tricks two

Source: Internet
Author: User
Tags border color
5. Gradient Fill

You need to use two brushes:

Linear gradient Brush (LinearGradientBrush)

Path gradient Brush (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 line 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 a 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 ratios

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

ColorBlend blend = new ColorBlend ();

Blend. Colors = Colors;

Blend. positions = R;

Brush. Interpolationcolors = blend;

Fills a rectangle outside the ellipse

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


Define a second path gradient brush with a path with an ellipse added

GraphicsPath GP2 = new GraphicsPath ();

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

PathGradientBrush brush2 = new PathGradientBrush (GP2);

Set center point position and color

Brush2. CenterPoint = new PointF (450, 50);

Brush2. Centercolor = Color.FromArgb (0, 255, 0);

Set Border color

Color[] Color2 = {Color.FromArgb (255, 0, 0)};

Brush2. Surroundcolors = Color2;

Fills the ellipse with a second gradient brush

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

{

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)

{

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

{

Rotating

Graphics g = this.pictureBoxII1.CreateGraphics ();

G.clear (Color.White);

G.rotatetransform (-30);

This. Draw (g);

}


private void Button8_click (object sender, System.EventArgs e)

{

Amplification

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)

{

Narrow

Graphics g = this.pictureBoxII1.CreateGraphics ();

G.clear (Color.White);

G.scaletransform (0.8f, 0.8f);

This. Draw (g);

}


7. Global coordinates-transformations can have an impact on each element on the drawing surface. Typically used to set a universal coordinate system.

The next program moves the original point to the center of the control, and the y-axis is facing upward.

Draw a circle first

Graphics g = e.graphics;

G.fillrectangle (Brushes.white, this. ClientRectangle);

G.drawellipse (Pens.black,-100,-100, 200, 200);


To make the y-axis forward upward, 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);


Make a circle with a radius of 100, centered on the origin.

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 some graphics are transformed, while other graphic elements are unchanged.


protected override void OnPaint (PaintEventArgs e)

{

Graphics g = e.graphics;

Customer area set to White

G.fillrectangle (Brushes.white, this. ClientRectangle);

Y-axis facing up

Matrix mat = new Matrix (1, 0, 0,-1, 0, 0);

G.transform = Mat;

Move coordinate origin to form 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 transform it in a local coordinate system

GraphicsPath GP = new GraphicsPath ();

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

Matrix MAT2 = new Matrix ();

Translation

Mat2. Translate (150, 150);

Rotating

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


A of Color.FromArgb () is Alpha. The alpha value ranges from 0 to 255. 0 means full transparency and 255 is completely opaque.


Current color = foreground color xalpha/255+ background x (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);

Creates a bitmap with a transparency 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, 255, 0));

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

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

G.drawimage (BM1, 100, 100);

Creates a bitmap where there are no transparency effects between 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);

}

10. Anti-aliasing


protected override void OnPaint (PaintEventArgs e)

{

Graphics g = e.graphics;

Zoom in 8 times times

G.scaletransform (8, 8);

No anti-aliasing graphics and text

Draw (g);


Set anti-aliasing

G.smoothingmode = Smoothingmode.antialias;


Move Right 40

G.translatetransform (40, 0);

That's after the 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 ("Arial", 5);

SolidBrush brush = new SolidBrush (Color.gray);


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

}



Related Article

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.