Articles in this section
Category / Section

How to change the image saving location in UWP?

2 mins read

By default, in SfImageEditor when we save image, the image will be getting saved in gallery. We can also able to change the image saving location.

 

Step 1: Create SfImageEditor sample with all necessary assemblies.

 

Please refer the below link to create a simple SfImageEditor sample along with the ways to configure it.

https://help.syncfusion.com/uwp/sfimageeditor/getting-started

 

Step 2: Get stream from ImageSaving event. Create method for selecting folders and pass the stream as argument

 

Step 3: Convert the stream to bitmap and save image in custom location.

 

The following code snippet illustrates the way to this method,

 

C#

 

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            BitmapImage bitmapImage = new BitmapImage();
            Uri uri = new Uri("ms-appx:///Assets/Image.jpg");
            bitmapImage.UriSource = uri;
            imageeditor.ImageSource = bitmapImage;
 
            imageeditor.ImageSaving -= Imageeditor_ImageSaving;
            imageeditor.ImageSaving += Imageeditor_ImageSaving;
        }
        Stream stream;
        private void Imageeditor_ImageSaving(object sender, ImageSavingEventArgs args)
        {
            args.Cancel = true;
            stream = args.Stream;
            SelectDirectory(stream);
        }
 
        public async void SelectDirectory(Stream stream)
        {
            this.stream = stream;
            IRandomAccessStream randomAccessStream = this.stream.AsRandomAccessStream();
            var wbm = new WriteableBitmap(600, 800);
            await wbm.SetSourceAsync(randomAccessStream);
            string fileName = "image.jpg";
            var folderPicker = new Windows.Storage.Pickers.FolderPicker();
            folderPicker.SuggestedStartLocation = 
                                          Windows.Storage.Pickers.PickerLocationId.Desktop;
            folderPicker.FileTypeFilter.Add("*");
            Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            string SelectedFolderPath = folder.Path;
            if (folder != null)
            {
                // Application now has read/write access to all contents in the picked folder
                // (including other sub-folder contents)
                Windows.Storage.AccessCache.StorageApplicationPermissions.
                FutureAccessList.AddOrReplace("PickedFolderToken", folder);
                StorageFile file = await folder.CreateFileAsync(fileName, 
                                    CreationCollisionOption.ReplaceExisting);
                using (var storageStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, storageStream);
                    var pixelStream = wbm.PixelBuffer.AsStream();
                    var pixels = new byte[pixelStream.Length];
                    await pixelStream.ReadAsync(pixels, 0, pixels.Length);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)wbm.PixelWidth, (uint)wbm.PixelHeight, 200, 200, pixels);
                    await encoder.FlushAsync();
                }
            }
        }
    }

 

 

 

Sample link:

 

https://www.syncfusion.com/downloads/support/directtrac/general/ze/ImageEditor_GettingStarted-494894932.zip

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied