none
[UWP]How to save image or bitmap image to storagefile in code - without filepicker or URI

    Question

  • 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

      


    Wednesday, November 28, 2018 5:53 PM

All replies

  • 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


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 29, 2018 8:23 AM
    Moderator
  • Hi,

    Any updates for the question?

    Is my reply helpful? 

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 6, 2018 10:13 AM
    Moderator