■ Bitmap 클래스에서 스크롤 가능한 육각형 몽타주 비트맵을 사용하는 방법을 보여준다.
▶ HexagonHelper.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 |
using System; using System.Drawing; using System.Drawing.Drawing2D; namespace TestProject { /// <summary> /// 육각형 헬퍼 /// </summary> public static class HexagonHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 육각형 너비 구하기 - GetHexgonWidth(height) /// <summary> /// 육각형 너비 구하기 /// </summary> /// <param name="height">높이</param> /// <returns>육각형 너비</returns> public static float GetHexgonWidth(float height) { return (float)(4 * (height / 2 / Math.Sqrt(3))); } #endregion #region 육각형 인덱스 설정하기 - SetHexgonIndex(x, y, height, row, column) /// <summary> /// 육각형 인덱스 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="height">육각형 높이</param> /// <param name="row">행</param> /// <param name="column">열</param> public static void SetHexgonIndex(float x, float y, float height, out int row, out int column) { float width = GetHexgonWidth(height); column = (int)(x / (width * 0.75f)); if(column % 2 == 0) { row = (int)Math.Floor(y / height); } else { row = (int)Math.Floor((y - height / 2) / height); } float testX = column * width * 0.75f; float testY = row * height; if(column % 2 != 0) { testY += height / 2; } bool isAbove = false; bool isBelow = false; float deltaX = x - testX; if(deltaX < width / 4) { float deltaY = y - (testY + height / 2); if(deltaX < 0.001) { if(deltaY < 0) { isAbove = true; } if(deltaY > 0) { isBelow = true; } } else if(deltaY < 0) { if(-deltaY / deltaX > Math.Sqrt(3)) { isAbove = true; } } else { if(deltaY / deltaX > Math.Sqrt(3)) { isBelow = true; } } } if(isAbove) { if(column % 2 == 0) { row--; } column--; } else if(isBelow) { if(column % 2 != 0) { row++; } column--; } } #endregion #region 포인트 배열 구하기 - GetPointArray(height, row, column, minimumX, minimumY) /// <summary> /// 포인트 배열 구하기 /// </summary> /// <param name="height">육각형 높이</param> /// <param name="row">행</param> /// <param name="column">열</param> /// <param name="minimumX">최소 X</param> /// <param name="minimumY">최소 Y</param> /// <returns>포인트 배열</returns> public static PointF[] GetPointArray(float height, float row, float column, float minimumX, float minimumY) { float width = GetHexgonWidth(height); float y = height / 2; float x = 0; y += row * height; if(column % 2 != 0) { y += height / 2; } x += column * (width * 0.75f); return new PointF[] { new PointF(minimumX + x , minimumY + y ), new PointF(minimumX + x + width * 0.25f, minimumY + y - height / 2), new PointF(minimumX + x + width * 0.75f, minimumY + y - height / 2), new PointF(minimumX + x + width , minimumY + y ), new PointF(minimumX + x + width * 0.75f, minimumY + y + height / 2), new PointF(minimumX + x + width * 0.25f, minimumY + y + height / 2) }; } #endregion #region 육각형 그리드 그리기 - DrawHexgonGrid(graphics, pen, minimumX, maximumX, minimumY, maximumY, height) /// <summary> /// 육각형 그리드 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="pen">펜</param> /// <param name="minimumX">최소 X</param> /// <param name="maximumX">최대 X</param> /// <param name="minimumY">최소 Y</param> /// <param name="maximumY">최대 Y</param> /// <param name="height">육각형 높이</param> public static void DrawHexgonGrid(Graphics graphics, Pen pen, float minimumX, float maximumX, float minimumY, float maximumY, float height) { for(int row = 0; ; row++) { PointF[] pointArray = HexagonHelper.GetPointArray(height, row, 0, minimumX, minimumY); if(pointArray[4].Y > maximumY) { break; } for(int column = 0; ; column++) { pointArray = HexagonHelper.GetPointArray(height, row, column, minimumX, minimumX); if(pointArray[3].X > maximumX) { break; } if(pointArray[4].Y <= maximumY) { graphics.DrawPolygon(pen, pointArray); } } } } #endregion #region 다각형 내에서 이미지 그리기 - DrawImageInPolygon(graphcis, pointArray, image) /// <summary> /// 다각형 내에서 이미지 그리기 /// </summary> /// <param name="graphcis">그래픽스</param> /// <param name="pointArray">포인트 배열</param> /// <param name="image">이미지</param> public static void DrawImageInPolygon(Graphics graphcis, PointF[] pointArray, Image image) { float minimumX; float maximumX; float minimumY; float maximumY; SetPolygonBoundRectangle(pointArray, out minimumX, out maximumX, out minimumY, out maximumY); float width = maximumX - minimumX; float height = maximumY - minimumY; float centerX = (minimumX + maximumX) / 2f; float centerY = (minimumY + maximumY) / 2f; float scaleX = width / image.Width; float scaleY = height / image.Height; float scale = Math.Max(scaleX, scaleY); float scaledWdth = image.Width * scale; float scaledHeight = image.Height * scale; float radiusX = scaledWdth / 2f; float radiusY = scaledHeight / 2f; RectangleF sourceRectangle = new RectangleF(0, 0, image.Width, image.Height); PointF[] targetPointArray = { new PointF(centerX - radiusX, centerY - radiusY), new PointF(centerX + radiusX, centerY - radiusY), new PointF(centerX - radiusX, centerY + radiusY) }; GraphicsPath graphicsPath = new GraphicsPath(); graphicsPath.AddPolygon(pointArray); GraphicsState graphicsState = graphcis.Save(); graphcis.SetClip(graphicsPath); graphcis.DrawImage(image, targetPointArray, sourceRectangle, GraphicsUnit.Pixel); graphcis.Restore(graphicsState); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 다각형 테두리 사각형 설정하기 - SetPolygonBoundRectangle(pointArray, minimumX, maximumX, minimumY, maximumY) /// <summary> /// 다각형 테두리 사각형 설정하기 /// </summary> /// <param name="pointArray">포인트 배열</param> /// <param name="minimumX">최소 X</param> /// <param name="maximumX">최대 X</param> /// <param name="minimumY">최소 Y</param> /// <param name="maximumY">최대 Y</param> private static void SetPolygonBoundRectangle(PointF[] pointArray, out float minimumX, out float maximumX, out float minimumY, out float maximumY) { minimumX = pointArray[0].X; maximumX = minimumX; minimumY = pointArray[0].Y; maximumY = minimumY; foreach(PointF point in pointArray) { if(minimumX > point.X) { minimumX = point.X; } if(maximumX < point.X) { maximumX = point.X; } if(minimumY > point.Y) { minimumY = point.Y; } if(maximumY < point.Y) { maximumY = point.Y; } } } #endregion } } |
▶ Hexagon.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 |
using System.Drawing; using System.IO; namespace TestProject { /// <summary> /// 육각형 /// </summary> public class Hexagon { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 행 /// </summary> public int Row; /// <summary> /// 열 /// </summary> public int Column; /// <summary> /// 비트맵 /// </summary> public Bitmap Bitmap; /// <summary> /// 파일 경로 /// </summary> public string FilePath; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Hexagon(row, column, bitmap, filePath) /// <summary> /// 생성자 /// </summary> /// <param name="row">행</param> /// <param name="column">열</param> /// <param name="bitmap">비트맵</param> /// <param name="filePath">파일 경로</param> public Hexagon(int row, int column, Bitmap bitmap, string filePath) { Row = row; Column = column; Bitmap = bitmap; FileInfo fileInfo = new FileInfo(filePath); FilePath = fileInfo.Name; } #endregion } } |
▶ 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
using System; using System.Collections.Generic; 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 float hexgonHeight = 200; /// <summary> /// 테두리 두께 /// </summary> private float borderThickness = 5; /// <summary> /// 육각형 리스트 /// </summary> private List<Hexagon> hexagonList = new List<Hexagon>(); /// <summary> /// 행 수 /// </summary> private int rowCount = 1; /// <summary> /// 열 수 /// </summary> private int columnCount = 1; /// <summary> /// 비트맵 /// </summary> private Bitmap bitmap = null; #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.hexagonHeightTextBox.TextChanged += hexagonHeightTextBox_TextChanged; this.borderThicknessTextBox.TextChanged += hexagonHeightTextBox_TextChanged; this.borderColorPictureBox.Click += borderColorPictureBox_Click; this.backgroundColorPictureBox.Click += backgroundColorPictureBox_Click; this.createButton.Click += createButton_Click; this.pictureBox.MouseMove += pictureBox_MouseMove; this.pictureBox.MouseClick += pictureBox_MouseClick; } #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.openFileDialog1.ShowDialog() == DialogResult.OK) { FileInfo fileInfo1 = new FileInfo(this.openFileDialog1.FileName); DirectoryInfo directoryInfo = fileInfo1.Directory; List<FileInfo> fileInfoList = new List<FileInfo>(); foreach(FileInfo fileInfo2 in directoryInfo.GetFiles()) { string fileExtension = fileInfo2.Extension.ToLower().Replace(".", ""); if ( (fileExtension == "bmp" ) || (fileExtension == "png" ) || (fileExtension == "jpg" ) || (fileExtension == "jpeg") || (fileExtension == "gif" ) || (fileExtension == "tiff") ) { fileInfoList.Add(fileInfo2); } } int rowCount = (int)Math.Sqrt(fileInfoList.Count); int columnCount = rowCount; if(rowCount * columnCount < fileInfoList.Count) { columnCount++; } if(rowCount * columnCount < fileInfoList.Count) { rowCount++; } this.hexagonList = new List<Hexagon>(); int index = 0; for(int row = 0; row < rowCount; row++) { for(int column = 0; column < columnCount; column++) { string name = fileInfoList[index].Name; string filePath = fileInfoList[index].FullName; Bitmap bitmap = LoadBitmap(filePath); this.hexagonList.Add(new Hexagon(row, column, bitmap, name)); index++; if(index >= fileInfoList.Count) { break; } } if(index >= fileInfoList.Count) { break; } } MakeGrid(); } } #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.bitmap, 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 육각형 높이 텍스트 박스 텍스트 변경시 처리하기 - hexagonHeightTextBox_TextChanged(sender, e) /// <summary> /// 육각형 높이 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void hexagonHeightTextBox_TextChanged(object sender, EventArgs e) { float height; float thickness; if(!float.TryParse(this.hexagonHeightTextBox.Text, out height) || height < 10) { return; } if(!float.TryParse(this.borderThicknessTextBox.Text, out thickness)) { return; } this.hexgonHeight = height; this.borderThickness = thickness; this.pictureBox.Refresh(); } #endregion #region 테두리 색상 픽처 박스 클릭시 처리하기 - borderColorPictureBox_Click(sender, e) /// <summary> /// 테두리 색상 픽처 박스 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void borderColorPictureBox_Click(object sender, EventArgs e) { this.colorDialog.Color = this.borderColorPictureBox.BackColor; if(this.colorDialog.ShowDialog() == DialogResult.OK) { this.borderColorPictureBox.BackColor = this.colorDialog.Color; this.pictureBox.Refresh(); } } #endregion #region 배경 색상 픽처 박스 클릭시 처리하기 - backgroundColorPictureBox_Click(sender, e) /// <summary> /// 배경 색상 픽처 박스 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void backgroundColorPictureBox_Click(object sender, EventArgs e) { this.colorDialog.Color = this.backgroundColorPictureBox.BackColor; if(this.colorDialog.ShowDialog() == DialogResult.OK) { this.backgroundColorPictureBox.BackColor = this.colorDialog.Color; this.pictureBox.Refresh(); } } #endregion #region 생성 버튼 클릭시 처리하기 - createButton_Click(sender, e) /// <summary> /// 생성 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void createButton_Click(object sender, EventArgs e) { MakeGrid(); this.pictureBox.Visible = 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) { int row; int column; HexagonHelper.SetHexgonIndex(e.X, e.Y, this.hexgonHeight, out row, out column); int index = FindHexagon(row, column); string indexString = "(" + row.ToString() + ", " + column.ToString() + ")"; if(index < 0) { Text = $"Bitmap 클래스 : 스크롤 가능한 육각형 몽타주 비트맵 사용하기 : {indexString}"; } else { string name = this.hexagonList[index].FilePath; int position = name.IndexOf('.'); Text = $"{indexString} {name.Substring(0, position)}"; } } #endregion #region 픽처 박스 마우스 클릭시 처리하기 - pictureBox_MouseClick(sender, e) /// <summary> /// 픽처 박스 마우스 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseClick(object sender, MouseEventArgs e) { int row; int column; HexagonHelper.SetHexgonIndex(e.X, e.Y, this.hexgonHeight, out row, out column); RemoveHexagon(row, column); if(this.openFileDialog2.ShowDialog() == DialogResult.OK) { Bitmap bitmap = LoadBitmap(openFileDialog2.FileName); this.hexagonList.Add(new Hexagon(row, column, bitmap, openFileDialog2.FileName)); } MakeGrid(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 비트맵 로드하기 - LoadBitmap(filePath) /// <summary> /// 비트맵 로드하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>비트맵</returns> private Bitmap LoadBitmap(string filePath) { using(Bitmap bitmap = new Bitmap(filePath)) { return new Bitmap(bitmap); } } #endregion #region 그리드 테두리 사각형 찾기 - FindGridBoundRectangle(maximumX, maximumY) /// <summary> /// 그리드 테두리 사각형 찾기 /// </summary> /// <param name="maximumX">최대 X</param> /// <param name="maximumY">최대 Y</param> private void FindGridBoundRectangle(out float maximumX, out float maximumY) { maximumX = 0; maximumY = 0; float minimumX = this.borderThickness / 2f; PointF[] pointArray; pointArray = HexagonHelper.GetPointArray ( this.hexgonHeight, this.rowCount - 1, this.columnCount - 1, minimumX, minimumX ); if(maximumX < pointArray[3].X) { maximumX = pointArray[3].X; } if(maximumY < pointArray[4].Y) { maximumY = pointArray[4].Y; } pointArray = HexagonHelper.GetPointArray ( this.hexgonHeight, this.rowCount - 1, this.columnCount - 1, minimumX, minimumX ); if(maximumX < pointArray[3].X) { maximumX = pointArray[3].X; } if(maximumY < pointArray[4].Y) { maximumY = pointArray[4].Y; } maximumX += minimumX; maximumY += minimumX; } #endregion #region 그리드 그리기 - DrawGrid(graphics, maximumX, maximumY) /// <summary> /// 그리드 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="maximumX">최대 X</param> /// <param name="maximumY">최대 Y</param> private void DrawGrid(Graphics graphics, int maximumX, int maximumY) { graphics.Clear(this.backgroundColorPictureBox.BackColor); graphics.SmoothingMode = SmoothingMode.AntiAlias; float minimumX = this.borderThickness / 2f; foreach(Hexagon hexagon in this.hexagonList) { PointF[] pointArray = HexagonHelper.GetPointArray ( this.hexgonHeight, hexagon.Row, hexagon.Column, minimumX, minimumX ); if(pointArray[3].X > maximumX) { continue; } if(pointArray[4].Y > maximumY) { continue; } HexagonHelper.DrawImageInPolygon ( graphics, HexagonHelper.GetPointArray ( this.hexgonHeight, hexagon.Row, hexagon.Column, minimumX, minimumX ), hexagon.Bitmap ); } using(Pen pen = new Pen(this.borderColorPictureBox.BackColor, this.borderThickness)) { HexagonHelper.DrawHexgonGrid ( graphics, pen, minimumX, maximumX, minimumX, maximumY, this.hexgonHeight ); } } #endregion #region 그리드 만들기 - MakeGrid() /// <summary> /// 그리드 만들기 /// </summary> private void MakeGrid() { if(!int.TryParse(this.rowCountTextBox.Text, out this.rowCount)) { return; } if(!int.TryParse(this.columnCountTextBox.Text, out this.columnCount)) { return; } float maximumX; float maximumY; FindGridBoundRectangle(out maximumX, out maximumY); int width = (int)maximumX + 1; int height = (int)maximumY + 1; this.bitmap = new Bitmap(width, height); using(Graphics graphics = Graphics.FromImage(this.bitmap)) { DrawGrid(graphics, width, height); } this.pictureBox.Image = this.bitmap; this.saveAsMenuItem.Enabled = true; } #endregion #region 이미지 저장하기 - SaveImage(image, filePath) /// <summary> /// 이미지 저장하기 /// </summary> /// <param name="image">이미지</param> /// <param name="filePath">파일 경로</param> private void SaveImage(Image image, string filePath) { string fileExtension = Path.GetExtension(filePath); switch(fileExtension.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($"알 수 없는 파일 확장자 : {fileExtension}"); } } #endregion #region 육각형 찾기 - FindHexagon(row, column) /// <summary> /// 육각형 찾기 /// </summary> /// <param name="row">행</param> /// <param name="column">열</param> /// <returns>인덱스</returns> private int FindHexagon(int row, int column) { for (int i = this.hexagonList.Count - 1; i >= 0; i--) { if((this.hexagonList[i].Row == row) && (this.hexagonList[i].Column == column)) { return i; } } return -1; } #endregion #region 육각형 제거하기 - RemoveHexagon(row, column) /// <summary> /// 육각형 제거하기 /// </summary> /// <param name="row">행</param> /// <param name="column">열</param> private void RemoveHexagon(int row, int column) { int index = FindHexagon(row,column); if(index >= 0) { this.hexagonList.RemoveAt(index); } } #endregion } } |