System.Drawing
.NET Framework Class Library
Introduction
The System.Drawing
namespace provides access to basic graphics functionality. It enables you to draw shapes, text, and images on the screen or to files. This namespace contains classes for working with graphics contexts, bitmaps, metafiles, fonts, and colors.
Key Concepts
- Graphics Objects: Objects that represent a drawing surface, such as a form, a control, or a bitmap.
- Pens and Brushes: Used to define how lines and fills are drawn. Pens draw outlines, while brushes fill areas.
- Colors: Represented by the
Color
structure, which supports ARGB (Alpha, Red, Green, Blue) values. - Fonts: Defined by the
Font
class, specifying typeface, size, and style. - Bitmaps and Images: Used to load, manipulate, and save raster images.
Classes
- Graphics Represents a drawing surface.
- Pen Defines a Pen object that is used to draw lines and curves, or to display shapes.
- Brush Provides an abstract base class for creating objects used to fill the interior of a graphical shape.
- SolidBrush Specifies a set of RGBA and alpha values that represent a color and a SolidBrush object used to fill shapes.
- LinearGradientBrush Represents a gradient that is defined by a starting point, an ending point, and colors.
- Font Defines a particular format for text, including the font family, size, and style.
- FontFamily Specifies a name that represents a GDI character set.
- Point Represents an ordered pair of x and y coordinates that define a point in two-dimensional space.
- PointF Represents an ordered pair of floating-point x and y coordinates that define a point in two-dimensional space.
- Size Represents an ordered pair of integers that represent the width and height of a rectangle, and is used throughout GDI+.
- SizeF Represents an ordered pair of floating-point numbers that represent the width and height of a rectangle.
- Rectangle Defines a set of four integers that represent the location and size of a rectangle.
- RectangleF Defines a set of four floating-point numbers that represent the location and size of a rectangle.
- Color Represents an ARGB (alpha, red, green, blue) color.
- Bitmap Represents an object that is composed of pixel data for image manipulation.
- Image Provides functionality to load, save, and manage image objects.
Example Usage
Here's a simple example of how to draw a red line on a control using the System.Drawing
namespace:
using System;
using System.Drawing;
using System.Windows.Forms;
public class DrawingForm : Form
{
public DrawingForm()
{
this.Paint += new PaintEventHandler(DrawingForm_Paint);
this.Size = new Size(400, 300);
this.Text = "System.Drawing Example";
}
private void DrawingForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// Create a red pen with a thickness of 2 pixels
using (Pen redPen = new Pen(Color.Red, 2))
{
// Define the start and end points of the line
Point startPoint = new Point(50, 50);
Point endPoint = new Point(300, 150);
// Draw the line
g.DrawLine(redPen, startPoint, endPoint);
}
// Draw some text
using (Font drawFont = new Font("Arial", 16))
using (SolidBrush drawBrush = new SolidBrush(Color.Blue))
{
g.DrawString("Hello, System.Drawing!", drawFont, drawBrush, new Point(50, 200));
}
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DrawingForm());
}
}