■ UserControl 클래스를 사용해 대용량 이미지 갤러리를 만드는 방법을 보여준다.
▶ GalleryItem.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 |
using System.Drawing; namespace TestProject { /// <summary> /// 갤러리 항목 /// </summary> public class GalleryItem { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 이미지 /// </summary> public Image Image; #endregion } } |
▶ GalleryHelper.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 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; namespace TestProject { /// <summary> /// 갤러리 헬퍼 /// </summary> public static class GalleryHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 썸네일 비트맵 구하기 - GetThumbnailBitmap(sourceBitmap, targetWidth, targetHeight) /// <summary> /// 썸네일 비트맵 구하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="targetWidth">타겟 너비</param> /// <param name="targetHeight">타겟 높이</param> /// <returns>썸네일 비트맵</returns> public static Bitmap GetThumbnailBitmap(Bitmap sourceBitmap, int targetWidth, int targetHeight) { Bitmap bitmap = new Bitmap(targetWidth, targetHeight); using(Graphics graphics = Graphics.FromImage(bitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.Clear(Color.Transparent); int width; int height; if(sourceBitmap.Width < targetWidth && sourceBitmap.Height < targetHeight) { width = sourceBitmap.Width; height = sourceBitmap.Height; } else { if(sourceBitmap.Width > sourceBitmap.Height) { width = targetWidth; height = Convert.ToInt32(targetHeight * Convert.ToDouble(sourceBitmap.Height) / sourceBitmap.Width); } else { width = Convert.ToInt32(targetWidth * Convert.ToDouble(sourceBitmap.Width) / sourceBitmap.Height); height = targetHeight; } } int left = (targetWidth - width ) / 2; int top = (targetHeight - height) / 2; graphics.DrawImage(sourceBitmap, left, top, width, height); } return bitmap; } #endregion #region 썸네일 비트맵 구하기 - GetThumbnailBitmap(filePath) /// <summary> /// 썸네일 비트맵 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>썸네일 비트맵</returns> public static Bitmap GetThumbnailBitmap(string filePath) { using(Bitmap sourceBitmap = new Bitmap(filePath)) { Bitmap thumbnailBitmap = GetThumbnailBitmap(sourceBitmap, 200, 200); return thumbnailBitmap; } } #endregion #region 행 인덱스 구하기 - GetRowIndex(columnCount, sequenceIndex) /// <summary> /// 행 인덱스 구하기 /// </summary> /// <param name="columnCount">컬럼 수</param> /// <param name="sequenceIndex">순번 인덱스</param> /// <returns>행 인덱스</returns> public static int GetRowIndex(int columnCount, int sequenceIndex) { return sequenceIndex / columnCount; } #endregion #region 컬럼 인덱스 구하기 - GetColumnIndex(columnCount, sequenceIndex) /// <summary> /// 컬럼 인덱스 구하기 /// </summary> /// <param name="columnCount">컬럼 수</param> /// <param name="sequenceIndex">순번 인덱스</param> /// <returns>컬럼 인덱스</returns> public static int GetColumnIndex(int columnCount, int sequenceIndex) { return sequenceIndex % columnCount; } #endregion #region 순번 인덱스 구하기 - GetSequenceIndex(columnCount, rowIndex, columnIndex) /// <summary> /// 순번 인덱스 구하기 /// </summary> /// <param name="columnCount">컬럼 수</param> /// <param name="rowIndex">행 인덱스</param> /// <param name="columnIndex">컬럼 인덱스</param> /// <returns>순번 인덱스</returns> public static int GetSequenceIndex(int columnCount, int rowIndex, int columnIndex) { return columnCount * rowIndex + columnIndex; } #endregion #region 항목 그리기 - DrawItem(graphics, x, y, width, height, item) /// <summary> /// 항목 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <param name="item">갤러리 항목</param> public static void DrawItem(Graphics graphics, int x, int y, int width, int height, GalleryItem item) { graphics.DrawRectangle(Pens.Black, x + 1, y + 1, width - 2, height - 2); if(item.Image != null) { graphics.DrawImage(item.Image, new Rectangle(x + 2, y + 2, width - 4, height - 4)); } } #endregion } } |
▶ GalleryControl.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 |
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 갤러리 컨트롤 /// </summary> public partial class GalleryControl : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 뷰포트 너비 /// </summary> private int viewportWidth; /// <summary> /// 뷰포트 높이 /// </summary> private int viewportHeight; /// <summary> /// 항목 너비 /// </summary> private int itemWidth = 200; /// <summary> /// 항목 높이 /// </summary> private int itemHeight = 200; /// <summary> /// 항목 리스트 /// </summary> private List<GalleryItem> itemList = null; /// <summary> /// 항목 수 /// </summary> private int itemCount; /// <summary> /// 뷰포트 항목 X 수 /// </summary> private int viewportItemXCount; /// <summary> /// 뷰포트 항목 Y 수 /// </summary> private int viewportItemYCount; /// <summary> /// 뷰포트 항목 수 /// </summary> private int viewportItemCount; /// <summary> /// 스크롤바 너비 /// </summary> private int scrollBarWidth = 18; /// <summary> /// 컨텐트 너비 /// </summary> private int contentWidth; /// <summary> /// 컨텐트 높이 /// </summary> private int contentHeight; /// <summary> /// 스크롤 Y /// </summary> private int scrollY = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 뷰포트 너비 - ViewportWidth /// <summary> /// 뷰포트 너비 /// </summary> public int ViewportWidth { get { return this.viewportWidth; } } #endregion #region 뷰포트 높이 - ViewportHeight /// <summary> /// 뷰포트 높이 /// </summary> public int ViewportHeight { get { return this.viewportHeight; } } #endregion #region 항목 너비 - ItemWidth /// <summary> /// 항목 너비 /// </summary> public int ItemWidth { get { return this.itemWidth; } set { if(this.itemWidth == value) { return; } this.itemWidth = value; Calculate(); } } #endregion #region 항목 높이 - ItemHeight /// <summary> /// 항목 높이 /// </summary> public int ItemHeight { get { return this.itemHeight; } set { if(this.itemHeight == value) { return; } this.itemHeight = value; Calculate(); } } #endregion #region 항목 리스트 - ItemList /// <summary> /// 항목 리스트 /// </summary> public List<GalleryItem> ItemList { get { return this.itemList; } set { if(this.itemList == value) { return; } this.itemList = value; Calculate(); } } #endregion #region 항목 수 - ItemCount /// <summary> /// 항목 수 /// </summary> public int ItemCount { get { return this.itemCount; } } #endregion #region 뷰포트 항목 X 수 - ViewportItemXCount /// <summary> /// 뷰포트 항목 X 수 /// </summary> public int ViewportItemXCount { get { return this.viewportItemXCount; } } #endregion #region 뷰포트 항목 Y 수 - ViewportItemYCount /// <summary> /// 뷰포트 항목 Y 수 /// </summary> public int ViewportItemYCount { get { return this.viewportItemYCount; } } #endregion #region 뷰포트 항목 수 - ViewportItemCount /// <summary> /// 뷰포트 항목 수 /// </summary> public int ViewportItemCount { get { return this.viewportItemCount; } } #endregion #region 스크롤바 너비 - ScrollBarWidth /// <summary> /// 스크롤바 너비 /// </summary> public int ScrollBarWidth { get { return this.scrollBarWidth; } set { if(this.scrollBarWidth == value) { return; } this.scrollBarWidth = value; this.vScrollBar.Width = this.scrollBarWidth - 1; Calculate(); } } #endregion #region 컨텐트 너비 - ContentWidth /// <summary> /// 컨텐트 너비 /// </summary> public int ContentWidth { get { return this.contentWidth; } } #endregion #region 컨텐트 높이 - ContentHeight /// <summary> /// 컨텐트 높이 /// </summary> public int ContentHeight { get { return this.contentHeight; } } #endregion #region 스크롤 Y - ScrollY /// <summary> /// 스크롤 Y /// </summary> public int ScrollY { get { return this.scrollY; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - GalleryControl() /// <summary> /// 생성자 /// </summary> public GalleryControl() { InitializeComponent(); SetStyle(ControlStyles.DoubleBuffer , true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint , true); this.vScrollBar.Width = this.scrollBarWidth - 1; this.vScrollBar.Visible = false; SizeChanged += UserControl_SizeChanged; Paint += UserControl_Paint; MouseWheel += UserControl_MouseWheel; this.vScrollBar.ValueChanged += vScrollBar_ValueChanged; this.vScrollBar.MouseWheel += vScrollBar_MouseWheel; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 사용자 컨트롤 크기 변경시 처리하기 - UserControl_SizeChanged(sender, e) /// <summary> /// 사용자 컨트롤 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_SizeChanged(object sender, EventArgs e) { Calculate(); } #endregion #region 사용자 컨트롤 페인트시 처리하기 - UserControl_Paint(sender, e) /// <summary> /// 사용자 컨트롤 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; graphics.Clear(Color.White); if(this.itemCount > 0 && this.viewportItemCount > 0) { int viewportStartY = this.scrollY; int viewportEndY = this.scrollY + this.viewportHeight; int itemStartRowIndex = viewportStartY / this.itemHeight; int itemEndRowIndex = viewportEndY / this.itemHeight; for(int y = itemStartRowIndex; y <= itemEndRowIndex; y++) { for(int x = 0; x < this.viewportItemXCount; x++) { int sequenceIndex = GalleryHelper.GetSequenceIndex(this.viewportItemXCount, y, x); if(sequenceIndex > this.itemList.Count - 1) { continue; } GalleryItem item = this.itemList[sequenceIndex]; int itemX = x * this.itemWidth; int itemY = y * this.itemHeight - this.scrollY; GalleryHelper.DrawItem(graphics, itemX, itemY, this.itemWidth, this.itemHeight, item); } } } } #endregion #region 사용자 컨트롤 마우스 휠 처리하기 - UserControl_MouseWheel(sender, e) /// <summary> /// 사용자 컨트롤 마우스 휠 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void UserControl_MouseWheel(object sender, MouseEventArgs e) { if(this.vScrollBar.Visible) { this.vScrollBar.Value = Math.Max(this.vScrollBar.Minimum, Math.Min(this.vScrollBar.Maximum, this.vScrollBar.Value - e.Delta)); } } #endregion #region 수직 스크롤바 값 변경시 처리하기 - vScrollBar_ValueChanged(sender, e) /// <summary> /// 수직 스크롤바 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void vScrollBar_ValueChanged(object sender, EventArgs e) { this.scrollY = this.vScrollBar.Value; Invalidate(); } #endregion #region 수직 스크롤바 마우스 휠 처리하기 - vScrollBar_MouseWheel(sender, e) /// <summary> /// 수직 스크롤바 마우스 휠 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void vScrollBar_MouseWheel(object sender, MouseEventArgs e) { this.vScrollBar.Value = Math.Max(this.vScrollBar.Minimum, Math.Min(this.vScrollBar.Maximum, this.vScrollBar.Value - e.Delta)); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 스크롤바 설정하기 - SetScrollBar() /// <summary> /// 스크롤바 설정하기 /// </summary> private void SetScrollBar() { this.vScrollBar.ValueChanged -= vScrollBar_ValueChanged; this.vScrollBar.MouseWheel -= vScrollBar_MouseWheel; if(this.viewportItemCount == 0) { this.vScrollBar.Visible = false; this.scrollY = 0; } else { if(this.itemCount == 0) { this.vScrollBar.Visible = false; this.scrollY = 0; } else { if(this.itemCount > this.viewportItemCount) { this.vScrollBar.Visible = true; this.vScrollBar.Minimum = 0; this.vScrollBar.Maximum = this.contentHeight - this.viewportHeight + 10; this.scrollY = Math.Max(this.vScrollBar.Minimum, Math.Min(this.scrollY, this.vScrollBar.Maximum)); this.vScrollBar.Value = this.scrollY; } else { this.vScrollBar.Visible = false; this.scrollY = 0; } } } this.vScrollBar.ValueChanged += vScrollBar_ValueChanged; this.vScrollBar.MouseWheel += vScrollBar_MouseWheel; } #endregion #region 계산하기 - Calculate() /// <summary> /// 계산하기 /// </summary> private void Calculate() { int temporaryViewportWidth = ClientSize.Width; int temporaryViewportHeight = ClientSize.Height; int temporaryViewportItemXCount = temporaryViewportWidth / this.itemWidth; int temporaryViewportItemYCount = temporaryViewportHeight / this.itemHeight; int temporaryViewportItemCount = temporaryViewportItemXCount * temporaryViewportItemYCount; if(temporaryViewportItemCount == 0) { this.viewportItemCount = 0; } else { this.itemCount = this.itemList == null ? 0 : this.itemList.Count; if(this.itemCount == 0) { this.viewportItemCount = 0; } else { if(this.itemCount > temporaryViewportItemCount) { temporaryViewportWidth = ClientSize.Width - this.scrollBarWidth; temporaryViewportHeight = ClientSize.Height; temporaryViewportItemXCount = temporaryViewportWidth / this.itemWidth; temporaryViewportItemYCount = temporaryViewportHeight / this.itemHeight; temporaryViewportItemCount = temporaryViewportItemXCount * temporaryViewportItemYCount; if(temporaryViewportItemCount == 0) { this.viewportItemCount = 0; } else { this.viewportWidth = temporaryViewportWidth; this.viewportHeight = temporaryViewportHeight; this.viewportItemXCount = temporaryViewportItemXCount; this.viewportItemYCount = temporaryViewportItemYCount; this.viewportItemCount = temporaryViewportItemCount; this.contentWidth = this.viewportItemXCount * this.itemWidth; this.contentHeight = (GalleryHelper.GetRowIndex(this.viewportItemXCount, this.itemCount - 1) + 1) * this.itemHeight; } } else { this.viewportWidth = temporaryViewportWidth; this.viewportHeight = temporaryViewportHeight; this.viewportItemXCount = temporaryViewportItemXCount; this.viewportItemYCount = temporaryViewportItemYCount; this.viewportItemCount = temporaryViewportItemCount; this.contentWidth = this.viewportItemXCount * this.itemWidth; this.contentHeight = (GalleryHelper.GetRowIndex(this.viewportItemXCount, this.itemCount - 1) + 1) * this.itemHeight; } } } SetScrollBar(); Invalidate(); } #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 |
using System.Collections.Generic; using System.Drawing; using System.IO; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); string sourceImageDirectoryPath = "IMAGE"; string[] filePathArray = Directory.GetFiles(sourceImageDirectoryPath, "*.*", SearchOption.TopDirectoryOnly); List<GalleryItem> itemList = new List<GalleryItem>(); for(int i = 0; i < 30; i++) { GalleryItem item = new GalleryItem(); item.Image = new Bitmap(filePathArray[i]); itemList.Add(item); } this.galleryControl.ItemList = itemList; } #endregion } } |