Hi All,
I have been working on an image gallary. When the user uploads an Image I now check the size of the file. If it is less than 1MB I check to see that the file is actually an image type. Finally I resize the image to an appropriate gallary size, and create a small thumbnail of the image. However since adding in the code to check the types I have been experiancing and OutOfMemoryException.
Here's my controller method:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(Image image, HttpPostedFileBase ImageFile)
{
if (ImageFile.ContentLength > 0)
{
// Get the size in bytes of the file to upload.
int fileSize = ImageFile.ContentLength;
// Allow only files less than 1,048,576 bytes (approximately 1 MB) to be uploaded.
if (fileSize < 1048576)
{
string fileclass = "";
using (BinaryReader r = new BinaryReader(ImageFile.InputStream))
{
byte buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
r.Close();
}
switch (fileclass)
{
case "7137":
case "255216":
case "13780":
try
{
string path = Server.MapPath("~/Uploads/");
ImageFile.SaveAs(path + ImageFile.FileName);
ResizeImageHelper resizeImageHelper = new ResizeImageHelper();
resizeImageHelper.ResizeImage(path + ImageFile.FileName, path + ImageFile.FileName, 640, 480, false);
resizeImageHelper.ResizeImage(path + ImageFile.FileName, path + "thumb" + ImageFile.FileName, 74, 74, false);
image.imageLocation = ImageFile.FileName;
image.imageThumb = "thumb" + ImageFile.FileName;
imageRepository.Add(image);
imageRepository.Save();
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return View("Error");
}
}
}
else
{
//If file over 1MB
return View("Error");
}
}
else
{
//If file not uploaded
return View("Error");
}
return View("Error");
}
And here is the Resize method I use:
public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (OnlyResizeIfWider)
{
if (FullsizeImage.Width <= NewWidth)
{
NewWidth = FullsizeImage.Width;
}
}
int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
if (NewHeight > MaxHeight)
{
// Resize with height instead
NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
NewHeight = MaxHeight;
}
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
NewImage.Save(NewFile);
}
Can anyone advise with this? I am currently just playing around trying to learn new things :-)
Thanks,
Jon
Progress I have narrowed it down to this block, when commented out things function as normal:
using (BinaryReader r = new BinaryReader(ImageFile.InputStream))
{
byte buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
r.Close();
}