C# - Apps: Save Images

By , 2/6/2015
(0 ratings)
If you want to save images in WP- or Windows Store Apps, you can use one of this Extension methods. They are created for RenderTargetBitmap and WritableBitmap and supports all FileTypes from the BitmapEncoder class.

Required Namespaces
System
System.Threading.Tasks
Windows.Graphics.Display
Windows.Graphics.Imaging
Windows.Storage
Windows.UI.Xaml.Media.Imaging
System.Runtime.InteropServices.WindowsRuntime
Translate to VB
/// <summary>
/// Saves the given <see cref="Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap"/> object as a file.
/// </summary>
/// <param name="image">The image object to save.</param>
/// <param name="file">The <see cref="Windows.Storage.IStorageFile"/> to save the image.</param>
/// <returns>A Task, that assumes the save action.</returns>
public static async Task Save(this RenderTargetBitmap image, IStorageFile file)
{
    var rtb = image as RenderTargetBitmap;
    await Save((await rtb.GetPixelsAsync()).ToArray(), (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, file);
}
/// <summary>
/// Saves the given <see cref="Windows.UI.Xaml.Media.Imaging.WriteableBitmap"/> object as a file.
/// </summary>
/// <param name="image">The image object to save.</param>
/// <param name="file">The <see cref="Windows.Storage.IStorageFile"/> to save the image.</param>
/// <returns>A Task, that assumes the save action.</returns>
public static async Task Save(this WriteableBitmap image, IStorageFile file)
{
    var wb = image as WriteableBitmap;
    await Save(wb.PixelBuffer.ToArray(), (uint)wb.PixelWidth, (uint)wb.PixelHeight, file);
}

private static async Task Save(byte[] bytes, uint width, uint height, IStorageFile file)
{
    using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
        var encoder = await BitmapEncoder.CreateAsync(GetEncoderGuid(file), stream);
        encoder.SetPixelData(
            BitmapPixelFormat.Bgra8,
            BitmapAlphaMode.Ignore,
            width,
            height,
            logicalDpi,
            logicalDpi,
            bytes);
        await encoder.FlushAsync();
    }
}

private static Guid GetEncoderGuid(IStorageFile file)
{
    Guid fileType;
    switch (file.FileType.ToLower())
    {
        case ".dib":
        case ".bmp":
            fileType = BitmapEncoder.BmpEncoderId;
            break;
        case ".gif":
            fileType = BitmapEncoder.GifEncoderId;
            break;
        case ".jpg":
        case ".jpeg":
        case ".jif":
        case ".jfif":
        case ".jpe":
            fileType = BitmapEncoder.JpegEncoderId;
            break;
        case ".hdp":
        case ".jsr":
        case ".wdp":
            fileType = BitmapEncoder.JpegXREncoderId;
            break;
        case ".png":
            fileType = BitmapEncoder.PngEncoderId;
            break;
        case ".tif":
        case ".tiff":
            fileType = BitmapEncoder.TiffEncoderId;
            break;
        default:
            throw new ArgumentOutOfRangeException("Not supported file type.");
    }
    return fileType;
}

Comments

 

Log in, to comment!