保存文件多次失败,GDI +中发生一般错误。例外

时间:2014-09-30 08:33:36

标签: c# .net gdi

以下是有问题的代码。 Stream来自HttpPostedFile。在当地测试很好。 在线测试也是第一次覆盖时没问题。只有在尝试多次覆盖文件时才会抛出异常。例如。上传然后重新上传任何见解?

    public void ResizeFromStream(string ImageSavePath, int MaxSideSize, Stream Buffer)
    {   
        int intNewWidth;
        int intNewHeight;
        System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Buffer);

        //Determine image format 
        ImageFormat fmtImageFormat = imgInput.RawFormat;

        //get image original width and height 
        int intOldWidth = imgInput.Width;
        int intOldHeight = imgInput.Height;

        //determine if landscape or portrait 
        int intMaxSide;

        if (intOldWidth >= intOldHeight)
        {
            intMaxSide = intOldWidth;
        }
        else
        {
            intMaxSide = intOldHeight;
        }

        Bitmap bmpResized = null;
        if (intMaxSide > MaxSideSize)
        {
            //set new width and height 
            double dblCoef = MaxSideSize / (double)intMaxSide;
            intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
            intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);

            bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);
        }
        else
        {
            intNewWidth = intOldWidth;
            intNewHeight = intOldHeight;
            bmpResized = new Bitmap(Buffer);
        }

        //save bitmap to disk
        bmpResized.Save(ImageSavePath, fmtImageFormat);

        //release used resources 
        imgInput.Dispose();
        bmpResized.Dispose();
        Buffer.Dispose();

    }

0 个答案:

没有答案