C#剪下產生高品質縮放圖片

來源:互聯網
上載者:User

標籤:

/// <summary>        /// 高品質縮放圖片        /// </summary>        /// <param name="OriginFilePath">源圖的路徑</param>        /// <param name="TargetFilePath">儲存縮圖的路徑</param>        /// <param name="DestWidth">縮放後圖片寬度</param>        /// <param name="DestHeight">縮放後圖片高度</param>        /// <returns>表明此次操作是否成功</returns>        public static bool ToSuolue(System.IO.Stream oStream, string TargetFilePath, int towidth, int toheight)        {            //System.Drawing.Image OriginImage = System.Drawing.Image.FromFile(OriginFilePath);            System.Drawing.Image OriginImage = System.Drawing.Image.FromStream(oStream);            System.Drawing.Imaging.ImageFormat thisFormat = OriginImage.RawFormat;            //按比例縮放            int x = 0, y = 0;            int ow = OriginImage.Width;            int oh = OriginImage.Height;            if ((double)ow / (double)oh > (double)towidth / (double)toheight)            {                oh = OriginImage.Height;                ow = OriginImage.Height * towidth / toheight;                y = 0;                x = (OriginImage.Width - ow) / 2;            }            else            {                ow = OriginImage.Width;                oh = OriginImage.Width * toheight / towidth;                x = 0;                y = (OriginImage.Height - oh) / 2;            }            Bitmap bt = new Bitmap(towidth, toheight); //根據指定大小建立Bitmap執行個體            using (Graphics g = Graphics.FromImage(bt))            {                g.Clear(Color.White);                //設定畫布的描繪品質                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                g.DrawImage(OriginImage, new Rectangle(0, 0, towidth, toheight),                    new System.Drawing.Rectangle(x, y, ow, oh),                System.Drawing.GraphicsUnit.Pixel);                g.Dispose();            }            System.Drawing.Imaging.EncoderParameters EncoderParams = new System.Drawing.Imaging.EncoderParameters(); //取得內建的編碼器            long[] Quality = new long[1];            Quality[0] = 100;            System.Drawing.Imaging.EncoderParameter EncoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality);            EncoderParams.Param[0] = EncoderParam;            //try            //{                //獲得包含有關內建映像編碼解碼器的資訊的ImageCodecInfo 對象                System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();                System.Drawing.Imaging.ImageCodecInfo jpegICI = null;                for (int i = 0; i < arrayICI.Length; i++)                {                    if (arrayICI[i].FormatDescription.Equals("JPEG"))                    {                        jpegICI = arrayICI[i]; //設定為JPEG編碼方式                        break;                    }                }                if (jpegICI != null) //儲存縮圖                {                    bt.Save(TargetFilePath, jpegICI, EncoderParams);                }                else                {                    bt.Save(TargetFilePath, thisFormat);                }                OriginImage.Dispose();                return true;            //}            //catch (System.Runtime.InteropServices.ExternalException e1)  //GDI+發生一般錯誤            //{            //    return false;            //}            //catch (Exception e2)            //{            //    return false;            //}        }

 

C#剪下產生高品質縮放圖片

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.