程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET中高質量縮略圖的生成代碼

ASP.NET中高質量縮略圖的生成代碼

編輯:ASP.NET基礎
private Size NewSize(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 Size(Convert.ToInt32(w), Convert.ToInt32(h));
        }

        private void SendSmallImage(string fileName, int maxWidth, int maxHeight)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(fileName));
            System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;

            Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
            Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
            Graphics g = Graphics.FromImage(outBmp);

            // 設置畫布的描繪質量
            g.CompositingQuality = CompositingQuality.HighQuality; 
            g.SmoothingMode = SmoothingMode.HighQuality; 
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
                0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
            g.Dispose();

            if (thisFormat.Equals(ImageFormat.Gif))
            {
                Response.ContentType = "image/gif";
            }
            else
            {
                Response.ContentType = "image/jpeg";
            }

            // 以下代碼為保存圖片時,設置壓縮質量
            EncoderParameters encoderParams = new EncoderParameters();
            long[] quality = new long[1];
            quality[0] = 100;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            encoderParams.Param[0] = encoderParam;

            //獲得包含有關內置圖像編碼解碼器的信息的ImageCodecInfo 對象。
            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICI = null;
            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICI = arrayICI[x];//設置JPEG編碼
                    break;
                }
            }

            if (jpegICI != null)
            {
                outBmp.Save(Response.OutputStream, jpegICI, encoderParams);
            }
            else
            {
                outBmp.Save(Response.OutputStream, thisFormat);
            }

            img.Dispose();
            outBmp.Dispose();
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved