title: How to avoid OutOfMemoryException when using BinaryReader
body: <p>Hi I am working in an application where we provide option to user to change their profile image.</p>
  
  <p>I am using file upload control to selecting files. It allows max 100MB.</p>
  
  <p>But I am restricting the image size to 30Mb. Images uploading upto 20mb without any problem. When it is above 20MB it shows OutOfMemoryException. I am using BinaryReader to save the images.</p>
  
  <pre><code>var file = context.Request.Files[0];
  if (!Directory.Exists(Folder))
  {
      Directory.CreateDirectory(Folder);
  }
  
  if (file.ContentLength != 0)
  {
      if (Directory.Exists(targetFolder + "\\" + context.Request["Name"]) == false)
      {
          Directory.CreateDirectory(targetFolder + "\\" + context.Request["Name"]);
      }
  
      var binaryReader = new BinaryReader(file.InputStream);
      var memoryBytes = binaryReader.ReadBytes(file.ContentLength);
      using (var memoryStream = new MemoryStream(memoryBytes))
      {
          var imageStream = Image.FromStream(memoryStream);
          imageStream.Save(targetFolder + "\\" + context.Request["Name"] + "\\" + "picture" + ".png" , ImageFormat.Png);
      }
  }
  </code></pre>
  
  <p>Is there any mistake I made?? Or Please suggest any other method to save the image.</p>
  
  <p>Thanks in advance !!! Any help would be appreciated.</p>