本文介绍了[UWP]如何在代码中将图像或位图图像保存到storagefile - 没有filepicker或URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在开发一个应用程序,我将图像编码为base64string作为JSON对象的一部分。我能够解码它并将其作为图像对象。我需要能够将此图像保存到代码中的存储文件中。


我看过他们使用文件选择器的示例,我看过他们使用URI的示例图片。在我的情况下,我没有图像的URI,我需要指定文件而不使用filepicker。


我可以从存储文件中加载图像 像这样:


public static async Task< Image> LoadBitmapFromStorageFile(字符串aFileName)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; StorageFile ImageFile = await Globals.picturesLibrary.GetFileAsync(aFileName);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; BitmapImage bimg = new BitmapImage();  &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; FileRandomAccessStream stream =(FileRandomAccessStream)await ImageFile.OpenAsync(FileAccessMode.Read);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; bimg.SetSource(stream);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Image img = new Image();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; img.Source = bimg;



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回img;

  &NBSP; &NBSP; &NBSP; }

但我该怎么做呢?


谢谢,


Yanky


  

解决方案

Hello,

I am developing an app where I am getting images encoded to base64string over as part of a JSON object. I am able to decode this and get it as an image object. I need to be able to save this image to a Storagefile in the code.

I have seen examples where they are using a filepicker, and I have seen examples where they are using the URI of the image. In my case I do not have a URI for the image, and I need to specify the file and not use the filepicker.

I can load an image from a storagefile  like this:

public static async Task<Image> LoadBitmapFromStorageFile(string aFileName)
        {
            StorageFile ImageFile = await Globals.picturesLibrary.GetFileAsync(aFileName);

            BitmapImage bimg = new BitmapImage();            
            FileRandomAccessStream stream = (FileRandomAccessStream)await ImageFile.OpenAsync(FileAccessMode.Read);
            bimg.SetSource(stream);

            Image img = new Image();
            img.Source = bimg;

            return img;
        }

but how would I do the reverse?

Thanks,

Yanky

  

解决方案


这篇关于[UWP]如何在代码中将图像或位图图像保存到storagefile - 没有filepicker或URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:52