当前位置:编程学习 > C#/ASP.NET >>

图片等比例缩放报错----GDI+ 中发生一般性错误。

 protected void Page_Load(object sender, EventArgs e)
    {
        //./images/g_index/g_index_high_03.gif

        ImageChange("/PRC/image/DSC_0356.JPG", "/PRC/video/", 141, 94);

    }
    /// <summary>
    /// </summary>
    /// <param name="sourcePath">要缩放图片路径</param>
    /// <param name="savePath">缩放后图片路径</param>
    /// <param name="w">最大的宽度</param>
    /// <param name="h">最大的高度</param>
    public static void ImageChange(string sourcePath, string savePath, int w, int h)
    {
        string AllFolderPath = System.Web.HttpContext.Current.Server.MapPath("~" + sourcePath);
        string path = HttpContext.Current.Server.MapPath("~" + savePath);
        System.Drawing.Image _sourceImg = System.Drawing.Image.FromFile(AllFolderPath);
        //图片路径不存在时,创建图片文件夹
        if (!Directory.Exists(@path))
            Directory.CreateDirectory(@path);
        double _newW = (double)w, _newH = (double)h, t;
        if ((double)_sourceImg.Width > w)
        {
            t = (double)w;
        }
        else
        {
            t = (double)_sourceImg.Width;
        }

        if ((double)_sourceImg.Height * (double)t / (double)_sourceImg.Width > (double)h)
        {
            _newH = (double)h;
            _newW = (double)h / (double)_sourceImg.Height * (double)_sourceImg.Width;
        }
        else
        {
            _newW = t;
            _newH = (t / (double)_sourceImg.Width) * (double)_sourceImg.Height;
        }
        System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)_newW, (int)_newH);
        Graphics g = Graphics.FromImage(bitmap);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.Clear(Color.Transparent);
        g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel);
        _sourceImg.Dispose();
        g.Dispose();
        try
        {
            bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);

        }
        catch
        {
            bitmap.Dispose();
        }

        bitmap.Dispose();
    } 图片等比例缩放 --------------------编程问答--------------------
引用 楼主 Luohong815 的回复:
 protected void Page_Load(object sender, EventArgs e)
    {
        //./images/g_index/g_index_high_03.gif

        ImageChange("/PRC/image/DSC_0356.JPG", "/PRC/video/", 141, 94);

    }
    /// <summary>
    /// </summary>
    /// <param name="sourcePath">要缩放图片路径</param>
    /// <param name="savePath">缩放后图片路径</param>
    /// <param name="w">最大的宽度</param>
    /// <param name="h">最大的高度</param>
    public static void ImageChange(string sourcePath, string savePath, int w, int h)
    {
        string AllFolderPath = System.Web.HttpContext.Current.Server.MapPath("~" + sourcePath);
        string path = HttpContext.Current.Server.MapPath("~" + savePath);
        System.Drawing.Image _sourceImg = System.Drawing.Image.FromFile(AllFolderPath);
        //图片路径不存在时,创建图片文件夹
        if (!Directory.Exists(@path))
            Directory.CreateDirectory(@path);
        double _newW = (double)w, _newH = (double)h, t;
        if ((double)_sourceImg.Width > w)
        {
            t = (double)w;
        }
        else
        {
            t = (double)_sourceImg.Width;
        }

        if ((double)_sourceImg.Height * (double)t / (double)_sourceImg.Width > (double)h)
        {
            _newH = (double)h;
            _newW = (double)h / (double)_sourceImg.Height * (double)_sourceImg.Width;
        }
        else
        {
            _newW = t;
            _newH = (t / (double)_sourceImg.Width) * (double)_sourceImg.Height;
        }
        System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)_newW, (int)_newH);
        Graphics g = Graphics.FromImage(bitmap);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.Clear(Color.Transparent);
        g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel);
        _sourceImg.Dispose();
        g.Dispose();
        try
        {
            bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);

        }
        catch
        {
            bitmap.Dispose();
        }

        bitmap.Dispose();
    }


图片原图太大!你可以看下他的图片,估计有好几M把!缩小到1M一下,或者1M左右,应该就可以了!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,