在Uwp中将BitmapImage保存到本地图像文件?

时间:2017-09-20 13:37:48

标签: uwp

我的应用程序中有一个图像控件,并已设置其源。

如何将图像保存到本地图像文件?

我尝试了以下代码,但无法正常工作。

char first[128] ={0};
char second[128] ={0};

enter image description here

2 个答案:

答案 0 :(得分:3)

这是我的一个应用程序的工作方法。

基本上我将Image控件转换为RenderTargetBitmap,然后使用FileSavePicker保存图像。

如果您不想使用Image控件但希望保存Source本身,则需要在呈现SoftwareBitmap控件时保存Image,然后再用于保存Image的用途。示例是Here

private async void Button_Tapped(object sender, TappedRoutedEventArgs e)
{
    var _bitmap = new RenderTargetBitmap();
    await _bitmap.RenderAsync(myImageSource);    //-----> This is my ImageControl.

    var savePicker = new FileSavePicker();
    savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    savePicker.FileTypeChoices.Add("Image", new List<string>() { ".jpg" });
    savePicker.SuggestedFileName = "Card" + DateTime.Now.ToString("yyyyMMddhhmmss");
    StorageFile savefile = await savePicker.PickSaveFileAsync();
    if (savefile == null)
        return;

    var pixels = await _bitmap.GetPixelsAsync();
    using (IRandomAccessStream stream = await savefile.OpenAsync(FileAccessMode.ReadWrite))
    {
        var encoder = await
        BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
        byte[] bytes = pixels.ToArray();
        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                BitmapAlphaMode.Ignore,
                                (uint)_bitmap.PixelWidth,
                            (uint)_bitmap.PixelHeight,
                                200,
                                200,
                                bytes);

        await encoder.FlushAsync();
    }
}

答案 1 :(得分:0)

这是我想出的一种往返解决方案,用于处理UWP中的图像。

此外,我还需要从base-64进行编码和解码。

如果您不想调用FileOpenPicker,则下面的代码是唯一的方法。

此外,请注意:如果您不调用选择器,则文件保存位置的选择将受到限制,这就是为什么我默认使用用户的下载目录。

秘诀是始终始终创建WriteableBitmap。它可以轻松绑定到XAML组件,也可以存储。不要创建BitmapImage,不要创建图像源……只需要处理WriteableBitmap,尤其是在生成需要频繁读取和写入图像的应用程序时。

老实说,这在WinForms中非常容易。

using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography;
using Windows.UI.Xaml.Media.Imaging;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.Storage;
using Windows.Graphics.Imaging;

namespace Images
{
    public static class ImageHelper
    {
        public static async Task<WriteableBitmap> TestImage()
        {

            // example base64-encoded icon in PNG format at 24x24, 32 bit color
            string base64str = "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABGdBTUEAALGPC/xhBQAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAONSURBVEhLpVZZaxNRFJ6f4KNb3brZ1XSxrVq17pnpZDJpUxdUDIqgFKQPKhYF2xcXpBb6JPpQSDKZcY1L7YZYFbGKhfjkm/jkc37C8ftuMhBLG6098JHh3nO+s9xz7o1WTFbG9RWr4npsfaJztDJpzdQ6YQV+b0yYw9iz86pLExKvTRijm5OWND88LNHxC3L98125Mjsk/cDljzelB2twJjVOOLsu0TlAm7x5cYGiXZ4MZSNj52Ukc0/mfr0rittzI9Lx9AQd/VwVNwJ5moVlddyINXjdMjh7U6Z/PFkS+t5dlcZUJFuSWMQJ64nayq0vN+TR9/v/hYvv+6UJThDon05Qlg0sizV2Rh58u7MstDzukS2OnclT56QEXQIH0jvTK0Nfry0LRydPS7PbJaXJUEyRM3qWZl1Sl963Z2Xw04Vl4ejkcalMmXTyUzlA7fuq0W6bXV1OTh+TSx9OLwvd41Gp8nTZiixQmQ4NA5Mud0zZ8kiXQy9NlOnIf+Pc2yOy54UhAXA1uhGpSloDWrVjZUpQnqbHOjZ1sSdMib2JLBmnAHPcUBzNT3Spcy0JpOy0hgGR9Q5SwuJebB4aC0p40pCeaVMO/yOiQGhSl+DroOJoAVfAC0uL2zWjcdw3zHPQORGEAR0FxZ5CVkVgQYe6tFEOXuYcNPgOkEGGHdSERaZX6ICwpuBoEXDP16MNbcnBYOtRIgxdWitLhtKVOGQezO7nuhx4FRRjPCgmyfPGC5ETKgCAurQ5CAfkaMB5NnsRqXPsAW1N3OirT9lSjdba8UyX/a9ytSzMQhEVEBdGTvjloW07OGrAtQ13WkXSCqhB88+Bp9+BFBmJjoh8J34mC4E61KWNX54q15QdXjQ3aJRNCXO0NhVWWWxHBDyoQicswXxirvnkrP0+2NC25iF+vajUO3buqqDwsajDw1HqdKqBY6l8J0yd9SVZIbjGPeqQvD2N1oQt+7/N7f7zsqOsjRs2BkNKHUM5YTRMmXUlCaMkIcFvrnGPOgyI5LVuSHZ6PdnSRGjhNwFvb6whFZEyZMJUWU9G1oHO4IwwKwV8c4177Pla6DLyXSBH2xd/1fKZZHmXVKQMZdyItiNR29Mc+M017lW6hrShY3ComUUjny88E7TYcKvbnW3xujA0YZX+ppSuwGug0bOlFcTtuagH8qZLEzrCbRvDCzXMsUcJFFrxjSyH8Uj95W+Lpv0GtNxNNKdkcLwAAAAASUVORK5CYII=";

            // First load the image from the base64 string
            var img = await LoadImageFromBase64(base64str);

            // Now test round-trip Base64 to Base64
            string sTest = await SaveImageToBase64(img);

            img = await LoadImageFromBase64(sTest);

            // now save the round-trip image
            var accTok = await SaveImageToFile("checkbox-round-trip.png", img);

            // now load, round-trip, from disk.
            img = await LoadImageFromFile(accTok, true);

            return img;           
        }


