uwpImages

Parameters

ParameterDescription
DecodePixelWidthWill load the BitmapImage with the specified width. Helps with memory usage and speed when loading large images that are meant to be displayed smaller on the screen. This is more efficient than loading full image and rely on the Image control to do the resize.
DecodePixelHeightSame as DecodePixelHeight. If only one parameter is specified the system will maintain the Aspect Ratio of the image while loading at the required size.

Using BitmapImage with Image control

<Image x:Name="MyImage" />
// Show image from web
MyImage.Source = new BitmapImage(new Uri("http://your-image-url.com"))

// Show image from solution
MyImage.Source = new Uri("ms-appx:///your-image-in-solution", UriKind.Absolute)

// Show image from file
IRandomAccessStreamReference file = GetFile();
IRandomAccessStream fileStream = await file.OpenAsync();
var image = new BitmapImage();
await image.SetSourceAsync(fileStream);
MyImage.Source = image;
fileStream.Dispose(); // Don't forget to close the stream

Rendering controls to image with RenderTargetBitmap

<TextBlock x:Name="MyControl"
           Text="Hello, world!" />
var rtb = new RenderTargetBitmap();
await rtb.RenderAsync(MyControl); // Render control to RenderTargetBitmap

// Get pixels from RTB
IBuffer pixelBuffer = await rtb.GetPixelsAsync();
byte[] pixels = pixelBuffer.ToArray();

// Support custom DPI
DisplayInformation displayInformation = DisplayInformation.GetForCurrentView();

var stream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, // RGB with alpha
                     BitmapAlphaMode.Premultiplied,
                     (uint)rtb.PixelWidth,
                     (uint)rtb.PixelHeight,
                     displayInformation.RawDpiX,
                     displayInformation.RawDpiY,
                     pixels);

await encoder.FlushAsync(); // Write data to the stream
stream.Seek(0); // Set cursor to the beginning

// Use stream (e.g. save to file)

Convert Bitmap (e.g. from Clipboard content) to PNG

IRandomAccessStreamReference bitmap = GetBitmap();
IRandomAccessStreamWithContentType stream = await bitmap.OpenReadAsync();
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
var pixels = await decoder.GetPixelDataAsync();
var outStream = new InMemoryRandomAccessStream();

// Create encoder for PNG
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream);

// Get pixel data from decoder and set them for encoder
encoder.SetPixelData(decoder.BitmapPixelFormat,
                     BitmapAlphaMode.Ignore, // Alpha is not used
                     decoder.OrientedPixelWidth,
                     decoder.OrientedPixelHeight,
                     decoder.DpiX, decoder.DpiY,
                     pixels.DetachPixelData());

await encoder.FlushAsync(); // Write data to the stream

// Here you can use your stream

Load image in XAML

<Image Source="ms-appx:///Assets/Windows_10_Hero.png"/>

Your image is part of the application, in the Assets folder and marked as Content

<Image Source="ms-appdata:///local/Windows_10_Hero.png"/>

Your image was saved in your application's Local Folder

<Image Source="ms-appdata:///roaming/Windows_10_Hero.png"/>

Your image was saved in your application's Roaming Folder

Load image from Assets in Code

 ImageSource result = new BitmapImage(new Uri("ms-appx:///Assets/Windows_10_Hero.png"));

Use result to set the Source property of an Image control either though a Binding or code-behind

Load Image from StorageFile

public static async Task<ImageSource> FromStorageFile(StorageFile sf)
{
    using (var randomAccessStream = await sf.OpenAsync(FileAccessMode.Read))
    {
        var result = new BitmapImage();
        await result.SetSourceAsync(randomAccessStream);
        return result;
    }
}

Use result to set the Source property of an Image control either though a Binding or code-behind

Useful when you need to open images that are stored on the user's disk and not shipped with your application

Rendering a UI element to an Image

public static async Task<WriteableBitmap> RenderUIElement(UIElement element)
{
    var bitmap = new RenderTargetBitmap();
    await bitmap.RenderAsync(element);   
    var pixelBuffer = await bitmap.GetPixelsAsync();
    var pixels = pixelBuffer.ToArray();
    var writeableBitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
    using (Stream stream = writeableBitmap.PixelBuffer.AsStream())
    {
        await stream.WriteAsync(pixels, 0, pixels.Length);
    }
    return writeableBitmap;
}

Since WriteableBitmap is an ImageSource you can use it to set the Source property of an Image control either though a Binding or code-behind

Save a WriteableBitmap to a Stream

public static async Task<IRandomAccessStream> ConvertWriteableBitmapToRandomAccessStream(WriteableBitmap writeableBitmap)
{
    var stream = new InMemoryRandomAccessStream();

    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
    Stream pixelStream = writeableBitmap.PixelBuffer.AsStream();
    byte[] pixels = new byte[pixelStream.Length];
    await pixelStream.ReadAsync(pixels, 0, pixels.Length);

    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels);
    await encoder.FlushAsync();

    return stream;
}

Use the stream to save the Bitmap to a file.