locked
How to convert Xamarin.Forms.Image ToBase64String? RRS feed

  • Question

  • User376301 posted

    Hello everyone!

    I have Xamarin.Forms.Image or image file in project (embedded resource). I need convert to ToBase64String, but I don't know how get byte array. If I try use System.Drawing.Image then I get excepition. If i try use File.ReadAllBytes then file not found all time. I specify links from android or embedded resource of project.

    Maybe you can convert a page to bytes or Xamarin.Forms.Image convert to Bitmap as an option?

    Thank you.

    Thursday, November 22, 2018 2:25 PM

Answers

  • User376301 posted

    I did that way:

            var assembly = this.GetType().GetTypeInfo().Assembly;
            byte[] buffer;
            using (Stream s = assembly.GetManifestResourceStream("DirectorixRoom.Images.bubble.png"))
            {
                long length = s.Length;
                buffer = new byte[length];
                s.Read(buffer, 0, (int)length);
            }
    
            string _base64String = Convert.ToBase64String(buffer);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, November 27, 2018 11:37 AM

All replies

  • User368076 posted

    @Harshadcse provided this answer in another forum:

    try this:

    == image path to base64 string ==

    // provide read access to the file FileStream fs = new FileStream(media.Path, FileMode.Open,FileAccess.Read); // Create a byte array of file stream length byte[] ImageData = new byte[fs.Length]; //Read block of bytes from stream into the byte array fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); string _base64String = Convert.ToBase64String (ImageData);

    Friday, November 23, 2018 1:20 AM
  • User376301 posted

    This is a good solution, but how do I get media.Path? My images are located: Xamarin.Forms/Images/bubble.png or Android Resources/drawable/bubble.png

    Tuesday, November 27, 2018 11:04 AM
  • User376301 posted

    I did that way:

            var assembly = this.GetType().GetTypeInfo().Assembly;
            byte[] buffer;
            using (Stream s = assembly.GetManifestResourceStream("DirectorixRoom.Images.bubble.png"))
            {
                long length = s.Length;
                buffer = new byte[length];
                s.Read(buffer, 0, (int)length);
            }
    
            string _base64String = Convert.ToBase64String(buffer);
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, November 27, 2018 11:37 AM
  • User155041 posted

    @MindRus said: I did that way:

            var assembly = this.GetType().GetTypeInfo().Assembly;
            byte[] buffer;
            using (Stream s = assembly.GetManifestResourceStream("DirectorixRoom.Images.bubble.png"))
            {
                long length = s.Length;
                buffer = new byte[length];
                s.Read(buffer, 0, (int)length);
            }
    
            string _base64String = Convert.ToBase64String(buffer);
    

    Thank you, ? ? ,Finally it worked

    Wednesday, July 31, 2019 5:05 PM