繁体   English   中英

如何释放ImageResizer文件句柄?

[英]How to release ImageResizer file handle?

我使用ImageResizer已有很长时间了,但是今天,我遇到了一个我无法解决的问题。

我在网络项目中使用ImageResizer来显示一些缩略图。 用户可以通过单击缩略图选择图片之一,然后应该能够编辑图像(旋转)。

ImageResizer仅用于缩略图,使用URL api(img.jpg?width = 128)。 旋转/图像编辑是通过GDI +(System.Drawing.Bitmap RotateFlip)完成的。

如果我只调用rotate方法,并访问图像URL(而不调用ImageResizer),则可以正常工作。 我可以调用任意数量的Rotate方法,并且始终可以获得预期的结果。

但是,一旦我访问ImageResizer URL API,该文件就会被ImageResizer使用,并且无法旋转。 当我尝试保存我的位图图像时,发生异常:“该进程无法访问该文件,因为该文件正在被另一个进程使用”。

为什么ImageResizer保留文件的句柄? 如何强制它释放文件句柄?

预先感谢

编辑#1

这是与ImgRzr web.config相关的行:

<configuration>
    <configSections>
        <section name="resizer" type="ImageResizer.ResizerSection" />
    </configSections>

    <system.web>
        <httpModules>
            <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
        </httpModules>
    </system.web>


    <system.webServer>
        <modules runAllManagedModulesForAllRequests="false">
            <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
        </modules>            
    </system.webServer>

    <resizer>
        <sizelimits imageWidth="0" imageHeight="0" totalWidth="6400" totalHeight="6400" totalBehavior="throwexception" />
        <remotereader signingKey="ag383ht2A162aCDe84fZ1pRfc2F5GhpB2twfqw" allowAllSignedRequests="true" allowRedirects="5">
        <allow domain="foobar.net" onlyWhenSigned="false" />
        <allow domain="localhost" onlyWhenSigned="false" />
        <allow domain="*.indev.be" onlyWhenSigned="false" />
        <!-- XML whitelisting requires V3.2 or higher -->
        <allow domain="*.foobar.net" onlyWhenSigned="false" />
        </remotereader>
        <plugins>
        <add name="RemoteReader" />
        <add name="DiskCache" />
        <add name="PdfRenderer" downloadNativeDependencies="true" />
        </plugins>
        <diskcache dir="~/Content/imagescache" />
    </resizer>
</configuration>

这是用于旋转的方法:

public JsonResult RotateImage(string imageName)
{
    string imagesPath = @"/Pictures/";
    string fullPath = Server.MapPath(imagesPath + imageName);
    if (System.IO.File.Exists(fullPath))
    {
        try
        {
            using (Bitmap image = new Bitmap(fullPath))
            {
                image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                image.Save(fullPath + "new", image.RawFormat);
                image.Dispose();
            }
            // I tried this trick because saving directly on the image was throwing the file is use exception
            System.IO.File.Delete(fullPath);
            System.IO.File.Move(fullPath + "new", fullPath);

            /* I also trie to use ImageResizer but I've got the exact same error                
            ImageResizer.ImageBuilder.Current.Build(fullPath, fullPath, new ImageResizer.ResizeSettings("srotate=90"));
            */
        }
        catch (Exception ex)
        {
            Logger.Log(ex);
            return Json("error");
        }
    }
    return Json("ok");
}

现在,假设在我的网站的图片存储库中有一张名为sample.jpg的图片。 我可以通过www.foobar.net/Pictures/sample.jpg访问它。 如果我使用RotateImage(“ sample.jpg”)方法,则图片将被旋转。 如果刷新www.foobar.net/Pictures/sample.jpg,则与第一次不同,图像已转换。 现在,我调用www.foobar.net/Pictures/sample.jpg?width=128,然后尝试再次调用RotateImage(“ sample.jpg”),出现使用中的文件异常。

编辑#2

我被误导了。 我试图为您设置一个示例项目,但也看到了问题,但工作正常。 它来自其他地方。

暂无
暂无

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

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