gpt4 book ai didi

c# - OutOfMemoryException C# 上传图像文件时

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:19 24 4
gpt4 key购买 nike

我一直在制作图片库。当用户上传图像时,我现在检查文件的大小。如果它小于 1MB,我会检查该文件是否实际上是图像类型。最后,我将图像调整为适当的图库大小,并创建图像的小缩略图。但是,自从添加代码来检查我一直在经历的类型和 OutOfMemoryException。

这是我的 Controller 方法:

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

这是我使用的调整大小方法:

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

有人可以对此提出建议吗?我目前只是在尝试学习新事物:-)

谢谢,

乔恩

进度我已经将它缩小到这个 block ,当注释掉时一切正常:

using (BinaryReader r = new BinaryReader(ImageFile.InputStream))
{
byte buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
r.Close();

}

最佳答案

我假设这不会在第一次运行时发生,但会在一段时间后发生。这是正确的吗?

编辑:删除了不正确的假设,但 IDisposable 仍然存在问题


您没有处理 NewImage,这会导致您在生产中出现问题。

我通常会说“只需使用一个 using”,但 try/finally 是同一回事。根据您的判断重构给我们使用。

System.Drawing.Image NewImage = null;
System.Drawing.Image FullsizeImage = null;

try
{
FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

[... snip ... ]

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);
}
finally
{
if (FullsizeImage != null)
FullsizeImage.Dispose();
if (NewImage != null)
NewImage.Dispose();
}

关于c# - OutOfMemoryException C# 上传图像文件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3081262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com