简体   繁体   English

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

[英]C# image crop resize delete

When I try to delete image by using System.IO.File.Delete(....) then I get error exception. 当我尝试使用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.

Could anyone please give me suggestion ? 有人可以给我一些建议吗?



My main function that crop, resize and delete image by c#. 我的主要功能是通过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);
        }
    }

For Crop Image Function, 对于裁剪图像功能,

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

For Resize Image Function, 对于调整图像大小功能,

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

    }

[Updated] [更新]

Finally I correct my problem as @landenedge suggested, 最后我纠正了我的问题,因为@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");

                }
            }

Even after you've matched up all your Dispose() statements you will find that you still have problems. 即使你已经匹配了所有的Dispose()语句,你仍会发现你仍然有问题。 Image.FromFile is problematic in any high volume web site. Image.FromFile在任何大量网站中都存在问题。 The solution is to use Image.FromStream instead. 解决方案是使用Image.FromStream

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

Using an explicit Dispose() , a using() statement or setting the value to null doesn't solve the issue until a garbage collection happens. 使用显式Dispose()using()语句或将值设置为null在垃圾收集发生之前不能解决问题 Forcing a garbage collection to happen is generally a bad idea. 强制垃圾收集发生通常是一个坏主意。

You need to dispose of the Image you create with FromFile() . 您需要处理使用FromFile()创建的Image Try something like this: 尝试这样的事情:

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

Also, don't rethrow exceptions with throw ex; 另外,不要用throw ex;重新抛出异常throw ex; - doing so resets the stack trace and can drop valuable debugging information. - 这样做会重置堆栈跟踪并丢弃有价值的调试信息。 Instead, just use throw; 相反,只需使用throw; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM