asp.net 生成缩略图问题

72 篇文章 0 订阅

有一些人问上传成功,生成缩略图问题。这是简单的老问题,把我的代码提出来分析

 /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="SourceImage">源图路径(含图片,物理路径)</param>
    /// <param name="ImageMapth">缩略图路径(含图片,物理路径)</param>
    /// <param name="width">缩略图宽</param>
    /// <param name="height">缩略图高</param>
    public static  void MakeImage(string sourceImage, string imageMapth, int width, int height, string mode)
    {
        System.Drawing.Image MyImage = System.Drawing.Image.FromFile(sourceImage);
        int towidth = width;
        int toheight = height;
        int x = 0, y = 0;
        int ow = MyImage.Width;
        int oh = MyImage.Height;
        switch (mode)
        {
            case "hw": break;
            case "width": toheight = MyImage.Height * width / MyImage.Width; break;
            case "height": towidth = MyImage.Width * height / MyImage.Height; break;
            case "cut":
                if ((double)MyImage.Width / (double)MyImage.Height > (double)towidth / (double)toheight)
                {
                    oh = MyImage.Height;
                    ow = MyImage.Width * towidth / toheight;
                    y = 0;
                    x = (MyImage.Width - ow) / 2;
                }
                else
                {
                    ow = MyImage.Width;
                    oh = MyImage.Height * height / toheight;
                    x = 0;
                    y = (MyImage.Height - oh) / 2;
                }; break;
            default: break;
        }

        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
        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(MyImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
        try
        {
            bitmap.Save(imageMapth, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch
        {
        }
        MyImage.Dispose();
        bitmap.Dispose();
        g.Dispose();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值