I am developing one application for Windows that is useful for uploading images to a web server. I select all the images from my device in one list object . I convert all bitmaps to bytes [] one by one.
My code
public byte[] ConvertToBytes(BitmapImage bitmapImage) { byte[] data = null; WriteableBitmap wBitmap = null; using (MemoryStream stream = new MemoryStream()) { wBitmap = new WriteableBitmap(bitmapImage); wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100); stream.Seek(0, SeekOrigin.Begin); //data = stream.GetBuffer(); data = stream.ToArray(); DisposeImage(bitmapImage); return data; } } public void DisposeImage(BitmapImage image) { if (image != null) { try { using (MemoryStream ms = new MemoryStream(new byte[] { 0x0 })) { image.SetSource(ms); } } catch (Exception ex) { } } }
Convert from Bitmap to Byte
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!store.DirectoryExists("ImagesZipFolder")) { //MediaImage mediaImage = new MediaImage(); //mediaImage.ImageFile = decodeImage(new byte[imgStream[0].Length]); //lstImages.Items.Add(mediaImage); store.CreateDirectory("ImagesZipFolder"); for (int i = 0; i < imgname.Count(); i++) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder\" + imgname[i], FileMode.CreateNew,store)) //using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder\text.txt" , System.IO.FileMode.OpenOrCreate, store)) { // byte[] bytes = new byte[imgStream[i].Length]; byte[] bytes = ConvertToBytes(ImgCollection[i]); stream.Write(bytes, 0, bytes.Length); } } } else { directory = true; } }
I have 91 images in my emulator. When I convert all these bitmaps to byte [] , I get the following error in the line wBitmap = new WriteableBitmap(bitmapImage);
An exception of type "System.OutOfMemoryException" occurred in System.Windows.ni.dll, but was not processed in the user code

What can I do to solve this error?
Web service
If we send a huge file to a web service, this gives an error, for example
An exception of type "System.OutOfMemoryException" occurred in System.ServiceModel.ni.dll, but was not processed in the user code