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
-
Bitmap Class
Represents a bitmap image. It provides methods for loading, saving, and manipulating image data.
-
Graphics Class
Provides methods and properties to create custom graphics and perform drawing operations.
-
Color Struct
Represents an RGBA color value.
-
Pen Class
Represents a graphic object used to draw lines and curves.
-
Brush Class
Abstract base class for all brushes, which are used to fill areas of graphics.
-
Font Class
Represents a typographic character collection (a font) with a specific style and size.
-
Point Struct
Represents an ordered pair of x and y coordinates that defines a point.
-
Rectangle Struct
Represents a drawing surface defined by a location and size.
Common Operations
Drawing a Line
Use the Graphics
class to draw lines on a surface.
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.
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.
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.
- Bitmap(string filename): Creates a
Bitmap
from a file. - Bitmap(int width, int height): Creates a blank
Bitmap
with specified dimensions. - Color GetPixel(int x, int y): Gets the color of the pixel at the specified coordinates.
- void SetPixel(int x, int y, Color color): Sets the color of the pixel at the specified coordinates.
- void Save(string filename): Saves the image to a file.
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
.
- void DrawLine(Pen pen, Point pt1, Point pt2): Draws a line between two points.
- void DrawRectangle(Pen pen, int x, int y, int width, int height): Draws a rectangle.
- void FillEllipse(Brush brush, int x, int y, int width, int height): Fills an ellipse within a bounding rectangle.
- void DrawImage(Image image, Point point): Draws an image at the specified location.
Color Struct Details
Represents colors using RGBA (Red, Green, Blue, Alpha) values. Provides predefined color constants and methods for color manipulation.
- static Color Red { get; }: Gets a system-defined color.
- static Color FromArgb(int red, int green, int blue): Creates a
Color
from RGB values. - static Color FromArgb(int alpha, Color baseColor): Creates a
Color
with an alpha value applied to a base color.