Question

I'm using the port of UrlImageViewHelper to lazy load images to an image view within my monodroid app. The problem is that I need to convert this image to a byte array, but can't seem to find anything that shows how to do this.

Currently I have this

var img = new ImageView(_context);
img.SetUrlDrawable(image, Resource.Drawable.image_holder);
var conv = img.ToArray<byte>();
DisplayImage(conv, lin);

This looks correct, but when I've used this previously, it's given an exception.

If the file was a straight resource, I could use this

var icon = BitmapFactory.DecodeResource(_context.Resources, Resource.Drawable.image_holder);
using (MemoryStream stream = new MemoryStream())
{
     icon.Compress(Bitmap.CompressFormat.Png, 100, stream);
     DisplayImage(stream.ToArray(), lin);
}
Was it helpful?

Solution

I don't really know what you want to do with a "byte array" of the image, but to use the code above - which stores a PNG - on an ImageView, you will need to use something like this:

    // Find image view.
    var imgView = this.FindViewById<ImageView>(Resource.Id.imageView);
    // Set some image
content.imgView.SetImageDrawable(this.Resources.GetDrawable(Resource.Drawable.Icon)); imgView.BuildDrawingCache ();

    // Get the bitmap content of the image view.
    Bitmap bitmap = imgView.DrawingCache;
    // Save as PNG to disk.
    string path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "image.png");
    using (var stream = File.OpenWrite(path))
    {
        bitmap.Compress (Bitmap.CompressFormat.Png, 100, stream);
    }
    Console.WriteLine("Image saved to {0}", path);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top