title: OutOfMemoryException occurs on WriteableBitmap
body: <p>I am developing one windows phone application which useful for upload images to web server. I am selecting all images from my device into one <strong>List object</strong>. I am converting all bitmap image to byte[] one by one. </p>
  
  <p><strong>My code</strong> </p>
  
  <pre><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)
              {
              }
          }
      }
  </code></pre>
  
  <p><strong>Conversion from bitmap to byte</strong></p>
  
  <pre><code> 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 &lt; 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;
              }
            }
  </code></pre>
  
  <p>I have 91 images in my emulator. When I am converting all these bitmap images into <strong>byte[]</strong>, get's following error on line <code>wBitmap = new WriteableBitmap(bitmapImage);</code></p>
  
  <blockquote>
    <p>An exception of type 'System.OutOfMemoryException' occurred in System.Windows.ni.dll but was not handled in user code</p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/sdLnz.png" alt="Error"></p>
  
  <p>What can I do to solve this error? </p>
  
  <p><strong>Web Service</strong></p>
  
  <p>If we are sending huge file to web service, is it gives error like </p>
  
  <blockquote>
    <p>An exception of type 'System.OutOfMemoryException' occurred in System.ServiceModel.ni.dll but was not handled in user code</p>
  </blockquote>