geostocker
geostocker

Reputation: 1200

Unable to create stream from stored image for byte[] conversion

I've been struggling with this implementation for a few hours now and can't seem to find any solutions wherever I look (SO, Xamarin Forums, Google etc)...

In this current scenario I have a few images in .Droid.Resources.Drawable which I wish to access and convert into a byte[] from my shared code. This is due to the fact that I wish to test the full span of my CRUD functionality on a REST API I've set up as an end-point for our server.

The images show up fine in the application, but for some reason I simply can't seem to warp my head around the process of converting these images to a byte[] in Xamarin. I've done it countless times in 'normal' C#...

Sorry if the code is a bit messy, but I'm sure you get the idea.

  1. I want to get an image from .Droid storage (will be ported for iOS later)
  2. Convert said image into a byte[]
  3. Send that byte[] representation to my API.

In the code's current state I'm getting this error:

C#: An instance of an abstract class can not be created

Where I'm attempting to create a new Stream (new Stream(sauce))


The below example is based on snippets found here and full credit goes to Sten and Vincent.

   /*
    * Takes an arbitrary string as a token, updates a record with dummy data and a placeholder_image.
    */
    public async Task<string> PostUpdateFoundation(string arbitrary, Image img)
    {

        ImageSource sauce = ImageSource.FromFile("abc.png");
        byte[] byte_img = FromStreamToByte(new Stream(sauce)); //error occurs here
        Debug.WriteLine("I'm in!");
        var client = new System.Net.Http.HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "application/json");

        var content = new StringContent(arbitrary);
        var response = await client.PostAsync(String.Format("http://some.api.to.test.com?s={0}&img={1}", arbitrary, byte_img), content); 
        var result = response.Content.ReadAsStringAsync().Result; 
        return result;

    }
   /*
    * Attempts to convert an stream (based on image source) into a byte[].
    */
    public static byte[] FromStreamToByte (Stream input)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            input.CopyTo(ms);
            return ms.ToArray();
        }
    }

Upvotes: 0

Views: 409

Answers (1)

Ingenator
Ingenator

Reputation: 222

Try using Plugin.Media

            byte BImageSource = ReadFully(file.GetStream());
            var bytes = new byte[file.GetStream().Length]; //file is from the plugin and contains your image
            file.GetStream().Position = 0;
            file.GetStream().Read(bytes, 0, (int)file.GetStream().Length);
            BImageSource = ReadFully(file.GetStream()); //BImageSource is your resource in bytes

    byte[] ReadFully(Stream input)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            input.CopyTo(ms);
            return ms.ToArray();
        }
    }

Hope this helps!

Upvotes: 1

Related Questions