■ Bitmap 클래스를 사용해 정사각형 크기 비트맵에 복사하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System.Drawing; using System.Drawing.Drawing2D; #region 복사하기 - Copy(sourceBitmap, squareSize) /// <summary> /// 복사하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="squareSize">정사각형 크기</param> /// <returns>비트맵</returns> public Bitmap Copy(Bitmap sourceBitmap, int squareSize) { float ratio = 1.0f; int maximumSide = sourceBitmap.Width > sourceBitmap.Height ? sourceBitmap.Width : sourceBitmap.Height; ratio = (float)maximumSide / (float)squareSize; Bitmap targetBitmap; if(sourceBitmap.Width > sourceBitmap.Height) { targetBitmap = new Bitmap(squareSize, (int)(sourceBitmap.Height / ratio)); } else { targetBitmap = new Bitmap((int)(sourceBitmap.Width / ratio), squareSize); } using(Graphics targetGraphics = Graphics.FromImage(targetBitmap)) { targetGraphics.CompositingQuality = CompositingQuality.HighQuality; targetGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; targetGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality; targetGraphics.DrawImage ( sourceBitmap, new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), GraphicsUnit.Pixel ); targetGraphics.Flush(); } return targetBitmap; } #endregion |