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;
' Define parameters used to create the BitmapSource.
Dim pf As PixelFormat = PixelFormats.Bgr32
Dim width As Integer = 200
Dim height As Integer = 200
Dim rawStride As Integer = CType((width * pf.BitsPerPixel + 7) / 8, Integer)
Dim rawImage(rawStride * height) As Byte
' Initialize the image with data.
Dim value As New Random()
value.NextBytes(rawImage)
' Create a BitmapSource.
Dim bitmap As BitmapSource = BitmapSource.Create(width, height, 96, 96, pf, Nothing, rawImage, rawStride)
' Create an image element;
Dim myImage As New Image()
myImage.Width = 200
' Set image source.
myImage.Source = bitmap
See also
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET Desktop feedback