Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to convert Xamarin.Forms.Image ToBase64String?

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.

Best Answer

  • MindRusMindRus Member ✭✭
    Accepted Answer

    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);
    

Answers

  • stemadostemado Member ✭✭✭

    @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);

  • MindRusMindRus Member ✭✭

    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

  • MindRusMindRus Member ✭✭
    Accepted Answer

    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);
    
  • CharwakaCharwaka INMember ✭✭✭✭✭

    @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

Sign In or Register to comment.