I have the following controller, which upon receiveing a file from the view generates a thumbnail and saves both, image abd thumbnail to the database:
public async Task<ActionResult> FileUpload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var logo = new Logo() { LogoGuid = Guid.NewGuid() };
var clinica = await GetClinicaAsync();
if (IsClinicNonexistent(clinica))
{
db.Logos.Add(logo);
clinica.LogoGuid = logo.LogoGuid;
}
else
logo = await db.Logos.FirstOrDefaultAsync(l => l.LogoGuid == clinica.LogoGuid);
logo.LogoImage = GetImage(file);
logo.Thumbnail = GetThumbnail(file);
db.SaveChanges();
}
return RedirectToAction("Arquivos");
}
Which, for the matters of this error calls a method:
private byte[] GetThumbnail(HttpPostedFileBase file)
{
var image = Image.FromStream(file.InputStream);
var thumbnailSize = GetThumbnailSize(image);
var memoryStream = new MemoryStream();
var thumbnail = image.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
image.Save(memoryStream, ImageFormat.Jpeg);
return memoryStream.ToArray();
}
That configuration of code produces an erro while running the line
var image = Image.FromStream(file.InputStream);
The error is: "System.ArgumentException: Parameter is not valid."
All documentation that I could refer to, makes me believe that it's all sintatically and logically correct, yet there is the error...
But, tne funny thing is, if a move the conversion line which produces error, do the calling method, specifically to the begining as:
public async Task<ActionResult> FileUpload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var logo = new Logo() { LogoGuid = Guid.NewGuid() };
var clinica = await ClinicaAsync();
var image = Image.FromStream(file.InputStream);
There will be no error. Please notice that the convertion line is in the last line of the code just above, thus in the begining of the controller. Again, if I move that line further down, but still in the controller, there will be the error again...
How can I keep the line where it logically belongs, thus in the GetThumbnail method?
Just as an additional reference, the GetImage method is:
private byte[] GetImage(HttpPostedFileBase file)
{
using (var br = new BinaryReader(file.InputStream))
return br.ReadBytes(file.ContentLength);
}