色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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();
        }

AspNet技術ASP.NET中高質量縮略圖的生成代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 稷山县| 保康县| 高安市| 青阳县| 海门市| 鸡泽县| 温泉县| 上高县| 石林| 新源县| 中宁县| 广安市| 宁阳县| 滦南县| 六盘水市| 海门市| 东辽县| 墨竹工卡县| 临猗县| 日土县| 陇川县| 崇义县| 青冈县| 海口市| 依兰县| 固始县| 临沂市| 库车县| 洞头县| 肃北| 梅河口市| 申扎县| 九江市| 昆明市| 肇东市| 三河市| 保亭| 双柏县| 西和县| 大关县| 丹寨县|