• 网站中的缩略图是如何生成的?(C#处理图像)


     网站中的缩略图是如何生成的?(C#处理图像)

    controller层代码:

    /// <summary>
            /// 生成缩略图
            /// </summary>
            /// <returns></returns>
            public ActionResult ThumImg()
            {

                Response.Clear();
                //string originalFileName = Server.MapPath(Request.QueryString["fn"]); //服务器上的目录名
                string originalFileName = Request.QueryString["fn"];//本地目录名
                int thumbnailWidth = Convert.ToInt16(Request.QueryString["tw"]);
                int thumbnailHeight = Convert.ToInt16(Request.QueryString["th"]);
                string bgColorString = Convert.ToString(Request.QueryString["bg"]);
                new ThumImage(originalFileName, thumbnailWidth, thumbnailHeight, bgColorString).ThumImg();
                return View();
            }


        }

    VIEW层代码:

       <%int j=0;
            foreach (var i in ViewData["ImgList"] as List<string>)
          {
              j++;
              if (j > 20) break;
                 %>
        <img src='/product/ThumImg?fn=<%=i %>&tw=50&th=50&bg=000000' border="0">
        <%} %>

    Coder层代码:

     public class ThumImage
        {
            string originalFileName;
            int thumbnailWidth;
            int thumbnailHeight;
            string bgColorString;
            public ThumImage(string _originalFileName, int _thumbnailWidth, int _thumbnailHeight, string _bgColorString)
            {
                this.originalFileName = _originalFileName;
                this.thumbnailWidth = _thumbnailWidth;
                this.thumbnailHeight = _thumbnailHeight;
                this.bgColorString = _bgColorString;
            }
            /// <summary>
            /// 生成缩略图
            /// </summary>
            /// <returns></returns>
            public void ThumImg()
            {

                HttpContext.Current.Response.Clear();
                #region 建立图像主体对象
                ColorConverter cv = new ColorConverter();
                Color bgColor = (Color)cv.ConvertFromString("#" + bgColorString);//通过string,生成一个color对象,ColorConverter是颜色转换方法
                System.Drawing.Image img = System.Drawing.Image.FromFile(originalFileName);//通过url得到image对象
                System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;//指定文件的文件格式,它是ImageFormat类型,不可承继
                System.Drawing.Size newSize = this.GetNewSize(thumbnailWidth, thumbnailHeight, img.Width, img.Height);//图像尺寸,GetNewSize实现等比例缩放
                System.Drawing.Bitmap outBmp = new Bitmap(thumbnailWidth, thumbnailHeight);//建立一个封闭GDI+ 位图对象
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outBmp);//建立一个GDI+ 位图画面,从指定的Image对象中
                #endregion

                #region 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.Default;
                g.SmoothingMode = SmoothingMode.Default;
                g.InterpolationMode = InterpolationMode.Default;
                g.Clear(bgColor);
                Rectangle r = new Rectangle((thumbnailWidth - newSize.Width) / 2,
                                               (thumbnailHeight - newSize.Height) / 2,
                                               newSize.Width, newSize.Height);
                g.DrawImage(img, r, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
                g.Dispose();
                #endregion

                #region 设置服务器响应文件头类型
                if (thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
                {
                    HttpContext.Current.Response.ContentType = "image/gif";//网页内容类型为图像的MIME
                }
                else
                {
                    HttpContext.Current.Response.ContentType = "image/jpeg";
                }
                #endregion

     

                //设置压缩质量
                System.Drawing.Imaging.EncoderParameters encoderParams = new EncoderParameters();
                System.Drawing.Imaging.EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { 100 });
                encoderParams.Param[0] = encoderParam;

                //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
                System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                System.Drawing.Imaging.ImageCodecInfo jpegICI = null;

                for (int fwd = 0; fwd < arrayICI.Length; fwd++)
                {
                    if (arrayICI[fwd].FormatDescription.Equals("JPEG"))
                    {
                        jpegICI = arrayICI[fwd];
                        break;
                    }
                }

                if (jpegICI == null)
                {
                    outBmp.Save(HttpContext.Current.Response.OutputStream, jpegICI, encoderParams);
                }
                else
                {
                    outBmp.Save(HttpContext.Current.Response.OutputStream, thisFormat); //保存的页面的输出流中
                }
            }

            /// <summary>
            /// 图像根据相应尺寸实现等比例缩放
            /// </summary>
            /// <param name="maxWidth"></param>
            /// <param name="maxHeight"></param>
            /// <param name="width"></param>
            /// <param name="height"></param>
            /// <returns></returns>
            System.Drawing.Size GetNewSize(int maxWidth, int maxHeight, int width, int height)
            {
                Double w = 0.0;
                Double h = 0.0;
                Double sw = Convert.ToDouble(width);
                Double sh = Convert.ToDouble(height);
                Double mw = Convert.ToDouble(maxWidth);
                Double mh = Convert.ToDouble(maxHeight);

                if (sw < mw && sh < mh)
                {
                    w = sw;
                    h = sh;
                }
                else if ((sw / sh) > (mw / mh))
                {
                    w = maxWidth;
                    h = (w * sh) / sw;
                }
                else
                {
                    h = maxHeight;
                    w = (h * sw) / sh;
                }
                return new System.Drawing.Size(Convert.ToInt32(w), Convert.ToInt32(h));
            }

        }

  • 相关阅读:
    佛山Uber优步司机奖励政策(2月1日~2月7日)
    长沙Uber优步司机奖励政策(2月1日~2月7日)
    广州Uber优步司机奖励政策(2月1日~2月7日)
    西安Uber优步司机奖励政策(2月1日~2月7日)
    武汉Uber优步司机奖励政策(2月1日~2月7日)
    Apicloud自定义模块
    Android Studio导出jar包
    android studio 、 as 如何导入eclipse项目
    安卓 使用Gradle生成正式签名apk文件
    如何用Android studio生成正式签名的APK文件
  • 原文地址:https://www.cnblogs.com/lori/p/2051220.html
Copyright © 2020-2023  润新知