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

时间:2013-02-05 05:05:49

标签: c# multithreading io

当我尝试使用System.IO.File.Delete(....)删除图像时,我会收到错误异常。

IOException was unhandled by user code

The process cannot access the file 'xxx\image\6132_15658422-b0a1-45a9-b0f9-7e9af783ad53_Temp.jpg' because it is being used by another process.

有人可以给我一些建议吗?

<小时/> <小时/>

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

    int X1 = 70, Y1 = 20, X2 = 201, Y2 = 236, w = 800, h = 600;
    string filelocation = "image/6132_15658422-b0a1-45a9-b0f9-7e9af783ad53_Temp.jpg";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            using (System.Drawing.Image _Image = cropImage(
                                                ResizeImage(
                                                        System.Drawing.Image.FromFile( Server.MapPath("~") +"/"+  filelocation), w, h),
                                                        (new System.Drawing.Rectangle(X1, Y1, X2, Y2))))
            {
                _Image.Save(Server.MapPath("~") + "/" + "image/output.jpg");

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally {
            File.Delete(Server.MapPath("~") + "/" + filelocation);
        }
    }

对于裁剪图像功能,

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

                }
            }

2 个答案:

答案 0 :(得分:2)

即使您已完成所有Dispose()语句的匹配,您仍会发现问题仍然存在。 Image.FromFile在任何大量网站中都存在问题。解决方案是使用Image.FromStream代替。

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

使用明确的Dispose()using()语句或将值设置为null 无法解决问题,直到垃圾收集发生。强制垃圾收集发生通常是一个坏主意。

答案 1 :(得分:1)

您需要使用Image处理您创建的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;

相关问题