TheDeveloperBlog.com


C# Drawing: Images and Fonts

Drawing. The System.Drawing namespace provides graphical capabilities. With its tools, we manipulate and create visual effects. We handle images and primitive shapes. The Image and Font types are among the most important.


Image. This console program opens an image with the Image.FromFile method. It then prints the size of the image to the console. The Image type makes working with image data, such as PNG and JPG, easier.

Image

Tip: If the program doesn't compile, try adding the assembly reference System.Drawing in Visual Studio.

Bitmaps: Images are usually stored as bitmaps, which represent collections of colored pixels. They can also store translucency values.

Thumbnails: Thumbnail images can be created with the Image type. We provide details and examples.

Thumbnail Image
Based on:

.NET 4

C# program that uses Image, System.Drawing namespace

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	Image image = Image.FromFile("C:\\background1.png");
	Console.WriteLine(image.Size);
    }
}

Output

{Width=180, Height=110}


Colors are the end result of how the light receptors in our eyes interact with our computer monitors. Also, things in the outside world apparently have colors too. We describe the Color type in the System.Drawing namespace.

Color

Console: The console also supports colors. These are specified with a separate, ConsoleColor enum type.

Console Colors

Sizes, Points. If you want to draw shapes on a two-dimensional surface, you will need sizes and points. These are made up of integers and floating-point numbers. Size has integer coordinates. SizeF has floating-point coordinates.

Size, SizeF

Point: A Size stores a width and a height. A Point instead stores just two coordinates: X and Y.

Point, PointF

Font. The Font type is also found in the System.Drawing namespace. We describe some interesting techniques of handling fonts in your programs, beginning with the Font type itself. We test a typeface's existence by using the constructor.

Font

WPF. The WPF Framework supports drawing operations. With some controls, such as Ellipse, we draw shapes. And with the RenderTransform element, we can apply dynamic transformations to these shapes.

EllipseRenderTransformWPF

Summary. These tips will not make it possible to create elaborate graphics, but they emphasize some important things about the System.Drawing namespace in the .NET Framework. Complex three-dimensional rendering requires more advanced libraries.