uwp

Images

Parameters#

Parameter Description
DecodePixelWidth Will 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.
DecodePixelHeight Same 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

Rendering controls to image with RenderTargetBitmap

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

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.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow