This subclass of Image encapsulates a picture represented by a 2D array of pixel data. It supports a range of PixelFormats, including both true-color and palette-color bitmaps, and those with an alpha channel for transparency.

A Bitmap conceptually represents a system bitmap and is ultimately implemented through a native GDI+ bitmap handle. As with most drawing classes that wrap limited resources (Pen and Brush being other examples), you should have a well-defined lifetime management plan, calling Dispose() when you are finished to release the resources back to the system (the using idiom in C# is useful here: see the main body of the text for details). If you rely on the garbage collector to do this, the Bitmap object will be collected but its pixel data will be leaked until the garbage collector happens to run, which may be too late.

There are several ways to construct a Bitmap object. One of the most is common is to provide the filename of the picture that you want to load. The framework will use the installed CODECs (BMP, PNG, JPG, and GIF) to attempt to load the image. Note that the file itself will remain locked until the Bitmap that loaded it is disposed. However, you can Clone() the bitmap and Dispose() the original to release the file.

Alternatively, you can construct bitmaps of a specific size and PixelFormat, or bitmaps compatible with a particular Graphics surface. You can also construct from another Image (optionally rescaling the original in the process), ...

Get .NET Windows Forms in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.