Share via


How to: Create a new BitmapSource

This example demonstrates how to use the Create method of BitmapSource to create a new BitmapSource object.

Example

// 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();
value.NextBytes(rawImage);

// 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;

See Also

Concepts

Imaging Overview