1

Hi I am working in an application where we provide option to user to change their profile image.

I am using file upload control to selecting files. It allows max 100MB.

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.

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

Is there any mistake I made?? Or Please suggest any other method to save the image.

Thanks in advance !!! Any help would be appreciated.

Rob
  • 26,989
  • 16
  • 82
  • 98
Shesha
  • 1,857
  • 6
  • 21
  • 28
  • Maybe this answer gives you some idea http://stackoverflow.com/a/8613300/2263683 – Ghasem Dec 15 '15 at 04:23
  • Probably won't fix your problem, but shouldn't you close that `BinaryReader`? Or use it in a `using` statement. Pretty sure it implements `IDisposable`, so you may have a leak there. – Pierre-Luc Pineault Dec 15 '15 at 04:23
  • I have used BinaryReader withing using, But issue not fixed. I am getting the same exception. – Shesha Dec 15 '15 at 05:13

0 Answers0