C#图像裁剪调整大小删除

C#图像裁剪调整大小删除,c#,multithreading,io,C#,Multithreading,Io,当我尝试使用System.IO.File.delete(..)删除图像时,我得到错误异常 用户代码未处理IOException 进程无法访问该文件 “xxx\image\6132_15658422-b0a1-45a9-b0f9-7e9af783ad53_Temp.jpg” 因为 它正在被另一个进程使用。 谁能给我提个建议吗 我的主要功能是通过c#裁剪、调整大小和删除图像 对于作物图像功能 public System.Drawing.Image cropImage(System.Dr

当我尝试使用
System.IO.File.delete(..)
删除图像时,我得到错误异常


用户代码未处理IOException

进程无法访问该文件 “xxx\image\6132_15658422-b0a1-45a9-b0f9-7e9af783ad53_Temp.jpg” 因为 它正在被另一个进程使用。

谁能给我提个建议吗



我的主要功能是通过c#裁剪、调整大小和删除图像

对于作物图像功能

    public System.Drawing.Image cropImage(System.Drawing.Image image, Rectangle cropArea)
    {
        try
        {
            Bitmap bmpImage = new Bitmap(image);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (System.Drawing.Image)(bmpCrop);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public System.Drawing.Image ResizeImage(System.Drawing.Image image, int maxWidth, int maxHeight)
    {
        try
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);

            return newImage;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
对于“调整图像大小”功能

    public System.Drawing.Image cropImage(System.Drawing.Image image, Rectangle cropArea)
    {
        try
        {
            Bitmap bmpImage = new Bitmap(image);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (System.Drawing.Image)(bmpCrop);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public System.Drawing.Image ResizeImage(System.Drawing.Image image, int maxWidth, int maxHeight)
    {
        try
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);

            return newImage;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
[更新]

最后,我按照@landenedge的建议纠正了我的问题

          using (var mainImage = System.Drawing.Image.FromFile(Server.MapPath("~") +"/"+  filelocation)){
                using (System.Drawing.Image _Image = cropImage(
                                                    ResizeImage(mainImage, w, h),
                                                            (new System.Drawing.Rectangle(X1, Y1, X2, Y2))))
                {
                    _Image.Save(Server.MapPath("~") + "/" + "image/output.jpg");

                }
            }

您需要处理使用
FromFile()
创建的
图像。试着这样做:

using (var mainImage = Image.FromFile(Server.MapPath("~") +"/"+  filelocation), w, h))
using (var _Image = cropImage(ResizeImage(mainImage, new Rectangle(X1, Y1, X2, Y2))))
{
    _Image.Save(Server.MapPath("~") + "/" + "image/output.jpg");
}

另外,不要使用
throw ex重新抛出异常-这样做会重置堆栈跟踪,并可能删除有价值的调试信息。相反,只需使用
throw

即使在匹配了所有
Dispose()
语句之后,您仍会发现仍然存在问题
Image.FromFile
在任何高容量网站中都存在问题。解决方案是使用
Image.FromStream

   using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
   {
     using (Image original = Image.FromStream(fs))
     {
      ...

使用显式的
Dispose()
,使用
Using()
语句或将值设置为
null
在垃圾收集发生之前无法解决问题。强制执行垃圾收集通常是个坏主意。

提示:“它正在被另一个进程使用”…是的,但我不知道哪个进程已经被使用…可能是您自己的:运行ProcessMonitor…它是免费的您正在创建
位图的新实例。。但不要处理你打开的那些。您需要在从现有实例创建新实例后执行此操作。关于Image.FromFile vs Image.FromStream的值得注意的建议,谢谢@Ian Mercer