Asp.Net 高清圖片縮圖產生

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   檔案   

產生縮圖核心代碼:

/// <summary>        /// 圖片上傳 產生縮圖        /// </summary>        /// <param name="files">檔案上傳控制項</param>        /// <param name="path">檔案夾名稱</param>        /// <param name="fname">檔案名稱</param>        /// <param name="w">縮圖寬度</param>        /// <param name="h">縮圖高度</param>        public static void UploadImageThumbs(HttpPostedFile files, string filePath, string fname, int w, int h)        {            if (UtilsFile.IsOrNoFileUp(files) && fname.Equals(String.Empty) == false)            {                UtilsFile.DirectorysCreate(filePath);                Image image = Image.FromStream(files.InputStream, true);                //等比例縮放                if (w > 0 && h > 0)                {                    if (image.Width > image.Height)                    {                        if (image.Width > w)                            h = (int)(image.Height * ((decimal)w / image.Width));                        else                        {                            h = image.Height;                            w = image.Width;                        }                    }                    else                    {                        if (image.Height > h)                            w = (int)(image.Width * ((decimal)h / image.Height));                        else                        {                            h = image.Height;                            w = image.Width;                        }                    }                }                else if (w > 0 && h == 0)                {                    if (image.Width < w)                        w = image.Width;                    h = (int)(image.Height * ((decimal)w / image.Width));                }                else if (w == 0 && h > 0)                {                    if (image.Height < h)                        h = image.Height;                    w = (int)(image.Width * ((decimal)h / image.Height));                }                else                {                    w = image.Width;                    h = image.Height;                }                Bitmap ret = new Bitmap(w, h);                using (Graphics g = Graphics.FromImage(ret))                {                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                    g.FillRectangle(Brushes.White, 0, 0, w, h);                    g.DrawImage(image, 0, 0, w, h);                    EncoderParameters parms = new EncoderParameters();                    long[] quality = new long[1];                    quality[0] = 80;                    EncoderParameter parm = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);                    parms.Param[0] = parm;                    ImageCodecInfo[] arr = ImageCodecInfo.GetImageEncoders();                    ImageCodecInfo ar = null;                    for (int x = 0; x < arr.Length; x++)                    {                        if (arr[x].FormatDescription.Equals("JPEG"))                        {                            ar = arr[x];                            break;                        }                    }                    ret.Save(filePath + fname, ar, parms);                    ret.Dispose();                    image.Dispose();                }            }        }

 

總結:以上產生縮圖代碼,效果是比較好的,就是有時候紅色的點會失真,比較無解。不過這問題基本可以忽略不計。較真的朋友,如有較好的產生代碼,可以共用。

原文轉載: http://www.dyxue.com/tech/id2498.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.