Winform 에서 Image Resize 할 때 사용되는 코드입니다.
Stack Overflow에서 보았던 소스를 적어 둡니다.
public static System.Drawing.Image Resize(Image image, int Width, int Height, bool needToFill) { int sourceWidth = image.Width; int sourceHeight = image.Height; int sourceX = 0; int sourceY = 0; double destX = 0; double destY = 0; double nScale = 0; double nScaleW = 0; double nScaleH = 0; nScaleW = ((double)Width / (double)sourceWidth); nScaleH = ((double)Height / (double)sourceHeight); if (!needToFill) { nScale = Math.Min(nScaleH, nScaleW); } else { nScale = Math.Max(nScaleH, nScaleW); destY = (Height - sourceHeight * nScale) / 2; destX = (Width - sourceWidth * nScale) / 2; } if (nScale > 1) nScale = 1; int destWidth = (int)Math.Round(sourceWidth * nScale); int destHeight = (int)Math.Round(sourceHeight * nScale); System.Drawing.Bitmap bmPhoto = null; try { bmPhoto = new System.Drawing.Bitmap(destWidth + (int)Math.Round(2 * destX), destHeight + (int)Math.Round(2 * destY)); } catch (Exception ex) { throw new ApplicationException(string.Format("destWidth:{0}, destX:{1}, destHeight:{2}, desxtY:{3}, Width:{4}, Height:{5}", destWidth, destX, destHeight, destY, Width, Height), ex); } using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)) { //grPhoto.InterpolationMode = _interpolationMode; //grPhoto.CompositingQuality = _compositingQuality; //grPhoto.SmoothingMode = _smoothingMode; Rectangle to = new System.Drawing.Rectangle((int)Math.Round(destX), (int)Math.Round(destY), destWidth, destHeight); Rectangle from = new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight); grPhoto.DrawImage(image, to, from, System.Drawing.GraphicsUnit.Pixel); return bmPhoto; } }
'The Piece of Code' 카테고리의 다른 글
[C#] Captcha (1) | 2016.02.10 |
---|---|
[C#] Image Resize - ASP.NET (0) | 2016.02.10 |
[CSS] margin, pdding 순서 (0) | 2015.02.03 |
[javascript] 모바일 기기 구분하기 (0) | 2013.02.28 |
[C#] StripHtml (1) | 2013.01.20 |