0

I am working on Windows Phone and I need to convert the images in the phone to a byte array, but the problem is that VisualStudio throws an OutOfMemory exception.

Is there any way to avoid this error? Or an alternative way?

public static byte[] GetBytes(Picture p)
{
    byte[] buffer=new byte[p.GetImage().Length];
    p.GetImage().Read(buffer, 0, buffer.Length);
    return buffer;
}
rae1
  • 6,066
  • 4
  • 27
  • 48
programmer23
  • 533
  • 4
  • 15
  • 3
    Yes. Do not load such large of an image. Perhaps you could stream the image rather than load it into memory in one step. – rae1 Jan 20 '14 at 16:36
  • you could pass the image to the byte array in chunks instead of all at once, maybe it'll help. Are you loading the picture from a folder? Or is it only in memory? – Steven Mills Jan 20 '14 at 16:40
  • The image is located in a folder – programmer23 Jan 20 '14 at 16:51
  • I try to send the image using chunks, but now I don't know what happen that the stream sends more bytes than the total of the images :S – programmer23 Jan 21 '14 at 08:42

3 Answers3

0

The image is too big for the memory.. Use Stream in System.IO to stream it and save it. That is the most efficient one.

iefpw
  • 6,816
  • 15
  • 55
  • 79
0

You need to use MemoryStream such as :

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

See How to convert image in byte array

Community
  • 1
  • 1
skolte
  • 367
  • 2
  • 9
0

Ok, I have found another way to upload images and now it doesn't throw exceptions. I decided to upload the file by chunks, just as Steven Mills. I would like to thank you all for your help.

programmer23
  • 533
  • 4
  • 15