C#圖片處理畫素質量等程式碼問題
阿新 • • 發佈:2018-11-01
private byte[] PutLogoInImage(Image commodityImage, Image priceImage, string activePrice, bool checked11, Image brandLogoImage = null) { byte[] result = null; Image im = commodityImage; using (Image mypriceImage = priceImage.Clone() as Image) { LogoPriceImage(mypriceImage, activePrice, checked11); //建立畫刷 using (Graphics g = Graphics.FromImage(im)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default; if (brandLogoImage != null) { using (Bitmap bm = new Bitmap(brandLogoImage)) { //打左上角 g.DrawImage(bm, new System.Drawing.Rectangle(0, 0, bm.Width, bm.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel); } } using (Bitmap bm = new Bitmap(mypriceImage)) { #region 縮圖 //var tempImg= bm.GetThumbnailImage(250, 250, null, new IntPtr()); //g.DrawImage(tempImg, new Rectangle(im.Width - tempImg.Width, im.Height - tempImg.Height, tempImg.Width, tempImg.Height)); #endregion //bm.MakeTransparent(Color.White); //註釋掉,否則底圖會對圖示有影響 zhjw 20151102 //打到右下角 g.DrawImage(bm, new Rectangle(im.Width - bm.Width, im.Height - bm.Height, bm.Width, bm.Height)); } } } //將儲存好的圖片byte[] using (MemoryStream ms = new MemoryStream()) { using (Bitmap bmp = new Bitmap(im))//必須建立一個新的物件 { var ici = GetImageEncoder(im); if (ici == null) { bmp.Save(ms, im.RawFormat); } else { using (var ep = new EncoderParameters { Param = new EncoderParameter[] { new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L) } }) { bmp.Save(ms, ici, ep); } } result = ms.ToArray(); } } return result; } private static ImageCodecInfo GetImageEncoder(Image img) { //關鍵質量控制 //獲取系統編碼型別陣列,包含了jpeg,bmp,png,gif,tiff var icis = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo i in icis) { if (img.RawFormat.Guid == i.FormatID)//圖片格式一致 //if (i.MimeType == "image/png") //預設取 png編碼器 { ici = i; break; } } return ici; }