C#生成缩略图代码

//生成缩略图
    protected void RebuildImage(string oldImgPath,string newName, int intThumbWidth, int intThumbHeight)
    {
        try
        {
            //原图加载
            using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(oldImgPath))
            {
                //原图宽度和高度
                int width = sourceImage.Width;
                int height = sourceImage.Height;
                int smallWidth;
                int smallHeight;
                //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
                if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
                {
                    smallWidth = intThumbWidth;
                    smallHeight = intThumbWidth * height / width;
                }
                else
                {
                    smallWidth = intThumbHeight * width / height;
                    smallHeight = intThumbHeight;
                }
                //判断缩略图在当前文件夹下是否同名称文件存在
                /**
                file_append = 0;
                sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
                while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
                {
                    file_append++;
                    sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
                    file_append.ToString() + extendName;
                }
                 * */

                //缩略图保存的绝对路径
                string smallImagePath = System.Web.HttpContext.Current.Server.MapPath("~/cn/UploadFiles/casesmall/" + "s_" + newName);

                //新建一个图板,以最小等比例压缩大小绘制原图
                using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
                {
                    //绘制中间图
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                    {
                        //高清,平滑
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.Clear(Color.Black);
                        g.DrawImage(
                        sourceImage,
                        new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
                        new System.Drawing.Rectangle(0, 0, width, height),
                        System.Drawing.GraphicsUnit.Pixel
                        );
                    }
                    //新建一个图板,以缩略图大小绘制中间图
                    using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
                    {
                        //绘制缩略图
                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
                        {
                            //高清,平滑
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            g.Clear(Color.Black);
                            int lwidth = (smallWidth - intThumbWidth) / 2;
                            int bheight = (smallHeight - intThumbHeight) / 2;
                            g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
                            g.Dispose();
                            bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }
                }
            }
        }
        catch(Exception e)
        {
            Response.Write("<script>alert('"+e+"')</script>");
            //出错则删除
            //System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
            //return "图片格式不正确";
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值