The Graphics Device Interface (GDI) is a key concept for handling graphics in Windows-based applications. It is an API provided by the Windows operating system for rendering graphics and managing visual elements such as shapes, text, and images on the screen. GDI is a fundamental part of the Windows GUI architecture and is widely used for drawing 2D graphics in desktop applications.
In the context of C#, the GDI+ (an enhanced version of the original GDI) is used through the System.Drawing namespace. GDI+ provides a set of classes that allow developers to create, modify, and render graphics such as lines, circles, polygons, and text, as well as manipulate images.
Graphics Object:
Graphics object represents the drawing surface, which can be the screen, a window, or an image.Graphics object from a variety of sources, including the Paint event in Windows Forms, a Bitmap, or directly from the screen using methods like Graphics.FromImage or Graphics.FromHwnd.Pen and Brush:
Pen is used for drawing outlines of shapes, lines, and curves. It defines properties such as color, width, and dash style.
Pen pen = new Pen(Color.Black, 3); creates a black pen with a width of 3 pixels.Brush is used for filling shapes with colors or patterns. The most common types of brushes are SolidBrush (for solid colors), LinearGradientBrush (for gradients), and TextureBrush (for patterns).
Brush brush = new SolidBrush(Color.Blue); creates a blue brush for filling shapes.Drawing Shapes:
Pen (for outlines) or a Brush (for filling).
Graphics.DrawLine(): Draws a line between two points.Graphics.DrawRectangle(): Draws a rectangle outline.Graphics.FillRectangle(): Fills a rectangle with a color or pattern.Graphics.FillEllipse(): Fills an ellipse with a brush.Text Rendering:
Font and Brush objects.
Graphics.DrawString("Hello, World!", font, brush, 100, 100); draws the text "Hello, World!" at the coordinates (100, 100).Font class defines the typeface, size, and style of text (such as bold, italic, etc.).Images:
Image image = Image.FromFile("path_to_image.jpg"); loads an image from a file.Graphics.DrawImage() can be used to draw the image on a surface.Coordinate System:
To use GDI+ in a C# Windows Forms application, you typically interact with the Graphics object during the Paint event of a form or control. The Paint event is triggered whenever the control or form needs to be redrawn, such as when the window is resized, exposed after being covered, or explicitly invalidated.
Here’s an example that demonstrates basic drawing operations (drawing lines, rectangles, and text) using GDI+ in a C# Windows Forms application:
using System;
using System.Drawing;
using System.Windows.Forms;
public class GDIExampleForm : Form
{
public GDIExampleForm()
{
// Set the size of the form
this.Size = new Size(400, 300);
}
// Override the OnPaint method to perform custom drawing
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Get the Graphics object for the current drawing surface
Graphics g = e.Graphics;
// Create pens and brushes
Pen pen = new Pen(Color.Black, 3);
Brush brush = new SolidBrush(Color.Blue);
Font font = new Font("Arial", 14);
// Draw a line
g.DrawLine(pen, 10, 10, 200, 50);
// Draw a rectangle (outline)
g.DrawRectangle(pen, 50, 70, 100, 50);
// Fill a rectangle with a color
g.FillRectangle(brush, 50, 130, 100, 50);
// Draw text
g.DrawString("Hello, GDI+!", font, brush, 50, 200);
// Cleanup
pen.Dispose();
brush.Dispose();
font.Dispose();
}
public static void Main()
{
// Run the application
Application.Run(new GDIExampleForm());
}
}
Graphics object from the PaintEventArgs (e.Graphics) in the OnPaint method. This object is used to perform all the drawing operations.Pen for drawing shapes with a black outline and a Brush for filling shapes with blue color.DrawLine(), DrawRectangle(), FillRectangle(), and DrawString().OnPaint method, which is called whenever the form needs to be redrawn.While GDI+ is a powerful and widely-used technology for 2D graphics, it can be slower than hardware-accelerated graphics frameworks like DirectX or OpenGL. Some performance tips for GDI+ include:
Pen and Brush objects: Creating new objects repeatedly during every paint event can be inefficient. It's better to reuse objects where possible.DoubleBuffered property of the form or control to true.GDI+ in C# allows developers to create 2D graphical applications easily by providing a rich set of objects and methods for drawing shapes, text, and images. It is most commonly used in Windows Forms applications, but can also be utilized in other types of applications that require basic graphics rendering. While it's great for simple graphics and 2D applications, for more complex or performance-critical applications, other APIs such as DirectX or OpenGL may be more appropriate.
Open this section to load past papers