.net生成固定大小缩略图,不变形

源码奉上

   /// <summary>
        /// 获取图片文件,支持远程路径
        /// </summary>
        /// <param name="SourcePath">路径或图片网址</param>
        /// <returns></returns>
        public static System.Drawing.Image GetImage(string SourcePath)
        {
            if (SourcePath.StartsWith("http"))
            {
                System.Net.WebRequest request = System.Net.WebRequest.Create(SourcePath);
                request.Timeout = 10000;
                System.Net.HttpWebResponse httpresponse = (System.Net.HttpWebResponse)request.GetResponse();
                Stream s = httpresponse.GetResponseStream();
                return System.Drawing.Image.FromStream(s);
            }
            else
            {
                return System.Drawing.Image.FromFile(SourcePath);
            }
        }
        /// <summary>
        /// 创建指定大小的图片文件
        /// </summary>
        /// <param name="width">图片文件宽度</param>
        /// <param name="height">图片文件高度</param>
        /// <param name="SoursePath">源图片物理路径</param>
        /// <param name="TargetPath">生成图片存储物理路径</param>
        /// <param name="OpenBorder">是否开启边框</param>
        /// <param name="ColorHex">边框颜色</param>
        public static void CreateImageOutput(int width, int height, string SourcePath, string TargetPath, bool OpenBorder, string ColorHex)
        {
            Bitmap originalBmp = null;
            originalBmp = new Bitmap(GetImage(SourcePath));
            // 源图像在新图像中的位置 
            int left, top;




            if (originalBmp.Width <= width && originalBmp.Height <= height)
            {
                // 原图像的宽度和高度都小于生成的图片大小 
                left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);
                top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);
                // 最终生成的图像 
                Bitmap bmpOut = new Bitmap(width, height);
                using (Graphics graphics = Graphics.FromImage(bmpOut))
                {
                    // 设置高质量插值法 
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    // 清空画布并以白色背景色填充 
                    graphics.Clear(Color.White);
                    //加上边框 
                    if (OpenBorder)
                    {
                        if (ColorHex == "")
                        {
                            ColorHex = "#cccccc";
                        }
                        Pen pen = new Pen(ColorTranslator.FromHtml(ColorHex));
                        graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);
                    }
                    // 把源图画到新的画布上 
                    graphics.DrawImage(originalBmp, left, top);
                }
                bmpOut.Save(TargetPath);//保存为文件,tpath 为要保存的路径 
                bmpOut.Dispose();
                return;
            }
            // 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形, 
            // 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上 
            int newWidth, newHeight;
            if (width * originalBmp.Height < height * originalBmp.Width)
            {
                newWidth = width;
                newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);
                // 缩放成宽度跟预定义的宽度相同的,即left=0,计算top 
                left = 0;
                top = (int)Math.Round((decimal)(height - newHeight) / 2);
            }
            else
            {
                newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);
                newHeight = height;
                // 缩放成高度跟预定义的高度相同的,即top=0,计算left 
                left = (int)Math.Round((decimal)(width - newWidth) / 2);
                top = 0;
            }




            // 生成按比例缩放的图,如:160*80的图 
            Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);
            using (Graphics graphics = Graphics.FromImage(bmpOut2))
            {
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
                graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
            }
            // 再把该图画到预先定义的宽高的画布上,如160*120 
            Bitmap lastbmp = new Bitmap(width, height);
            using (Graphics graphics = Graphics.FromImage(lastbmp))
            {
                // 设置高质量插值法 
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                // 清空画布并以白色背景色填充 
                graphics.Clear(Color.White);
                //加上边框 
                if (OpenBorder)
                {
                    if (ColorHex == "")
                    {
                        ColorHex = "#cccccc";
                    }
                    Pen pen = new Pen(ColorTranslator.FromHtml(ColorHex));
                    graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);
                }
                // 把源图画到新的画布上 
                graphics.DrawImage(bmpOut2, left, top);
            }
            lastbmp.Save(TargetPath);//保存为文件,tpath 为要保存的路径 
            lastbmp.Dispose();
        }


个人博客:http://blog.amtemai.com

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值