[UWP]如何在代码中将图像或位图图像保存到storagefile - 没有filepicker或URI [英] [UWP]How to save image or bitmap image to storagefile in code - without filepicker or URI

查看:101
本文介绍了[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


  

解决方案



>>
如何在代码中将图像或位图图像保存到storagefile - 没有filepicker或URI



来自
这个
文档
,如果你想直接通过路径操作文件而不是使用文件选择器,你需要添加受限制的
broadFileSystemAccess 功能。除了指定功能外,还必须添加rescap命名空间,并将其添加到IgnorableNamespaces:

< Package 
......
xmlns:rescap =" http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces =" uap mp rescap">
......
<功能>
......
< rescap:Capability Name =" broadFileSystemAccess" />
< /功能>
< / Package>

将文件保存在代码中,有两种方法如下:


1,您可以直接保存ImageFile:

等待ImageFile.CopyAsync(等待StorageFolder.GetFolderFromPathAsync(Path),"Bird.jpg",NameCollisionOption。 GenerateUniqueName); 

2,或者将图像保存到文件使用
BitmapEncoder.FlushAsync
方法
(img是加载图像的XAML控件)

 

public async任务SaveImageToFileAsync(string fileName)
{
var rtb = new RenderTargetBitmap();
等待rtb.RenderAsync(img);

StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(path);
StorageFile storageFile = await storageFolder.CreateFileAsync(fileName);

if(storageFile == null)
return;

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

等待SaveImageToFileAsync(" Bird.jpg");






祝你好运,


Roy



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

  

解决方案

Hi,

>> How to save image or bitmap image to storagefile in code - without filepicker or URI

From this document, if you want to manipulate the file directly through the path instead of using file picker, you need to add the restricted broadFileSystemAccess capability. In addition to specifying the capability, the rescap namespace must be added, and is also added to IgnorableNamespaces:

<Package 
         ……
        xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
         IgnorableNamespaces="uap mp rescap">
  ……
 <Capabilities>
    ……
    <rescap:Capability Name="broadFileSystemAccess" />
  </Capabilities>
</Package>

To save the file in your code, there are two methods as below:

1, You can save the ImageFile directly with:

await ImageFile.CopyAsync(await StorageFolder.GetFolderFromPathAsync(Path), "Bird.jpg", NameCollisionOption.GenerateUniqueName);

2, Or save the Image to file with BitmapEncoder.FlushAsync Method (the img is a XAML control to load an image)

public async Task SaveImageToFileAsync(string fileName) { var rtb = new RenderTargetBitmap(); await rtb.RenderAsync(img); StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(path); StorageFile storageFile = await storageFolder.CreateFileAsync(fileName); if (storageFile == null) return; var pixels = await rtb.GetPixelsAsync(); using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); byte[] bytes = pixels.ToArray(); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, 200, 200, bytes); await encoder.FlushAsync(); } }

await SaveImageToFileAsync("Bird.jpg");


Best regards,

Roy


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