        public static async Task<WriteableBitmap> LoadImageFromBase64(string base64str)
        {
            try
            {
                var decodedBin = CryptographicBuffer.DecodeFromBase64String(base64str);

                var readStream = new InMemoryRandomAccessStream();

                await readStream.WriteAsync(decodedBin);
                readStream.Seek(0);

                var decoder = await BitmapDecoder.CreateAsync(readStream);

                var workBmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
                readStream.Seek(0);

                await workBmp.SetSourceAsync(readStream);
                return workBmp;

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static async Task<string> SaveImageToBase64(WriteableBitmap workBmp)
        {
            try
            {
                var writeStream = new InMemoryRandomAccessStream();
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, writeStream);

                // saving to a straight 32 bpp PNG, the dpiX and dpiY values are irrelevant, but also cannot be zero. 
                // We will use the default "since-the-beginning-of-time" 96 dpi.
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)workBmp.PixelWidth, (uint)workBmp.PixelHeight, 96, 96, workBmp.PixelBuffer.ToArray());

                await encoder.FlushAsync();
                writeStream.Seek(0);

                var proxyStream = writeStream.AsStreamForRead();
                byte[] b = new byte[proxyStream.Length];

                await proxyStream.ReadAsync(b, 0, (int)proxyStream.Length);

                proxyStream.Dispose();

                return (string)CryptographicBuffer.EncodeToBase64String(b.AsBuffer());
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static async Task<WriteableBitmap> LoadImageFromFile(string fileName, bool isToken = false)
        {
            try
            {
                var xc = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList;
                StorageFile decodedBin;

                if (!isToken)
                {
                    decodedBin = await StorageFile.GetFileFromPathAsync(fileName);
                }
                else
                {
                    decodedBin = await xc.GetFileAsync(fileName, Windows.Storage.AccessCache.AccessCacheOptions.None);
                }

                var readStream = await decodedBin.OpenReadAsync();

                var decoder = await BitmapDecoder.CreateAsync(readStream);
                var workBmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);

                readStream.Seek(0);

                await workBmp.SetSourceAsync(readStream);

                readStream.Dispose();
                return workBmp;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static async Task<string> SaveImageToFile(string fileName, WriteableBitmap workBmp)
        {
            try
            {
                var writeStream = new InMemoryRandomAccessStream();
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, writeStream);

                // saving to a straight 32 bpp PNG, the dpiX and dpiY values are irrelevant, but also cannot be zero. 
                // We will use the default "since-the-beginning-of-time" 96 dpi.
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)workBmp.PixelWidth, (uint)workBmp.PixelHeight, 96, 96, workBmp.PixelBuffer.ToArray());

                await encoder.FlushAsync();

                writeStream.Seek(0);

                var dl = await DownloadsFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
                var imgFileOut = await dl.OpenStreamForWriteAsync();

                var fileProxyStream = writeStream.AsStreamForRead();

                await fileProxyStream.CopyToAsync(imgFileOut);
                await imgFileOut.FlushAsync();

                fileProxyStream.Dispose();
                imgFileOut.Dispose();

                var xc = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList;

                return xc.Add(dl);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}