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

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

                }
            }

最佳答案

即使你已经匹配了所有的Dispose()陈述你会发现你仍然有问题。 Image.FromFile在任何大容量网站中都是有问题的。解决方法是使用 Image.FromStream反而。

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

使用显式 Dispose() , 一个 using()语句或将值设置为 null 不能解决问题 直到垃圾收集发生。强制进行垃圾收集通常是一个坏主意。

关于C#图像裁剪调整大小删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14700538/

相关文章:

C#串口检查设备是否连接

c# - 键值对作为控制台应用程序中的参数

java - 当其他任务完成时,强制 BufferedWriter 从 BlockingQueue 写入

java - 如何在后台线程中启动服务器并知道服务器在启动时没有抛出异常?

c# - Action<> 对象作为函数的参数并使用它

InfoPath 表单中的 C# 字符串插值

multithreading - x86 保留 EFLAGS 位 1 == 0 : how can this happen?

c - 如何缓冲和延迟 printf() 输出?

performance - mzscheme 中的 I/O 性能

c++ - 如何从 c 中的文件中删除记录(保存的记录是 struct )