■ 기준선을 사용해 이미지를 회전하는 방법을 보여준다.
▶ MainForm.cs
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 비트맵 /// </summary> private Bitmap sourceBitmap = null; /// <summary> /// 타겟 비트맵 /// </summary> private Bitmap targetBitmap = null; /// <summary> /// 각도 /// </summary> private float angle = 0; /// <summary> /// 시작 포인트 /// </summary> private Point startPoint = new Point(50, 100); /// <summary> /// 종료 포인트 /// </summary> private Point endPoint = new Point(210, 100); /// <summary> /// 그리기 여부 /// </summary> private bool isDrawing = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.openMenuItem.Click += openMenuItem_Click; this.saveAsMenuItem.Click += saveAsMenuItem_Click; this.exitMenuItem.Click += exitMenuItem_Click; this.resetAngleMenuItem.Click += resetAngleMenuItem_Click; this.drawGridMenuItem.Click += drawGridMenuItem_Click; this.pictureBox.MouseDown += pictureBox_MouseDown; this.pictureBox.MouseMove += pictureBox_MouseMove; this.pictureBox.MouseUp += pictureBox_MouseUp; this.pictureBox.Paint += pictureBox_Paint; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 열기 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e) /// <summary> /// 열기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openMenuItem_Click(object sender, EventArgs e) { if(this.openFileDialog.ShowDialog() == DialogResult.OK) { this.sourceBitmap = LoadBitmapUnlocked(this.openFileDialog.FileName); this.angle = 0; DrawImage(); } } #endregion #region 다른 이름으로 저장 메뉴 항목 클릭시 처리하기 - saveAsMenuItem_Click(sender, e) /// <summary> /// 다른 이름으로 저장 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveAsMenuItem_Click(object sender, EventArgs e) { if(this.saveFileDialog.ShowDialog() == DialogResult.OK) { SaveImage(this.targetBitmap, this.saveFileDialog.FileName); } } #endregion #region 종료 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e) /// <summary> /// 종료 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void exitMenuItem_Click(object sender, EventArgs e) { Close(); } #endregion #region 각도 리셋 메뉴 항목 클릭시 처리하기 - resetAngleMenuItem_Click(sender, e) /// <summary> /// 각도 리셋 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void resetAngleMenuItem_Click(object sender, EventArgs e) { this.angle = 0; this.startPoint = new Point(50, 100); this.endPoint = new Point(210, 100); DrawImage(); } #endregion #region 그리드 그리기 - drawGridMenuItem_Click(sender, e) /// <summary> /// 그리드 그리기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void drawGridMenuItem_Click(object sender, EventArgs e) { this.pictureBox.Refresh(); } #endregion #region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e) /// <summary> /// 픽처 박스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseDown(object sender, MouseEventArgs e) { this.startPoint = e.Location; this.endPoint = e.Location; this.isDrawing = true; } #endregion #region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e) /// <summary> /// 픽처 박스 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseMove(object sender, MouseEventArgs e) { if(!this.isDrawing) { return; } this.endPoint = e.Location; this.pictureBox.Refresh(); } #endregion #region 픽처 박스 마우스 UP 처리하기 - pictureBox_MouseUp(sender, e) /// <summary> /// 픽처 박스 마우스 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseUp(object sender, MouseEventArgs e) { this.isDrawing = false; int deltaX = this.endPoint.X - this.startPoint.X; int deltaY = this.endPoint.Y - this.startPoint.Y; double newAngle = Math.Atan2(deltaY, deltaX) * 180.0 / Math.PI; this.angle -= (float)newAngle; DrawImage(); } #endregion #region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e) /// <summary> /// 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_Paint(object sender, PaintEventArgs e) { if(this.isDrawing) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.DrawLine(Pens.Yellow, this.startPoint, this.endPoint); using(Pen pen = new Pen(Color.Red)) { pen.DashPattern = new float[] { 5, 5 }; e.Graphics.DrawLine(pen, this.startPoint, this.endPoint); } } if(this.drawGridMenuItem.Checked) { const int deltaX = 50; int maximumX = this.pictureBox.ClientSize.Width; int maximumY = this.pictureBox.ClientSize.Height; for(int x = 0; x < maximumX; x += deltaX) { e.Graphics.DrawLine(Pens.Silver, x, 0, x, maximumY); } for(int y = 0; y < maximumY; y += deltaX) { e.Graphics.DrawLine(Pens.Silver, 0, y, maximumX, y); } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 잠금없이 비트맵 로드하기 - LoadBitmapUnlocked(filePath) /// <summary> /// 잠금없이 비트맵 로드하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>비트맵</returns> private Bitmap LoadBitmapUnlocked(string filePath) { using(Bitmap bitmap = new Bitmap(filePath)) { return new Bitmap(bitmap); } } #endregion #region 이미지 저장하기 - SaveImage(image, filePath) /// <summary> /// 이미지 저장하기 /// </summary> /// <param name="image">이미지</param> /// <param name="filePath">파일 경로</param> public void SaveImage(Image image, string filePath) { string extension = Path.GetExtension(filePath); switch(extension.ToLower()) { case ".bmp" : image.Save(filePath, ImageFormat.Bmp ); break; case ".exif" : image.Save(filePath, ImageFormat.Exif); break; case ".gif" : image.Save(filePath, ImageFormat.Gif ); break; case ".jpg" : case ".jpeg" : image.Save(filePath, ImageFormat.Jpeg); break; case ".png" : image.Save(filePath, ImageFormat.Png ); break; case ".tif" : case ".tiff" : image.Save(filePath, ImageFormat.Tiff); break; default : throw new NotSupportedException("Unknown file extension " + extension); } } #endregion #region 이미지 그리기 - DrawImage() /// <summary> /// 이미지 그리기 /// </summary> private void DrawImage() { if(this.sourceBitmap == null) { this.pictureBox.Refresh(); return; } int width = this.sourceBitmap.Width; int height = this.sourceBitmap.Height; Matrix matrix = new Matrix(); matrix.Rotate(this.angle); PointF[] pointArray = { new PointF(0 , 0 ), new PointF(width, 0 ), new PointF(0 , height), new PointF(width, height) }; matrix.TransformPoints(pointArray); float minimumX = pointArray[0].X; float maximumX = minimumX; float minimumY = pointArray[0].Y; float maximumY = minimumY; for(int i = 1; i < pointArray.Length; i++) { if(minimumX > pointArray[i].X) minimumX = pointArray[i].X; if(maximumX < pointArray[i].X) maximumX = pointArray[i].X; if(minimumY > pointArray[i].Y) minimumY = pointArray[i].Y; if(maximumY < pointArray[i].Y) maximumY = pointArray[i].Y; } float newWidth = maximumX - minimumX; float newHeight = maximumY - minimumY; matrix.Translate(newWidth / 2, newHeight / 2, MatrixOrder.Append); this.targetBitmap = new Bitmap((int)newWidth, (int)newHeight); using(Graphics graphics = Graphics.FromImage(this.targetBitmap)) { graphics.InterpolationMode = InterpolationMode.High; graphics.Clear(Color.White); graphics.Transform = matrix; PointF[] targetPointArray = { new PointF(-width / 2, -height / 2), new PointF( width / 2, -height / 2), new PointF(-width / 2, height / 2) }; graphics.DrawImage(sourceBitmap, targetPointArray); } this.pictureBox.Image = this.targetBitmap; } #endregion } } |