如何发布ImageResizer文件句柄?

时间:2014-12-10 17:13:22

标签: c# bitmap imageresizer

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

我在Web项目中使用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(&#34; sample.jpg&#34;)方法,图片将会旋转。如果我刷新www.foobar.net/Pictures/sample.jpg,它与第一次不同,图像已被转换。现在,我打电话给www.foobar.net/Pictures/sample.jpg?width=128,我再次尝试调用RotateImage(&#34; sample.jpg&#34;),将出现正在使用的文件异常。

编辑#2

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

0 个答案:

没有答案