Search code examples
c#asp.net-mvcimageimageresizer

ImageResizer error


I'm using the Image Resizer imageresizing.net. But, when I'm trying to upload or resize an image, an error occurs:

The source stream is at the end (have you already read it?). You must call stream.Seek(0, SeekOrigin.Begin); before re-using a stream, or use ImageJob with ResetSourceStream=true the first time the stream is read.

using (Stream newFile =  System.IO.File.Create(Path.Combine(_pathResolver.ResolvePath(_appSettings.CompanyLogosDirectory), newFileName)) )
{
     //newFile.Seek(0, SeekOrigin.Begin);

     ImageResizer.ImageJob i = new ImageJob();
     //i.ResetSourceStream = true;
     i = new ImageResizer.ImageJob(logo.InputStream, newFile, new ImageResizer.ResizeSettings("width=120;height=45;format=jpg;mode=max"));

     i.CreateParentDirectory = false; //Auto-create the uploads directory.
     i.Build();
}

Solution

  • You have used logo.InputStream before provided code (maybe for other job). You can read image again or as exception suggested put below code before using statement:

    logo.InputStream.Seek(0, SeekOrigin.Begin);