简体   繁体   English

在LocalFolder中存储BitmapImage - UWP

[英]Storing a BitmapImage in LocalFolder - UWP

I'm trying to store a BitmapImage to the filesystem using C# on UWP. 我正在尝试使用UWP上的C#将BitmapImage存储到文件系统。 The image is downloaded from Facebook using the graph api and returned as a BitmapImage. 图像使用图形api从Facebook下载并作为BitmapImage返回。 That part works, and to retrieve the image (once I can store it, tested with pictures just dropped in the local folder) I'm using the following code: 该部分工作,并检索图像(一旦我可以存储它,测试刚刚放在本地文件夹中的图片)我正在使用以下代码:

public static async Task<BitmapImage> GetProfilePicture(string userId){
    BitmapImage profilePicture = new BitmapImage(); 

    StorageFolder pictureFolder = await
                  ApplicationData.Current.LocalFolder.GetFolderAsync("ProfilePictures");
    StorageFile pictureFile = await pictureFolder.GetFileAsync(userId + ".jpg");
    IRandomAccessStream stream = await pictureFile.OpenAsync(FileAccessMode.Read);
    profilePicture.SetSource(stream);

    return profilePicture;   

This works as well, so what I would want is to simply do the opposite. 这也有效,所以我想要的只是做相反的事情。 The preferred result would look like this: 首选结果如下:

public static async void SaveBitmapToFile(BitmapImage  image, userId){
    StorageFolder pictureFolder = await
        ApplicationData.Current.LocalFolder.CreateFolderAsync(
            "ProfilePictures",CreationCollisionOption.OpenIfExists);

       //save bitmap to pictureFolder with name userId.jpg

}

I've searched far and wide trying to find a solution, but I can't seem to find any for the UWP platform. 我一直在寻找解决方案,但我似乎无法找到任何UWP平台。 How would a go about saving the Bitmap to file? 如何将Bitmap保存到文件? The extension doesn't have to be .jpg if it would be easier to use another extension. 如果更容易使用其他扩展名,则扩展名不必是.jpg。

It would be easier if you used a WriteableBitmap. 如果使用WriteableBitmap会更容易。 For example, the first method would then be: 例如,第一种方法是:

public static async Task<WriteableBitmap> GetProfilePictureAsync(string userId)
{
    StorageFolder pictureFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("ProfilePictures");
    StorageFile pictureFile = await pictureFolder.GetFileAsync(userId + ".jpg");

    using (IRandomAccessStream stream = await pictureFile .OpenAsync(FileAccessMode.Read))
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
        WriteableBitmap bmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);

        await bmp.SetSourceAsync(stream);

        return bmp;
    }
}

Then you could do: 然后你可以这样做:

public static async Task SaveBitmapToFileAsync(WriteableBitmap image, userId)
{
    StorageFolder pictureFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePictures",CreationCollisionOption.OpenIfExists);
    var file = await pictureFolder.CreateFileAsync(userId + ".jpg", CreationCollisionOption.ReplaceExisting);

    using (var stream = await file.OpenStreamForWriteAsync())
    {
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream.AsRandomAccessStream());
        var pixelStream = image.PixelBuffer.AsStream();
        byte[] pixels = new byte[bmp.PixelBuffer.Length];

        await pixelStream.ReadAsync(pixels, 0, pixels.Length);

        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)image.PixelWidth, (uint)image.PixelHeight, 96, 96, pixels);

        await encoder.FlushAsync();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM