GDI + Drawing
- Introduction
- System.Drawing namespace
-
- Common classes in the System.Drawing namespace:
- Common structures in the System.Drawing namespace:
- Graphics class
- Pen class
- Brush class
- Font class
- PictureBox image control
- Bitmap class
Introduction
GDI (Graphics Device Interface) image device interface, which is a drawing API. The System.Drawing namespace provides access to the basic graphics functions of GDI+.
System.Drawing namespace
Common classes in the System.Drawing namespace:
Class | Description |
---|---|
Bitmap | Encapsulates a GDI+ bitmap that consists of the pixel data of a graphics image and its properties. |
Brush | Brush, defines internal objects used to fill graphic shapes (such as rectangles, ellipses, pie charts, polygons and closed paths) |
Font | Font, defines a specific text format, including: font, font size and font style attributes. This class cannot be inherited |
Graphics | Encapsulates a GDI+ drawing surface. This class cannot be inherited |
Pen | Brush, which defines an object used to draw straight lines and curves. This class cannot be inherited |
Region | Indicates the interior of a graphic shape composed of a rectangle and a path. This class cannot be inherited |
Common structures in the System.Drawing namespace:
Structure | Description |
---|---|
Color | Represents RGB color |
Point | Represents an ordered pair of points, integer X and Y coordinates defined in a two-dimensional plane |
Rectangle | Stores a set of 4 integers, representing the position and size of a rectangle |
Size | Stores an ordered pair of integers, usually the width and height of a rectangle |
Graphics class
The Graphics class encapsulates a GDI + drawing interface and provides methods for drawing objects to display devices.
Common methods of the Graphics class:
Name | Description |
---|---|
Dispose | Releases all resources used by Graphics |
DrawEllipse | Draws an ellipse defined by a border specified by a pair of coordinates, height and width |
DrawArc | Draw an arc |
DrawLine | Draw a straight line |
td> | |
DrawPolygon | Draw a polygon defined by a set of Point structures |
DrawRectangle | Draw a rectangle specified by a coordinate pair, width and height |
DrawPie | Draw a sector shape specified by a coordinate pair, width, height and two The ellipse definition specified by the sky ray |
DrawCurse | Draw a curve, specified by the parameter Point array |
FillEllips | Fills the interior of an ellipse defined by a border specified by a pair of coordinates, a width and a height |
FillRegion | Fill the interior of the Region |
ScaleTransform | Apply the specified scaling operation to this Graphics |
TanslateTransform | Translation coordinate system origin |
Pen class
The Pen class can set the color of the brush, the thickness of the line and the style of the line (realization, dashed line, etc.). The brush is the tool for drawing, and the Graphics object is the place for drawing.
Brush class
Brush class (brush), used to fill graphics. This class is an abstract base class and cannot be instantiated directly.
Derived classes of the Brush class:
Name | Description |
---|---|
ImageBrush | Image drawing area |
LinearGradientBrush | Linear gradient drawing area |
RadialGradientBrush | Radial gradient drawing area, the focus defines the beginning of the gradient, and the ellipse defines the focus of the gradient |
SolidColorBrush | Single color drawing area |
VideoBrush | Video content drawing area |
Font class
Font class, when drawing text, you can set the font style, size, and font type.
using System.Drawing.Drawing2D; using System.Numerics; using System.Security.Cryptography.Xml; using System.Threading; namespace GDIDemo {<!-- --> public partial class Form1 : Form {<!-- --> public Form1() {<!-- --> InitializeComponent(); } /// <summary> /// Draw a straight line /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Black); g.DrawLine(p, 0, this.Height / 2, this.Width, this.Height / 2); p.Dispose(); g.Dispose(); } /// <summary> /// Draw a rectangle /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 50, 50, 200, 200); p.Dispose(); g.Dispose(); } /// <summary> /// Draw a circle /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Blue); g.DrawEllipse(p, 50, 50, 100, 100); p.Dispose(); g.Dispose(); } /// <summary> /// Draw a cylinder /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) {<!-- --> int height = this.ClientSize.Height - 100; int width = this.ClientSize.Width - 50; int vHeight = 200; int vWidth = 100; Graphics g = this.CreateGraphics(); g.Clear(Color.White); Pen pen = new Pen(Color.Gray); SolidBrush brush = new SolidBrush(Color.Gainsboro); for (int i = height / 2; i > 0; i--) {<!-- --> g.DrawEllipse(pen, width / 2, i, vHeight, vWidth); } g.FillEllipse(brush, width / 2, 0, vHeight, vWidth); } /// <summary> /// Draw a filled rectangle /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); Pen pen = new Pen(Color.Blue); Brush brush = pen.Brush; Rectangle rect = new Rectangle(50, 50, 100, 100); g.FillRectangle(brush, rect); pen.Dispose(); g.Dispose(); } /// <summary> /// Draw a gradient circle /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); Rectangle rect = new Rectangle(150, 50, 200, 200); LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Purple, 90); g.FillEllipse(brush, rect); brush.Dispose(); g.Dispose(); } /// <summary> /// Draw text /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button7_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); Pen pen = new Pen(Color.Blue); Font f = new Font("official script", 24, FontStyle.Italic); g.DrawString("Windows Application Design", f, pen.Brush, 50, 50); pen.Dispose(); g.Dispose(); } private void button9_Click(object sender, EventArgs e) {<!-- --> using (Graphics g = this.CreateGraphics()) {<!-- --> g.Clear(Color.White); } } private void Form1_Paint(object sender, PaintEventArgs e) {<!-- --> #if false Graphics g = this.CreateGraphics(); g.Clear(Color.White); Pen myPen = new Pen(Color.Red, 3); g.DrawRectangle(myPen, 0, 0, 200, 100); g.DrawEllipse(myPen, 0, 0, 200, 100); g.Dispose(); myPen.Dispose(); #endif } /// <summary> /// Coordinate translation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button8_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); g.Clear(Color.White); Pen myPen = new Pen(Color.Red, 3); g.DrawRectangle(myPen, 0, 0, 200, 100); g.DrawEllipse(myPen, 0, 0, 200, 100); Thread.Sleep(1000); g.Clear(Color.White); g.TranslateTransform(30, 30); g.DrawRectangle(myPen, 0, 0, 200, 100); g.DrawEllipse(myPen, 0, 0, 200, 100); g.Dispose(); myPen.Dispose(); } /// <summary> /// Coordinate scaling /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button10_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); g.Clear(Color.White); Pen myPen = new Pen(Color.Red, 3); g.DrawRectangle(myPen, 0, 0, 200, 100); g.DrawEllipse(myPen, 0, 0, 200, 100); Thread.Sleep(1000); g.Clear(Color.White); g.ScaleTransform(1.5f, 2.0f); // The horizontal axis is enlarged to 1.5 times and the vertical axis is enlarged to 2 times g.DrawRectangle(myPen, 0, 0, 200, 100); g.DrawEllipse(myPen, 0, 0, 200, 100); g.Dispose(); myPen.Dispose(); } /// <summary> /// Draw waveform /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button11_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); g.Clear(Color.White); Pen pen = new Pen(Color.Blue, 3); Point p1 = new Point(30, this.ClientSize.Height - 200); Point p2 = new Point(this.ClientSize.Width - 100, this.ClientSize.Height - 200); Point p3 = new Point(30, 30); g.DrawLine(pen, p1, p2); g.DrawLine(pen, p1, p3); Font f = new Font("宋体", 12, FontStyle.Bold); g.DrawString("x", f, pen.Brush, p2); g.DrawString("y", f, pen.Brush, 10, 10); double x1, x2, y1, y2, a; x1 = x2 = y1 = y2 = 0; y2 = this.ClientSize.Height - 200; for (x2 = 0; x2 <this.ClientSize.Width; x2 + + ) {<!-- --> a = 2 * Math.PI * x2 / this.ClientSize.Width; y2 = Math.Sin(a); y2 = (1 - y2) * (this.ClientSize.Height - 200) / 2; g.DrawLine(pen, (int)(x1 + 30), (int)y1, (int)(x2 + 30), (int)y2); x1 = x2; y1 = y2; } } /// <summary> /// Draw a pie chart /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button12_Click(object sender, EventArgs e) {<!-- --> Graphics g = this.CreateGraphics(); Rectangle rect = new Rectangle(50, 50, 200, 100); Brush brush = new SolidBrush(Color.Blue); g.FillPie(brush, rect, 0, 60); g.FillPie(brush, rect, 60, 150); brush = new SolidBrush(Color.Yellow); g.FillPie(brush, rect, 210, 150); brush.Dispose(); g.Dispose(); } } }
PictureBox image control
PictureBox control, the picture box is a basic control for operating graphics and images. It is mainly used to display and save graphics and image information. Its main attribute methods are:
- Attributes:
- Image: Set or get the image displayed with this control
- SizeMode: Indicates how to display the image
- method:
- Load: display image
Bitmap class
Bitmap class encapsulates GDI + bitmap. This bitmap consists of pixel data of the graphic image and its attributes. Bitmap is an object used to process images defined by pixel data. Its common properties and methods are:
Name | Description |
---|---|
Size | Get the width and height of the image in pixels |
Width | Get the width of the image |
Height | Get the height of the image |
FromFile() | Create an image from the specified file |
FromStream() | Create an image from the specified data stream |
GetPixel | Get this Bitmap Specify the color of the pixel |
MakeTransparent | Make the default transparent color transparent to this Bitmap |
Save | Save this image in the specified format to the specified stream |
RotateFlip | Rotate, mirror, or both rotate and mirror Image |
/// <summary> /// Create a bitmap and save it as a picture /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button13_Click(object sender, EventArgs e) {<!-- --> Bitmap bm = new Bitmap(this.ClientSize.Width, this.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bm); Font f = new Font("official script", 24, FontStyle.Italic); Pen pen = new Pen(Color.OrangeRed); g.DrawString("Create bitmap test save image", f, pen.Brush, 0, 0); pictureBox1.Image = bm; pictureBox1.Show(); bm.Save("c:\temp\1.bmp"); pen.Dispose(); g.Dispose(); } /// <summary> /// Load the image and save the image /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button14_Click(object sender, EventArgs e) {<!-- --> OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "Image file *.bmp|*.bmp|*.jpg|*.png"; if (openfile.ShowDialog() == DialogResult.OK) {<!-- --> pictureBox1.Image = Image.FromFile(openfile.FileName); pictureBox1.Image.Save("c:\temp\2.bmp"); } }