簡體   English   中英

ImageResizer-進程無法訪問文件,因為該文件正在被另一個進程使用

[英]ImageResizer - The process cannot access the file because it is being used by another process

這行拋出錯誤: ImageBuilder.Current.Build(imgURL, imgURL, resizeImg);

知道為什么嗎?

public void setImgSize(string controlId, string filename)
{
    decimal currentWidth = 0;
    decimal currentHeight = 0;
    int maxFileWidth = 600;
    int maxFileHeight = 600;
    bool imageExists = false;
    string imgURL = "~/SODocs/" + SONum + "/" + filename;
    string imgPath = Server.MapPath(imgURL);
    if (File.Exists(imgPath))
    {
        imageExists = true;
        System.Drawing.Image imgFile = System.Drawing.Image.FromFile(imgPath);
        Image imgControl = FindControl(controlId) as Image;
        int maxDisplayWidth = 273;
        int maxDisplayHeight = 200;
        currentWidth = Convert.ToDecimal(imgFile.Width);
        currentHeight = Convert.ToDecimal(imgFile.Height);
        int newDisplayWidth = 0;
        int newDisplayHeight = 0;
        int paddingHeight = 0;

        if (currentHeight > currentWidth)
        {
            imgControl.Height = maxDisplayHeight;
            newDisplayWidth = Convert.ToInt32((maxDisplayHeight / currentHeight) * currentWidth);
            imgControl.Width = newDisplayWidth;
            imgControl.Style.Add("margin", "0 auto");
        }
        else if (currentWidth > currentHeight)
        {
            newDisplayHeight = Convert.ToInt32((maxDisplayWidth / currentWidth) * currentHeight);

            if (newDisplayHeight > maxDisplayHeight)
            {
                // set newWidth based on maxHeight
                newDisplayWidth = Convert.ToInt32((maxDisplayHeight / currentHeight) * currentWidth);
                imgControl.Width = newDisplayWidth;
                imgControl.Style.Add("margin", "0 auto");
            }
            else
            {
                imgControl.Width = maxDisplayWidth;
            }

            paddingHeight = maxDisplayHeight - newDisplayHeight;
            imgControl.Style.Add("padding-top", paddingHeight.ToString() + "px");
        }

        imgControl.ImageUrl = imgURL;
        imgFile.Dispose();
        imgFile = null;

        if (imageExists)
        {
            // resize the image file
            if (currentWidth > maxFileWidth | currentHeight > maxFileHeight)
            {
                var resizeImg = new ResizeSettings();
                resizeImg.MaxWidth = maxFileWidth;
                resizeImg.MaxHeight = maxFileHeight;

                ImageBuilder.Current.Build(imgURL, imgURL, resizeImg);

            }
        }
    }
}

您正在打開文件以自己單獨讀取,但是希望ImageResizer以后可以寫入該文件。 System.Drawing無法正確處理文件鎖定,並且.Dispose()不足以處理內存泄漏或您遇到的文件訪問問題。

我建議閱讀有關使用ImageResizer 的最佳實踐指南

您想要在這里完成的工作很可能可以通過URL API以更簡單,更不易崩潰的方式實現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM