System.Drawing Namespace

Provides access to basic graphics functionality. This namespace includes classes for drawing graphics, managing images, and working with colors, fonts, and visual styles.

Key Classes

Common Operations

Drawing a Line

Use the Graphics class to draw lines on a surface.

public void DrawLine(Pen pen, int x1, int y1, int x2, int y2)

Draws a line connecting two points.

using System.Drawing;

// Assuming 'g' is an instance of the Graphics class
Pen bluePen = new Pen(Color.Blue, 2);
g.DrawLine(bluePen, 10, 10, 100, 100);

Drawing Text

Use the Graphics.DrawString method to render text.

public void DrawString(string s, Font font, Brush brush, PointF point)

Draws the specified string, font, and brush.

using System.Drawing;

Font arialFont = new Font("Arial", 16);
SolidBrush solidBrush = new SolidBrush(Color.Black);
g.DrawString("Hello, Graphics!", arialFont, solidBrush, new PointF(50, 50));

Working with Images

Load and display images using the Bitmap class.

public Bitmap(string filename)

Initializes a new instance of the Bitmap class from the specified file.

using System.Drawing;

Bitmap myBitmap = new Bitmap("path/to/your/image.jpg");
g.DrawImage(myBitmap, new Point(10, 10));

Bitmap Class Details

The Bitmap class represents an image object, allowing for pixel-level manipulation.

Graphics Class Details

The Graphics class is the central object for all drawing operations. You typically obtain a Graphics object from a control's CreateGraphics() method or from a Bitmap.

Color Struct Details

Represents colors using RGBA (Red, Green, Blue, Alpha) values. Provides predefined color constants and methods for color manipulation.