网站中的缩略图是什么转变的?(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);//建立2个封闭GDI+
位图对象
            System.Drawing.Graphics g =
System.Drawing.Graphics.FromImage(outBmp);//建立2个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));
        }

    }

相关文章