This documentation is archived and is not being maintained.

BitmapSource Class

Represents a single, constant set of pixels at a certain size and resolution.

Namespace:  System.Windows.Media.Imaging
Assembly:  PresentationCore (in PresentationCore.dll)

[LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable)]
public abstract class BitmapSource : ImageSource
This class is abstract; see Inheritance Hierarchy for derived non-abstract classes usable in XAML.

BitmapSource is the basic building block of the Windows Presentation Foundation (WPF) imaging pipeline, conceptually representing a single, constant set of pixels at a certain size and resolution. A BitmapSource could be a single frame in an image file that a decoder provides, or it could be the result of a transform that operates on a BitmapSource of its own. BitmapSource is not used to represent a multi-frame image or an animation.

Windows Presentation Foundation (WPF) natively supports compression and decompression of bitmap (BMP), Graphics Interchange Format (GIF), Joint Photographics Experts Group (JPEG), Portable Network Graphics (PNG), and Tagged Image File Format (TIFF) images.

For bitmap decoding scenarios, BitmapSource uses automatic codec discovery, based on the installed codecs on the user's system.

The maximum height and width of an image is 2^16 pixels at 32 bits per channel * 4 channels. The maximum size of a BitmapSource is 2^32 bytes (64 gigabytes) and the maximum image size is four gigapixels. The minimum image size is 1x1.

The following code example demonstrates how to create a BitmapSource and use it as the source of an Image control.

// Define parameters used to create the BitmapSource.
PixelFormat pf = PixelFormats.Bgr32;
int width = 200;
int height = 200;
int rawStride = (width * pf.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[rawStride * height];

// Initialize the image with data.
Random value = new Random();

// Create a BitmapSource.
BitmapSource bitmap = BitmapSource.Create(width, height,
    96, 96, pf, null,
    rawImage, rawStride);

// Create an image element;
Image myImage = new Image();
myImage.Width = 200;
// Set image source.
myImage.Source = bitmap;

The following code example uses a BitmapSource derived class, BitmapImage, to create a bitmap from an image file and use it as the source of an Image control.

// Create the image element.
Image simpleImage = new Image();    
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);

// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);
// Set the image source.
simpleImage.Source = bi;

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0