How to i can save an Image ?

How to i can save an Image that was returned from DB to a property ?

My code until now :

byte[] imageBytes;
var FileImage = new Image();
imageBytes = Convert.FromBase64String(photo); \ Photo is a string property that contain the image coming of the DB
FileImage.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));

Tagged:

Best Answers

Answers

  • JiriMatejkaJiriMatejka CZMember ✭✭

    @Andreeh_Araujo said:
    How to i can save an Image that was returned from DB to a property ?

    My code until now :

    byte[] imageBytes;
    var FileImage = new Image();
    imageBytes = Convert.FromBase64String(photo); \ Photo is a string property that contain the image coming of the DB
    FileImage.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));

    Do you want to save the byte array to a file? If yes, the easiest way is to call File.WriteAllBytes(imageBytes)

  • Andreeh_AraujoAndreeh_Araujo BRMember ✭✭

    @JiriMatejka, I need save the image in a new folder or in a some folder existent in the project.
    But, i need of the way all. After this that i posted, i don't know continue.
    Do you can help me ?

  • Andreeh_AraujoAndreeh_Araujo BRMember ✭✭

    @JiriMatejka, @DerProgrammierer I can not use the File.WriteAllBytes(path, imageBytes); because returned the error "The name File does not exist in the current context". I tried adding the dll "System.IO" but i can not because returned other error "A reference to C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.IO.dll could not be added. This component is already automatically referenced by the build system". How can i proceed ?

  • Andreeh_AraujoAndreeh_Araujo BRMember ✭✭

    @JiriMatejka I need create an interface or a class library in Droid or PCL ? I'm using the PCL.

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭

    I'm using PCLStorage for saving/reading files - no needed to use platform specific code (it's implemented inside this plugin):
    https://github.com/dsplaisted/PCLStorage

  • Andreeh_AraujoAndreeh_Araujo BRMember ✭✭

    @NamyslawSzymaniuk I tried use the PCLStorage but i couldn't do to save the photo in a folder. You know how i can do it ?

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭

    @Andreeh_Araujo said:
    @NamyslawSzymaniuk I tried use the PCLStorage but i couldn't do to save the photo in a folder. You know how i can do it ?

    What is not working?

    I'll share some code below;

    public IFolder GetRootFolder()
            {
                return FileSystem.Current.LocalStorage;
            }
    
            public async Task CreateRawFile(string filename, Stream fileContentStream, string folderLocation = null)
            {
                IFolder folder = GetRootFolder();
                if (!string.IsNullOrWhiteSpace(folderLocation))
                {
                    // create a folder, if one does not exist already
                    folder = await folder.CreateFolderAsync(folderLocation, CreationCollisionOption.OpenIfExists);
                }
                IFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
    
                var fileContent = GetBytesFromStream(fileContentStream);
    
                using (var memoryStreamHandler = new MemoryStream())
                {
                    await memoryStreamHandler.WriteAsync(fileContent, 0, fileContent.Length);
    
                    using (var fileStreamHandler = await file.OpenAsync(FileAccess.ReadAndWrite))
                    {
                        memoryStreamHandler.Position = 0;
                        await memoryStreamHandler.CopyToAsync(fileStreamHandler);
                    }
                }
            }
    
            public static byte[] GetBytesFromStream(Stream fileContentStream)
            {
                using (var memoryStreamHandler = new MemoryStream())
                {
                    fileContentStream.CopyTo(memoryStreamHandler);
                    return memoryStreamHandler.ToArray();
                }
            }
    
            public async Task CreateRawFile(string filename, byte[] fileContent, string folderLocation = null)
            {
                IFolder folder = GetRootFolder();
                if (!string.IsNullOrWhiteSpace(folderLocation))
                {
                    // create a folder, if one does not exist already
                    folder = await folder.CreateFolderAsync(folderLocation, CreationCollisionOption.OpenIfExists);
                }
                IFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
    
                using (var memoryStreamHandler = new MemoryStream())
                {
                    await memoryStreamHandler.WriteAsync(fileContent, 0, fileContent.Length);
    
                    using (var fileStreamHandler = await file.OpenAsync(FileAccess.ReadAndWrite))
                    {
                        memoryStreamHandler.Position = 0;
                        await memoryStreamHandler.CopyToAsync(fileStreamHandler);
                    }
                }
            }
    

    Two different methods CreateRawFile - based on byte[] and Stream file content.

  • MoniBhMoniBh USMember ✭✭

    HI @NamyslawSzymaniuk , I am also using the PCLStorage to store the images in Local Storage and the path to sqlite database. But the problem I am facing is that every time when I re debug my application on device or simulator the images disappear. Otherwise its working perfectly fine.

    I am unable to understand how exactly it stores the images and at what location? I want a storage location which is persistent because my application needs to work in offline mode as well.

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭
    edited September 15

    @MoniBh Well, I'm NOT! saving full paths in sqlite (currently in NoSQL LiteDB), but only partial path of file, excatly because of the same issue on iOS - full path is different on iOS in debug mode, between "redeploys". On Android there is no issue - there is still one the same path.

    I've already explained it detaily here - https://forums.xamarin.com/discussion/comment/217282/#Comment_217282

    TL;DR - currently I'm using PCLStorage -> FileSystem.Current.LocalStorage, which returns me base path, like /data/user/0/com.mycompany.myapp/files, and I'm saving only partial path inside my DB.

    So when my file is in /data/user/0/com.mycompany.myapp/files/Images/Logo.png, I'm saving only Images/Logo.png, and based that + FileSystem.Current.LocalStorage, I'm building correct file path.

  • MoniBhMoniBh USMember ✭✭

    @NamyslawSzymaniuk Thanks alot. It worked for me.

Sign In or Register to comment.