MSDN Documentation

Introduction to .NET Graphics

.NET provides a rich set of libraries for creating and manipulating graphics. Whether you're developing desktop applications, web applications, or games, .NET offers powerful tools to render visuals, handle images, and create sophisticated graphical user interfaces.

The primary namespaces for graphics operations in .NET are:

  • System.Drawing: For 2D graphics operations, often referred to as GDI+ (Graphical Device Interface+).
  • System.Windows.Media: For advanced graphics features in WPF (Windows Presentation Foundation), including vector graphics, animations, and multimedia.
  • System.Numerics: For vector and matrix types used in 3D graphics.
  • Microsoft.Xna.Framework: A framework for developing games on various platforms.

GDI+ (.NET Framework & Windows Forms)

GDI+ is the foundation for 2D graphics in .NET. It provides classes for drawing lines, curves, shapes, text, and images on various output surfaces like screens, printers, and metafile devices.

Key classes include:

  • Graphics: Represents a drawing surface.
  • Pen: Defines colors, widths, and styles for lines and outlines.
  • Brush: Defines how to fill shapes (e.g., SolidBrush, TextureBrush).
  • Bitmap: Represents an image loaded from a file or created in memory.
  • Point, Size, Rectangle: Structures for geometric representation.

Example: Drawing a Line


using System.Drawing;

// Assuming 'e.Graphics' is the Graphics object from a Paint event
Graphics g = e.Graphics;
Pen redPen = new Pen(Color.Red, 2); // Red pen with thickness 2
g.DrawLine(redPen, 10, 10, 100, 100);
redPen.Dispose();
                

Drawing Basic Shapes

You can easily draw common geometric shapes using methods on the Graphics object.

  • DrawRectangle(), FillRectangle()
  • DrawEllipse(), FillEllipse()
  • DrawLine(), DrawLines()
  • DrawPolygon(), FillPolygon()
  • DrawArc(), FillPie()

Example: Drawing a Rectangle and Ellipse


using System.Drawing;

Graphics g = e.Graphics;
Pen blackPen = new Pen(Color.Black, 1);
Brush blueBrush = new SolidBrush(Color.Blue);

// Draw a rectangle
g.DrawRectangle(blackPen, 50, 50, 150, 100);

// Fill an ellipse within the rectangle bounds
g.FillEllipse(blueBrush, 50, 50, 150, 100);

blackPen.Dispose();
blueBrush.Dispose();
                

Image Manipulation

.NET allows you to load, display, and manipulate images effectively. The Bitmap class is central to this, and you can perform operations like resizing, cropping, and applying effects.

  • Loading images from files: Bitmap.FromFile()
  • Saving images: bitmap.Save()
  • Drawing images: Graphics.DrawImage()
  • Pixel manipulation: Accessing individual pixels via Bitmap.GetPixel() and Bitmap.SetPixel(), or using LockBits() for performance.

Example: Loading and Drawing an Image


using System.Drawing;

Image myImage = Image.FromFile("path/to/your/image.jpg");
Graphics g = e.Graphics;

// Draw the image at a specific location
g.DrawImage(myImage, new Point(200, 20));

// Draw the image scaled to a new size
g.DrawImage(myImage, new Rectangle(200, 150, 200, 100));

myImage.Dispose();
                

Text Rendering

Displaying text is a common requirement. .NET provides the Font and StringFormat classes to control how text is drawn.

  • Graphics.DrawString(): The primary method for rendering text.
  • Font: Specifies the typeface, style, and size of the text.
  • StringFormat: Controls alignment, line spacing, wrapping, and hot-tracking.

Example: Drawing Text with Formatting


using System.Drawing;

Graphics g = e.Graphics;
Font arialFont = new Font("Arial", 16, FontStyle.Bold);
Brush blackBrush = new SolidBrush(Color.Black);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

Rectangle textRect = new Rectangle(10, 10, 200, 50);
g.DrawString("Hello, .NET Graphics!", arialFont, blackBrush, textRect, sf);

arialFont.Dispose();
blackBrush.Dispose();
sf.Dispose();
                

Transformations

You can apply transformations (translation, scaling, rotation, skewing) to the coordinate system of a Graphics object to manipulate drawing operations.

  • Graphics.TranslateTransform()
  • Graphics.ScaleTransform()
  • Graphics.RotateTransform()
  • Graphics.MultiplyTransform()

Example: Rotating a Shape


using System.Drawing;

Graphics g = e.Graphics;
Pen bluePen = new Pen(Color.Blue, 3);
Rectangle rect = new Rectangle(10, 10, 100, 50);

// Save the current graphics state
System.Drawing.Drawing2D.Matrix originalMatrix = g.Transform;

// Rotate around the center of the rectangle
PointF center = new PointF(rect.Left + rect.Width / 2f, rect.Top + rect.Height / 2f);
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(45); // Rotate by 45 degrees
g.TranslateTransform(-center.X, -center.Y);

// Draw the rectangle in its transformed state
g.DrawRectangle(bluePen, rect);

// Restore the original graphics state
g.Transform = originalMatrix;

bluePen.Dispose();
                

3D Graphics

For 3D graphics, .NET leverages underlying graphics APIs like DirectX or OpenGL. While direct .NET wrappers might be limited, frameworks like Unity or libraries like SharpDX provide comprehensive 3D capabilities.

In WPF, you can use the Viewport3D element and geometry primitives for 3D rendering.

XAML Graphics (WPF)

Windows Presentation Foundation (WPF) uses XAML (eXtensible Application Markup Language) to define rich graphical interfaces. It supports vector graphics, animations, transformations, and a retained mode graphics model.

Key XAML elements for graphics include:

  • , , for layout.
  • , , , , for shapes.
  • for displaying images.
  • , for text.
  • , for programmatic drawing.

API Summary

Commonly Used Classes and Interfaces

Namespace Class/Interface Description
System.Drawing Graphics Represents a GDI+ drawing surface.
System.Drawing Pen Defines properties for drawing lines and curves.
System.Drawing Brush Abstract base class for filling shapes.
System.Drawing SolidBrush Fills a shape with a solid color.
System.Drawing Bitmap Represents an image object.
System.Drawing Font Defines the text properties (font family, size, style).
System.Drawing Point Represents an x,y coordinate pair.
System.Drawing Rectangle Represents a rectangular area.
System.Windows.Media (WPF) DrawingContext Provides methods for drawing graphics elements in WPF.
System.Windows.Media (WPF) Brush Abstract base class for brushes in WPF.
System.Windows.Media (WPF) SolidColorBrush Creates a brush with a solid color in WPF.
System.Windows.Media (WPF) Geometry Represents a geometric shape in WPF.