Image to byte []

5

Hi! I'm using Xamarin.Forms to make an application. So, I need to save images in Parse. To do this, I need to convert the images to byte []. Any idea how I can do this? I already searched the internet but none of the solutions worked for me. In the Parse documentation you have an example of how to do it with a text file:

  byte[] data = System.Text.Encoding.UTF8.GetBytes("Working at Parse is great!");
    ParseFile file = new ParseFile("resume.txt", data);

Xamarin will not let me use System.Drawning.Image, so most of the conventional methods for C # will not work. Any ideas how I can do this?

Thank you!

    
asked by anonymous 23.10.2015 / 18:46

3 answers

3

Code

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
   using (var ms = new MemoryStream())
   {
      imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif); //aqui voce troca o formato de arquivo a salvar
      return  ms.ToArray();
   }
}
    
23.10.2015 / 18:54
3

Try the form below.

Method responsible for converting an Image object to byte [].

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

Method responsible for doing the inverse, ie, byte conversion [] to Image.

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

For more information access the following link: link

    
23.10.2015 / 22:06
2

in C #

Image img = Image.FromFile(@"C:\nomeimagem.jpg");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    arr =  ms.ToArray();
}

source: Image for Byte

    
23.10.2015 / 18:55