Image to byte[]

Asked

Viewed 2,845 times

5

Hi! I’m using Xamarin.Forms to make an app. So, I need to save images to Parse. To do this, I need to convert the images to byte[]. Any idea how I can do this ? I’ve looked on the internet but none of the solutions worked for me. In the Parse documentation there is an example of how to do with a text file :

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

Xamarin won’t let me use System.Drawning.Image, so most conventional methods for c# won’t work. Any idea how I can do that ?

Thank you !

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();
   }
}

3

Try it the way 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 reverse, 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 visit the following link: http://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.