C# 壓縮圖片 HttpPostedFile轉Image


                #region 以下代碼為保存圖片時,設置壓縮質量
                    Image iSource = Image.FromStream(file.InputStream);//HttpPostedFile轉Image
             long[] qy = new long[1];
  qy[0] = 10;//設置壓縮的比例1-100 ImageFormat tFormat
= iSource.RawFormat; int sW = iSource.Width, sH = iSource.Height; Tool tool = new Tool(); tool.GetPicZoomSize(ref sW, ref sH, 640, 360); Bitmap filemap = new Bitmap(640, 360); Graphics g = Graphics.FromImage(filemap); g.Clear(Color.WhiteSmoke); g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(iSource, new Rectangle((640 - sW) / 2, (360 - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel); g.Dispose(); //Bitmap filemap = (Bitmap)Image.FromStream(file.InputStream);//HttpPostedFile轉Bitmap EncoderParameters ep = new EncoderParameters(); EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } filemap.Save(FilePath + FileName + fileExt, jpegICIinfo, ep);//dFile是壓縮后的新路徑 filemap.Dispose(); #endregion

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM