■ Graphics 클래스에서 그래픽 변환을 사용하는 방법을 보여준다.
▶ 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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); SizeChanged += Form_SizeChanged; this.originalPictureBox.Paint += originalPictureBox_Paint; this.translatePictureBox.Paint += translatePictureBox_Paint; this.translateXTextBox.TextChanged += translateTextBox_TextChanged; this.translateYTextBox.TextChanged += translateTextBox_TextChanged; this.rotatePicutreBox.Paint += rotatePicutreBox_Paint; this.rotateAngleTextBox.TextChanged += rotateAngleTextBox_TextChanged; this.scalePictureBox.Paint += scalePictureBox_Paint; this.scaleXTextBox.TextChanged += scaleTextBox_TextChanged; this.scaleYTextBox.TextChanged += scaleTextBox_TextChanged; this.skewPictureBox.Paint += skewPictureBox_Paint; this.skewXTextBox.TextChanged += skewTextBox_TextChanged; this.skewYTextBox.TextChanged += skewTextBox_TextChanged; this.m11TextBox.TextChanged += matrixTextBox_TextChanged; this.m12TextBox.TextChanged += matrixTextBox_TextChanged; this.m21TextBox.TextChanged += matrixTextBox_TextChanged; this.m22TextBox.TextChanged += matrixTextBox_TextChanged; this.m31TextBox.TextChanged += matrixTextBox_TextChanged; this.m32TextBox.TextChanged += matrixTextBox_TextChanged; this.matrixPictureBox.Paint += matrixPictureBox_Paint; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 크기 변경시 처리하기 - Form_SizeChanged(sender, e) /// <summary> /// 폼 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_SizeChanged(object sender, EventArgs e) { this.originalPictureBox.Refresh(); this.translatePictureBox.Refresh(); this.rotatePicutreBox.Refresh(); this.scalePictureBox.Refresh(); this.skewPictureBox.Refresh(); this.matrixPictureBox.Refresh(); } #endregion #region 원본 픽처 박스 페인트시 처리하기 - originalPictureBox_Paint(sender, e) /// <summary> /// 원본 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void originalPictureBox_Paint(object sender, PaintEventArgs e) { DrawPicture(sender as PictureBox, e.Graphics); } #endregion #region 이동 텍스트 박스 텍스트 변경시 처리하기 - translateTextBox_TextChanged(sender, e) /// <summary> /// 이동 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void translateTextBox_TextChanged(object sender, EventArgs e) { this.translatePictureBox.Refresh(); } #endregion #region 이동 픽처 박스 페인트시 처리하기 - translatePictureBox_Paint(sender, e) /// <summary> /// 이동 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void translatePictureBox_Paint(object sender, PaintEventArgs e) { float translateX; float translateY; float.TryParse(this.translateXTextBox.Text, out translateX); float.TryParse(this.translateYTextBox.Text, out translateY); e.Graphics.TranslateTransform(translateX, translateY); DrawPicture(sender as PictureBox, e.Graphics); } #endregion #region 회전 각도 텍스트 박스 텍스트 변경시 처리하기 - rotateAngleTextBox_TextChanged(sender, e) /// <summary> /// 회전 각도 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rotateAngleTextBox_TextChanged(object sender, EventArgs e) { this.rotatePicutreBox.Refresh(); } #endregion #region 회전 픽처 박스 페인트시 처리하기 - rotatePicutreBox_Paint(sender, e) /// <summary> /// 회전 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void rotatePicutreBox_Paint(object sender, PaintEventArgs e) { float angle; float.TryParse(this.rotateAngleTextBox.Text, out angle); e.Graphics.RotateTransform(angle); DrawPicture(sender as PictureBox, e.Graphics); } #endregion #region 확대/축소 텍스트 박스 텍스트 변경시 처리하기 - scaleTextBox_TextChanged(sender, e) /// <summary> /// 확대/축소 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleTextBox_TextChanged(object sender, EventArgs e) { this.scalePictureBox.Refresh(); } #endregion #region 확대/축소 픽처 박스 페인트시 처리하기 - scalePictureBox_Paint(sender, e) /// <summary> /// 확대/축소 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scalePictureBox_Paint(object sender, PaintEventArgs e) { float scaleX; float scaleY; float.TryParse(this.scaleXTextBox.Text, out scaleX); float.TryParse(this.scaleYTextBox.Text, out scaleY); try { e.Graphics.ScaleTransform(scaleX, scaleY); DrawPicture(sender as PictureBox, e.Graphics); } catch { } } #endregion #region 기울이기 텍스트 박스 텍스트 변경시 처리하기 - skewTextBox_TextChanged(sender, e) /// <summary> /// 기울이기 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void skewTextBox_TextChanged(object sender, EventArgs e) { this.skewPictureBox.Refresh(); } #endregion #region 기울이기 픽처 박스 페인트시 처리하기 - skewPictureBox_Paint(sender, e) /// <summary> /// 기울이기 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void skewPictureBox_Paint(object sender, PaintEventArgs e) { float skewX; float skewY; float.TryParse(this.skewXTextBox.Text, out skewX); float.TryParse(this.skewYTextBox.Text, out skewY); Matrix matrix = new Matrix(); matrix.Shear(skewX, skewY); e.Graphics.Transform = matrix; DrawPicture(sender as PictureBox, e.Graphics); } #endregion #region 매트릭스 텍스트 박스 텍스트 변경시 처리하기 - matrixTextBox_TextChanged(sender, e) /// <summary> /// 매트릭스 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void matrixTextBox_TextChanged(object sender, EventArgs e) { this.matrixPictureBox.Refresh(); } #endregion #region 매트릭스 픽처 박스 페인트시 처리하기 - matrixPictureBox_Paint(sender, e) /// <summary> /// 매트릭스 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void matrixPictureBox_Paint(object sender, PaintEventArgs e) { float translateX = 0; float translateY = 0; float scaleX = 1; float scaleY = 1; float skewY = 0; float skewX = 0; float.TryParse(this.m11TextBox.Text, out scaleX ); float.TryParse(this.m12TextBox.Text, out skewY ); float.TryParse(this.m21TextBox.Text, out skewX ); float.TryParse(this.m22TextBox.Text, out scaleY ); float.TryParse(this.m31TextBox.Text, out translateX); float.TryParse(this.m32TextBox.Text, out translateY); try { Matrix matrix = new Matrix(scaleX, skewY, skewX, scaleY, translateX, translateY); e.Graphics.Transform = matrix; DrawPicture(sender as PictureBox, e.Graphics); } catch { } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 웃는 얼굴 그리기 - DrawSmiley(graphics, boundRectangle) /// <summary> /// 웃는 얼굴 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="boundRectangle">테두리 사각형</param> private void DrawSmiley(Graphics graphics, RectangleF boundRectangle) { graphics.DrawRectangle(Pens.Green, boundRectangle); float thickness = boundRectangle.Width / 30; boundRectangle.Inflate(-thickness, -thickness); using(Pen pen = new Pen(Color.Red, thickness)) { graphics.FillEllipse(Brushes.Yellow, boundRectangle); graphics.DrawEllipse(pen, boundRectangle); pen.Color = Color.Black; RectangleF rectangle = boundRectangle; rectangle.Inflate ( -boundRectangle.Width / 6, -boundRectangle.Width / 6 ); graphics.DrawArc(pen, rectangle, 0, 180); pen.Width /= 2f; float centerX = boundRectangle.X + boundRectangle.Width * 0.5f; float centerY = boundRectangle.Y + boundRectangle.Height * 0.55f; float noseWidth = boundRectangle.Width * 0.2f; float noseHeight = boundRectangle.Width * 0.35f; RectangleF noseRectangle = new RectangleF ( centerX - noseWidth / 2f, centerY - noseHeight / 2f, noseWidth, noseHeight ); graphics.FillEllipse(Brushes.LightBlue, noseRectangle); pen.Color = Color.Blue; graphics.DrawEllipse(pen, noseRectangle); centerX = boundRectangle.X + boundRectangle.Width * 0.3f; centerY = boundRectangle.Y + boundRectangle.Height * 0.3f; float eyeWidth = noseWidth * 0.85f; float eyeHeight = noseHeight * 0.75f; RectangleF eyeRectangle = new RectangleF ( centerX - eyeWidth / 2f, centerY - eyeHeight / 2f, eyeWidth, eyeHeight ); graphics.FillEllipse(Brushes.White, eyeRectangle); pen.Color = Color.Black; graphics.DrawEllipse(pen, eyeRectangle); float pupilX = centerX + noseWidth / 4f; float pupilY = centerY; float pupilWidth = noseWidth / 2f; float pupilHeight = noseHeight / 2f; RectangleF pupilRectangle = new RectangleF ( pupilX - pupilWidth / 2f, pupilY - pupilHeight / 2f, pupilWidth, pupilHeight ); graphics.FillEllipse(Brushes.Black, pupilRectangle); centerX = boundRectangle.X + boundRectangle.Width * 0.7f; centerY = boundRectangle.Y + boundRectangle.Height * 0.3f; eyeWidth = noseWidth * 0.85f; eyeHeight = noseHeight * 0.75f; eyeRectangle = new RectangleF ( centerX - eyeWidth / 2f, centerY - eyeHeight / 2f, eyeWidth, eyeHeight ); graphics.FillEllipse(Brushes.White, eyeRectangle); pen.Color = Color.Black; graphics.DrawEllipse(pen, eyeRectangle); pupilX = centerX + noseWidth / 4f; pupilY = centerY; pupilWidth = noseWidth / 2f; pupilHeight = noseHeight / 2f; pupilRectangle = new RectangleF ( pupilX - pupilWidth / 2f, pupilY - pupilHeight / 2f, pupilWidth, pupilHeight ); graphics.FillEllipse(Brushes.Black, pupilRectangle); } } #endregion #region 그림 그리기 - DrawPicture(pictureBox, graphics) /// <summary> /// 그림 그리기 /// </summary> /// <param name="pictureBox">픽처 박스</param> /// <param name="graphics">그래픽스</param> private void DrawPicture(PictureBox pictureBox, Graphics graphics) { graphics.Clear(pictureBox.BackColor); graphics.SmoothingMode = SmoothingMode.AntiAlias; DrawSmiley(graphics, pictureBox.ClientRectangle); } #endregion } } |