We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to show local image with path

Hi all,

I have a simple page (using FreshMVVM) and I can not load/show locally saved image. I have path as string and then create ImageSource from a file. Image control from XF shows image but sfImageEditor does not. What am I doing wrong?


This is my codebehind

class FullScreenPageModel : FreshBasePageModel
    {
        public async override void Init(object initData)
        {


            string imageString = initData as string;
            if (string.IsNullOrEmpty(imageString))
            {
                await CoreMethods.PopPopupPageModel();
            }
            string path = StaticDeclaration.ImageFolder();
            var imageSource = Path.Combine(path, imageString);
            SelectedImage = imageSource;
            SelectedImageSF = ImageSource.FromFile(imageSource);


        }


        ImageSource selectedImage;
        public ImageSource SelectedImage
        {
            get => selectedImage;
            set
            {
                selectedImage = value;
                RaisePropertyChanged();
            }
        }




        ImageSource selectedImageSF;
        public ImageSource SelectedImageSF
        {
            get => selectedImageSF;
            set
            {
                selectedImageSF = value;
                RaisePropertyChanged();
            }
        }
        public ImageSource Image { get; set; }
    }

And this is my Page

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:imageeditor="clr-namespace:Syncfusion.SfImageEditor.XForms;assembly=Syncfusion.SfImageEditor.XForms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="iVanApp.FullScreenPage">


    <ContentPage.Content>
        <StackLayout>
            <imageeditor:SfImageEditor Source="{Binding SelectedImageSF}"
                                       EnableZooming="True">
            </imageeditor:SfImageEditor>
            <Image Source="{Binding SelectedImage}"/>


        </StackLayout>
    </ContentPage.Content>
</ContentPage>

3 Replies

ET Eswaran Thirugnanasambandam Syncfusion Team February 24, 2023 03:53 PM UTC

Hi Gregor,


We have prepared a sample based on the code snippet provided and tested it for the reported problem. However, we were unable to replicate the problem on our end in the latest NuGet version (20.4.0.51). Please find the tested sample and video in the attached attachments.


If you are still experiencing the issue, please provide a modified sample of your scenario, this will help us to provide a better solution as soon as possible.


Regards,

Eswaran


Attachment: SfImageEditorFreshMVVM_3d928070.zip


GR Gregor March 3, 2023 08:14 AM UTC

Thank you for provided example, but I am still not able to show images that are saved in "

System.Environment.SpecialFolder.LocalApplicationData". I can not make it work.


This is my code

internal class StartPageModel : FreshBasePageModel
    {
        public async override void Init(object initData)
        {
            await DownloadImage();


        }


        async Task DownloadImage()
        {
            string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "Test Images");


            Uri image_url_format = new Uri("https://aka.ms/campus.jpg");
            WebClient webClient = new WebClient();
            string dest_folder = path;
            string file_name = Path.GetFileName(image_url_format.LocalPath);
            string dest_path = Path.Combine(dest_folder, file_name);
            if(!File.Exists(dest_path))
            {
                try
                {
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    byte[] bytes_image = await webClient.DownloadDataTaskAsync(image_url_format);
                    Stream image_stream = new MemoryStream(bytes_image);


                    var image = ImageSource.FromResource(dest_path);


                    using (var fileStream = new FileStream(dest_path, FileMode.Create, FileAccess.Write))
                    {
                        await image_stream.CopyToAsync(fileStream);
                    }
                }
                catch (Exception ex)
                {
                    await CoreMethods.DisplayAlert("Error", ex.ToString(), "OK");
                }
            }
            ImagePath = dest_path;
        }


        private string imagePath;
        public string ImagePath
        {
            get => imagePath;
            set{ imagePath = value; RaisePropertyChanged();}
        }


    }

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:imageeditor="clr-namespace:Syncfusion.SfImageEditor.XForms;assembly=Syncfusion.SfImageEditor.XForms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="sfImageEditorTest.StartPage">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="sfImageEditor"/>
            <imageeditor:SfImageEditor Source="{Binding ImagePath}">
            </imageeditor:SfImageEditor>
            <Label Text="Image XF"/>
            <Image Source="{Binding ImagePath}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>


ET Eswaran Thirugnanasambandam Syncfusion Team March 6, 2023 01:06 PM UTC

We checked your code snippet and encountered an error when attempting to get the image from the web client. We recommend loading the image source directly from the URI rather than saving the image through the web client, as mentioned in the below code snippet.

[C#]

ImageSource SelectedImage = ImageSource.FromUri(image_url_format);


We have modified the sample to achieve your requirements. Please get it from the below attachment.


Similar to your requirement we have already have a kb to load an image from the phone gallery. Please check the below Kb link.

https://www.syncfusion.com/kb/9368/how-to-load-image-from-camera-and-gallery-in-xamarin-image-editor-sfimageeditor


Attachment: SfImageEditorFreshMVVM_b413aa65.zip

Loader.
Live Chat Icon For mobile
Up arrow icon