1

I am trying to make a StreamImageSource into a Byte. My image named "imgPicked" is a StreamImageSource when I run it in the log. I want to convert that into a Byte [] but I am unsure of how you do it.

This is the code that I have:

private async void btnPickPicture_Clicked (object sender, EventArgs e)
{
    await cameraOps.SelectPicture ();
    var file = await cameraOps.SelectPicture();
    imgPicked.Source = ImageSource.FromStream(() => file.Source);

    System.Diagnostics.Debug.WriteLine (imgPicked.Source);
    //imgPicked is an StreamImageSource

}

How do I turn my StreamImageSource (imgPicked) into a byte []?

This is what I have so far after googling:

byte[] data =  File.ReadAll(imgPicked.Source);

But I do not find "File". Do I miss an assembly or did the author not mention what "File" inherits from (See link): Is there a cross-platform solution to ImageSource to byte[]?

Community
  • 1
  • 1
medvedo
  • 543
  • 2
  • 8
  • 23
  • `StreamImageSource` inherits from `IImageProvider2`.. Which has a `CreateImageWorker` for processing.. What have you tried ? REF: https://msdn.microsoft.com/en-us/library/lumia.imaging.streamimagesource.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 BTW I'm not being facetious, I don't know the answer right now. – Pogrindis Jun 22 '16 at 14:05
  • I have this so far: `byte[] data = File.ReadAll(imgPicked.Source);` after a bit of googeling but I cannot find "File". This is the post that I have been following: http://stackoverflow.com/questions/27532462/is-there-a-cross-platform-solution-to-imagesource-to-byte – medvedo Jun 22 '16 at 14:10
  • Possible duplicate of [How to convert ImageSource to Byte array?](https://stackoverflow.com/questions/26814426/how-to-convert-imagesource-to-byte-array) – NoWar Nov 09 '17 at 02:31

1 Answers1

0

You don't. You cannot convert a StreamImageSource to a byte[]. You can use the source stream to create a byte[], using the code you've been given before.

await cameraOps.SelectPicture ();
var file = await cameraOps.SelectPicture();

// use the file.Source stream to create a StreamImageSource
imgPicked.Source = ImageSource.FromStream(() => file.Source);

// use the file.Source stream to create a byte[]
byte[] imgData = ReadStream(file.Source);

public byte[] ReadStream(Stream input)
    {
        byte[] buffer = new byte[16*1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }

    }
Jason
  • 86,222
  • 15
  • 131
  • 146
  • It works. Wow, when I send in the byte to my DB now. Xamarin starts to lagg like a maniac. Is it because it is trying to upload the image? – medvedo Jun 22 '16 at 14:21
  • Yes, it's uploading a potentially huge file. Either resize the image or do the upload on a thread (preferably both) – Jason Jun 22 '16 at 14:25
  • Ok! Do i resize it on the viewmodel or on the contentpage where I send it? – medvedo Jun 22 '16 at 14:37