| 今天解决了一个图片缩略图报“A generic error occurred in GDI+”错误的问题 今天写了一个关于生成缩略图片的功能
 方法如下
 
 [C#] 纯文本查看 复制代码 /// <summary>
    /// 得到图片缩略图
    /// </summary>
    /// <param name="img">图片对象</param>
    /// <param name="maxWidth">最大宽度</param>
    /// <param name="maxHeight">最大高度</param>
    /// <returns></returns>
    public static System.Drawing.Image GetThumbImage(System.Drawing.Image img, int maxWidth, int maxHeight, bool isBlock, out float size)
    {
        int tWidth;
        int tHeight;
        size = 1;
        if (!isBlock)
        {
            tWidth = maxWidth;
            tHeight = maxHeight;
        }
        else
        {
            if (!(maxHeight > img.Height && maxWidth > img.Width))
            {
                float HeightMultipier = (float)maxHeight / (float)img.Height;
                float WidthMultipier = (float)maxWidth / (float)img.Width;
                if (HeightMultipier > 1) HeightMultipier = 1;
                if (WidthMultipier > 1) WidthMultipier = 1;
                float SizeMultiplier = WidthMultipier < HeightMultipier ? WidthMultipier : HeightMultipier;
                size = SizeMultiplier;
                tWidth = (int)(img.Width * SizeMultiplier);
                tHeight = (int)(img.Height * SizeMultiplier);
            }
            else
            {
                tWidth = img.Width;
                tHeight = img.Height;
            }
        }
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(tWidth, tHeight);
        //新建一个画板
        Graphics g = System.Drawing.Graphics.FromImage(bitmap);
        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //清空画布并以透明背景色填充
        g.Clear(Color.Transparent);
        //在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(img, new Rectangle(0, 0, tWidth, tHeight),
            new Rectangle(0, 0, img.Width, img.Height),
            GraphicsUnit.Pixel);
        return bitmap;
    }在本地测试的好好的,上传到服务器就出问题,
 错误如下
 
 A generic error occurred in GDI+   就是在调用这个方法时报这个问题
 而且是一台服务器有问题一台没有问题,
 我细看了下服务器一个是iis7另一个是IIS6,难道是IIS兼容性的问题
 于是我将代码修改如下
 
 [C#] 纯文本查看 复制代码 using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))
        {
            System.Drawing.Image.GetThumbnailImageAbort callback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            originalImage.GetThumbnailImage(497, 367, callback, IntPtr.Zero).Save(filePath);
        }
    }
    public bool ThumbnailCallback()
    {
        return false;
    }不过还是问题依旧,依然是报这样的错误
 会不会是位图的问题我查看了MSn的文章http://support.microsoft.com/?id=814675
 修改之后还是这样的问题
 这时我想到有可能不是代码的问题了。
 我到文件所在的服务器上查看了一下目录的权限,我靠原是来目录权限问题
 设置用户everyone的读写权限就可以的。大家如果有这种要先检查下是不是权限问题哦,不要忙着修改别的代码,像我一样忙了半天没找到问题所在,呵呵
 
 
 |