1

I saw so much of post on google and I couldn't make it works. However, it seems so easy and logic to get an Xamarin.Forms.Image into a String but I'm not able to realize it. I tried from stream, from platform renderer, still doesn't work.

I want it to work on every platform, can you help me?

Thank !

9
  • 1
    Why do you want to do this? What is the goal you're trying to achieve? Commented Jan 19, 2017 at 12:36
  • Because my boss wants to stock it on a mysql case... so I need this image in string in order to save it. I know how to generate the image from the string.. But image to string I can't find it Commented Jan 19, 2017 at 12:38
  • @Emixam23 you want a string representation of a image? Commented Jan 19, 2017 at 13:26
  • Yeah.. then I can store it and convert it back to an image Commented Jan 19, 2017 at 13:41
  • you need to use the original source image that was used to create the Xamarin.Forms.Image Commented Jan 19, 2017 at 14:16

1 Answer 1

1

If what you want is a string representation of a Image then first you need to get the bytes from this image and then convert it into a string format like Base64

But first we need to get the byte from the image, Xamarin.Forms's Image is a View which contains a Source

public class Image : View, IImageController, IElementConfiguration<Image> { public ImageSource Source { get; set; } } 

That source is used to load the image that will be shown, we have a some kinds of ImageSource (FileImageSource, StreamImageSource, UriImageSource) but if I'm not mistaken currently no way to transform ImageSource to bytes in Xamarin.Forms, but we can use native code for such

Android

In Android we can use IImageSourceHandler to transform an ImageSource to Bitmap and form the Bitmap to bytes

[assembly: Dependency(typeof(ImageLoader))] public class ImageLoader : IImageLoader { public async Task<byte[]> LoadImageAsync(ImageSource source) { IImageSourceHandler handler = GetHandlerFor(source); var bmp = await handler.LoadImageAsync(source, Forms.Context); byte[] result; using (Stream ms = new MemoryStream()) { await bmp.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 95, ms); result = new byte[ms.Length]; ms.Position = 0; await ms.ReadAsync(result, 0, (int)ms.Length); } return result; } private IImageSourceHandler GetHandlerFor(ImageSource source) { IImageSourceHandler result; if (source is FileImageSource) result = new FileImageSourceHandler(); else if (source is StreamImageSource) result = new StreamImagesourceHandler(); else result = new ImageLoaderSourceHandler(); return result; } } 

iOS

Same as Android we can use IImageSourceHandler to transform into UIImage and then get the bytes from it

[assembly: Dependency(typeof(ImageLoader))] public class ImageLoader : IImageLoader { public async Task<byte[]> LoadImageAsync(ImageSource source) { IImageSourceHandler handler = GetHandlerFor(source); UIImage image = await handler.LoadImageAsync(source); using (NSData imageData = image.AsPNG()) { return imageData.ToArray(); } } private IImageSourceHandler GetHandlerFor(ImageSource source) { IImageSourceHandler result; if (source is FileImageSource) result = new FileImageSourceHandler(); else if (source is StreamImageSource) result = new StreamImagesourceHandler(); else result = new ImageLoaderSourceHandler(); return result; } } 

#Forms Note that I inserted [assembly: Dependecy(typeof(ImageLoader))] so we can use the Xamarin Forms to recognize and bring the correct ImageLoader from each Platform so we use it like this

byte[] bytes = await DependencyService.Get<IImageLoader>().LoadImageAsync(imgSource); string base64String = Convert.ToBase64String(bytes) //convert the binary to a string representation in base64 

#note IImageLoaderis a simple interface like the following

public interface IImageLoader { Task<byte[]> LoadImageAsync(ImageSource source); } 
Sign up to request clarification or add additional context in comments.

2 Comments

The android snippet is getting stuck on var bmp = await handler.LoadImageAsync(source, Forms.Context);, any idea why?
Heinst, Check: Task<Bitmap> LoadImageAsync (ImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken)), you should look for Application Output and make sure no CancelException

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.