■ Bitmap 클래스를 사용해 부분 색상을 반전하는 방법을 보여준다.
▶ ColorInversionType.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 |
namespace TestProject { /// <summary> /// 색상 반전 타입 /// </summary> public enum ColorInversionType { /// <summary> /// 모두 /// </summary> All, /// <summary> /// 청색 /// </summary> Blue, /// <summary> /// 녹색 /// </summary> Green, /// <summary> /// 적색 /// </summary> Red, /// <summary> /// 청색/적색 /// </summary> BlueRed, /// <summary> /// 청색/녹색 /// </summary> BlueGreen, /// <summary> /// 적색/녹색 /// </summary> RedGreen } } |
▶ ColorSwapType.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 |
namespace TestProject { /// <summary> /// 색상 스왑 타입 /// </summary> public enum ColorSwapType { /// <summary> /// 오른쪽 시프트 /// </summary> ShiftRight, /// <summary> /// 왼쪽 시프트 /// </summary> ShiftLeft, /// <summary> /// 청색/적색 스왑 /// </summary> SwapBlueRed, /// <summary> /// 청색/적색 스왑, 녹색 고정 /// </summary> SwapBlueRedFixGreen, /// <summary> /// 청색/녹색 스왑 /// </summary> SwapBlueGreen, /// <summary> /// 청색/녹색 스왑, 적색 고정 /// </summary> SwapBlueGreenFixRed, /// <summary> /// 적색/녹색 스왑 /// </summary> SwapRedGreen, /// <summary> /// 적색/녹색 스왑, 청색 고정 /// </summary> SwapRedGreenFixBlue } } |
▶ BitmapHelper.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 |
using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Class ////////////////////////////////////////////////////////////////////////////////////////// Public #region ARGB 픽셀 - ARGBPixel /// <summary> /// ARGB 픽셀 /// </summary> public class ARGBPixel { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 픽셀 X /// </summary> public int PixelX = 0; /// <summary> /// 픽셀 Y /// </summary> public int PixelY = 0; /// <summary> /// 청색 /// </summary> public byte Blue = 0; /// <summary> /// 녹색 /// </summary> public byte Green = 0; /// <summary> /// 적색 /// </summary> public byte Red = 0; /// <summary> /// 알파 /// </summary> public byte Alpha = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 인덱서 - this[index] /// <summary> /// 인덱서 /// </summary> /// <param name="index">인덱스</param> /// <returns>색상 컴포넌트</returns> public byte this[int index] { get { switch(index) { case 0 : return Blue; case 1 : return Green; case 2 : return Red; case 3 : return Alpha; default : return 0; } } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ARGBPixel() /// <summary> /// 생성자 /// </summary> public ARGBPixel() { } #endregion #region 생성자 - ARGBPixel(colorComponentArray) /// <summary> /// 생성자 /// </summary> /// <param name="colorComponentArray">색상 컴포넌트 배열</param> public ARGBPixel(byte[] colorComponentArray) { Blue = colorComponentArray[0]; Green = colorComponentArray[1]; Red = colorComponentArray[2]; Alpha = colorComponentArray[3]; } #endregion #region 생성자 - ARGBPixel(colorComponentArray, x, y) /// <summary> /// 생성자 /// </summary> /// <param name="colorComponentArray">색상 컴포넌트 배열</param> /// <param name="x">X</param> /// <param name="y">Y</param> public ARGBPixel(byte[] colorComponentArray, int x, int y) { Blue = colorComponentArray[0]; Green = colorComponentArray[1]; Red = colorComponentArray[2]; Alpha = colorComponentArray[3]; PixelX = x; PixelY = y; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 색상 바이트 배열 구하기 - GetColorByteArray() /// <summary> /// 색상 바이트 배열 구하기 /// </summary> /// <returns>색상 바이트 배열</returns> public byte[] GetColorByteArray() { return new byte[] { Blue, Green, Red, Alpha }; } #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Metohd ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 구하기 - GetBitmap(sourceBitmap, targetWidth) /// <summary> /// 비트맵 구하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="targetWidth">타겟 너비</param> /// <returns>비트맵</returns> public static Bitmap GetBitmap(Bitmap sourceBitmap, int targetWidth) { int maximumSide = sourceBitmap.Width > sourceBitmap.Height ? sourceBitmap.Width : sourceBitmap.Height; float ratio = (float)maximumSide / (float)targetWidth; Bitmap targetBitmap = (sourceBitmap.Width > sourceBitmap.Height ? new Bitmap(targetWidth, (int)(sourceBitmap.Height / ratio)) : new Bitmap((int)(sourceBitmap.Width / ratio), targetWidth)); 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 #region 픽셀 플립하기 - FlipPixels(sourceBitmap) /// <summary> /// 픽셀 플립하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <returns>비트맵</returns> public static Bitmap FlipPixels(Bitmap sourceBitmap) { List<ARGBPixel> argbPixelList = GetARGBPixelList(sourceBitmap); argbPixelList.Reverse(); Bitmap targetBitmap = GetBitmap(argbPixelList, sourceBitmap.Width, sourceBitmap.Height); return targetBitmap; } #endregion #region 색상 반전하기 - InvertColors(sourceBitmap, colorInversionType) /// <summary> /// 색상 반전하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="colorInversionType">색상 반전 타입</param> /// <returns>비트맵</returns> public static Bitmap InvertColors(Bitmap sourceBitmap, ColorInversionType colorInversionType) { List<ARGBPixel> sourceARGBPixelList = GetARGBPixelList(sourceBitmap); List<ARGBPixel> targetARGBPixelList = null; byte maximumByte = 255; switch(colorInversionType) { case ColorInversionType.All : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = (byte)(maximumByte - pixel.Blue ), Red = (byte)(maximumByte - pixel.Red ), Green = (byte)(maximumByte - pixel.Green), Alpha = pixel.Alpha }).ToList(); break; } case ColorInversionType.Blue : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = (byte)(maximumByte - pixel.Blue), Red = pixel.Red, Green = pixel.Green, Alpha = pixel.Alpha }).ToList(); break; } case ColorInversionType.Green : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Blue, Red = pixel.Red, Green = (byte)(maximumByte - pixel.Green), Alpha = pixel.Alpha, }).ToList(); break; } case ColorInversionType.Red : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Blue, Red = (byte)(maximumByte - pixel.Green), Green = pixel.Green, Alpha = pixel.Alpha, }).ToList(); break; } case ColorInversionType.BlueRed : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = (byte)(maximumByte - pixel.Blue), Red = (byte)(maximumByte - pixel.Red ), Green = pixel.Green, Alpha = pixel.Alpha }).ToList(); break; } case ColorInversionType.BlueGreen : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = (byte)(maximumByte - pixel.Blue), Red = pixel.Red, Green = (byte)(maximumByte - pixel.Green), Alpha = pixel.Alpha, }).ToList(); break; } case ColorInversionType.RedGreen : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Blue, Red = (byte)(maximumByte - pixel.Blue ), Green = (byte)(maximumByte - pixel.Green), Alpha = pixel.Alpha }).ToList(); break; } } Bitmap targetBitmap = GetBitmap(targetARGBPixelList, sourceBitmap.Width, sourceBitmap.Height); return targetBitmap; } #endregion #region 색상 스왑하기 - SwapColors(sourceBitmap, colorSwapType, fixedValue) /// <summary> /// 색상 스왑하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="colorSwapType">색상 스왑 타입</param> /// <param name="fixedValue">고정 값</param> /// <returns>비트맵</returns> public static Bitmap SwapColors(Bitmap sourceBitmap, ColorSwapType colorSwapType, byte fixedValue = 0) { List<ARGBPixel> sourceARGBPixelList = GetARGBPixelList(sourceBitmap); List<ARGBPixel> targetARGBPixelList = null; switch(colorSwapType) { case ColorSwapType.ShiftRight : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Red, Red = pixel.Green, Green = pixel.Blue, Alpha = pixel.Alpha }).ToList(); break; } case ColorSwapType.ShiftLeft : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Green, Red = pixel.Blue, Green = pixel.Red, Alpha = pixel.Alpha }).ToList(); break; } case ColorSwapType.SwapBlueRed : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Red, Red = pixel.Blue, Green = pixel.Green, Alpha = pixel.Alpha }).ToList(); break; } case ColorSwapType.SwapBlueRedFixGreen : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Red, Red = pixel.Blue, Green = fixedValue, Alpha = pixel.Alpha }).ToList(); break; } case ColorSwapType.SwapBlueGreen : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Green, Red = pixel.Red, Green = pixel.Blue, Alpha = pixel.Alpha }).ToList(); break; } case ColorSwapType.SwapBlueGreenFixRed : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Green, Red = fixedValue, Green = pixel.Blue, Alpha = pixel.Alpha }).ToList(); break; } case ColorSwapType.SwapRedGreen : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = pixel.Blue, Red = pixel.Green, Green = pixel.Red, Alpha = pixel.Alpha }).ToList(); break; } case ColorSwapType.SwapRedGreenFixBlue : { targetARGBPixelList = (from pixel in sourceARGBPixelList select new ARGBPixel { Blue = fixedValue, Red = pixel.Green, Green = pixel.Red, Alpha = pixel.Alpha }).ToList(); break; } } Bitmap targetBitmap = GetBitmap(targetARGBPixelList, sourceBitmap.Width, sourceBitmap.Height); return targetBitmap; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region ARGB 픽셀 리스트 구하기 - GetARGBPixelList(sourceBitmap) /// <summary> /// ARGB 픽셀 리스트 구하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <returns>ARGB 픽셀 리스트</returns> private static List<ARGBPixel> GetARGBPixelList(Bitmap sourceBitmap) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] sourceByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; Marshal.Copy(sourceBitmapData.Scan0, sourceByteArray, 0, sourceByteArray.Length); sourceBitmap.UnlockBits(sourceBitmapData); List<ARGBPixel> argbPixelList = new List<ARGBPixel>(sourceByteArray.Length / 4); int x = 0; int y = 0; using(MemoryStream memoryStream = new MemoryStream(sourceByteArray)) { memoryStream.Position = 0; BinaryReader binaryReader = new BinaryReader(memoryStream); while(memoryStream.Position + 4 <= memoryStream.Length) { ARGBPixel argbPixel = new ARGBPixel(binaryReader.ReadBytes(4), x, y); argbPixelList.Add(argbPixel); x += 1; if(x >= sourceBitmapData.Width) { x = 0; y += 1; } } binaryReader.Close(); } return argbPixelList; } #endregion #region 비트맵 구하기 - GetBitmap(argbPixelList, width, height) /// <summary> /// 비트맵 구하기 /// </summary> /// <param name="argbPixelList">ARGB 픽셀 리스트</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <returns>비트맵</returns> private static Bitmap GetBitmap(List<ARGBPixel> argbPixelList, int width, int height) { Bitmap targetBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] targetByteArray = new byte[targetBitmapData.Stride * targetBitmapData.Height]; using(MemoryStream memoryStream = new MemoryStream(targetByteArray)) { memoryStream.Position = 0; BinaryWriter binaryWriter = new BinaryWriter(memoryStream); foreach(ARGBPixel argbPixel in argbPixelList) { binaryWriter.Write(argbPixel.GetColorByteArray()); } binaryWriter.Close(); } Marshal.Copy(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); return targetBitmap; } #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 |
using System; using System.Drawing; 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; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.invertColorTypeComboBox.Items.Add(ColorInversionType.All ); this.invertColorTypeComboBox.Items.Add(ColorInversionType.Blue ); this.invertColorTypeComboBox.Items.Add(ColorInversionType.BlueGreen); this.invertColorTypeComboBox.Items.Add(ColorInversionType.BlueRed ); this.invertColorTypeComboBox.Items.Add(ColorInversionType.Green ); this.invertColorTypeComboBox.Items.Add(ColorInversionType.Red ); this.invertColorTypeComboBox.Items.Add(ColorInversionType.RedGreen ); this.invertColorTypeComboBox.SelectedIndex = 0; this.colorSwapTypeComboBox.Items.Add(ColorSwapType.ShiftLeft ); this.colorSwapTypeComboBox.Items.Add(ColorSwapType.ShiftRight ); this.colorSwapTypeComboBox.Items.Add(ColorSwapType.SwapBlueGreen ); this.colorSwapTypeComboBox.Items.Add(ColorSwapType.SwapBlueGreenFixRed); this.colorSwapTypeComboBox.Items.Add(ColorSwapType.SwapBlueRed ); this.colorSwapTypeComboBox.Items.Add(ColorSwapType.SwapBlueRedFixGreen); this.colorSwapTypeComboBox.Items.Add(ColorSwapType.SwapRedGreen ); this.colorSwapTypeComboBox.Items.Add(ColorSwapType.SwapRedGreenFixBlue); this.colorSwapTypeComboBox.SelectedIndex = 0; this.flipPixelCheckBox.CheckedChanged += flipPixelCheckBox_CheckedChanged; this.invertColorCheckBox.CheckedChanged += invertColorCheckBox_CheckedChanged; this.invertColorTypeComboBox.SelectedIndexChanged += colorInversionTypeComboBox_SelectedIndexChanged; this.swapColorCheckBox.CheckedChanged += swapColorCheckBox_CheckedChanged; this.colorSwapTypeComboBox.SelectedIndexChanged += colorSwapTypeComboBox_SelectedIndexChanged; this.fixedValueTrackBar.ValueChanged += fixedValueTrackBar_ValueChanged; this.loadButton.Click += loadButton_Click; this.saveButton.Click += saveButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 픽셀 플립 체크 박스 체크 변경시 처리하기 - flipPixelCheckBox_CheckedChanged(sender, e) /// <summary> /// 픽셀 플립 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void flipPixelCheckBox_CheckedChanged(object sender, EventArgs e) { ApplyFilter(true); } #endregion #region 색상 반전 체크 박스 체크 변경시 처리하기 - invertColorCheckBox_CheckedChanged(sender, e) /// <summary> /// 색상 반전 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void invertColorCheckBox_CheckedChanged(object sender, EventArgs e) { ApplyFilter(true); } #endregion #region 색상 반전 타입 콤보 박스 선택 인덱스 변경시 처리하기 - colorInversionTypeComboBox_SelectedIndexChanged(sender, e) /// <summary> /// 색상 반전 타입 콤보 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void colorInversionTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { ApplyFilter(true); } #endregion #region 색상 스왑 체크 박스 체크 변경시 처리하기 - swapColorCheckBox_CheckedChanged(sender, e) /// <summary> /// 색상 스왑 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void swapColorCheckBox_CheckedChanged(object sender, EventArgs e) { ApplyFilter(true); } #endregion #region 색상 스왑 타입 콤보 박스 선택 인덱스 변경시 처리하기 - colorSwapTypeComboBox_SelectedIndexChanged(sender, e) /// <summary> /// 색상 스왑 타입 콤보 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void colorSwapTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { ApplyFilter(true); } #endregion #region 고정 값 트랙바 값 변경시 처리하기 - fixedValueTrackBar_ValueChanged(sender, e) /// <summary> /// 고정 값 트랙바 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fixedValueTrackBar_ValueChanged(object sender, EventArgs e) { ApplyFilter(true); } #endregion #region 이미지 로드 버튼 클릭시 처리하기 - loadButton_Click(sender, e) /// <summary> /// 이미지 로드 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void loadButton_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "이미지 파일 선택"; openFileDialog.Filter = "PNG 이미지(*.png)|*.png|JPEG 이미지(*.jpg)|*.jpg|비트맵 이미지(*.bmp)|*.bmp"; if(openFileDialog.ShowDialog() == DialogResult.OK) { StreamReader streamReader = new StreamReader(openFileDialog.FileName); this.sourceBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream); streamReader.Close(); this.sourcePictureBox.Image = BitmapHelper.GetBitmap(this.sourceBitmap, this.sourcePictureBox.Width); ApplyFilter(true); } } #endregion #region 이미지 저장 버튼 클릭시 처리하기 - saveButton_Click(sender, e) /// <summary> /// 이미지 저장 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveButton_Click(object sender, EventArgs e) { ApplyFilter(false); if(this.targetBitmap != null) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "이미지 파일 선택"; saveFileDialog.Filter = "PNG 이미지(*.png)|*.png|JPEG 이미지(*.jpg)|*.jpg|비트맵 이미지(*.bmp)|*.bmp"; if(saveFileDialog.ShowDialog() == DialogResult.OK) { string fileExtension = Path.GetExtension(saveFileDialog.FileName).ToUpper(); ImageFormat imageFormat = ImageFormat.Png; if(fileExtension == "BMP") { imageFormat = ImageFormat.Bmp; } else if(fileExtension == "JPG") { imageFormat = ImageFormat.Jpeg; } StreamWriter streamWriter = new StreamWriter(saveFileDialog.FileName, false); this.targetBitmap.Save(streamWriter.BaseStream, imageFormat); streamWriter.Flush(); streamWriter.Close(); this.targetBitmap = null; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 필터 적용하기 - ApplyFilter(preview) /// <summary> /// 필터 적용하기 /// </summary> /// <param name="preview">미리보기 여부</param> private void ApplyFilter(bool preview) { this.invertColorTypeComboBox.Enabled = this.invertColorCheckBox.Checked; this.colorSwapTypeComboBox.Enabled = this.swapColorCheckBox.Checked; this.fixedValueLabel.Text = "고정 값 : " + this.fixedValueTrackBar.Value.ToString(); Bitmap targetBitmap = null; if(this.sourcePictureBox.Image == null) { return; } if(preview == true) { targetBitmap = new Bitmap(this.sourcePictureBox.Image); } else { targetBitmap = new Bitmap(this.sourceBitmap); } if(targetBitmap == null) { return; } if(this.flipPixelCheckBox.Checked == true) { targetBitmap = BitmapHelper.FlipPixels(targetBitmap); } if(this.invertColorCheckBox.Checked == true) { ColorInversionType colourInversionType = (ColorInversionType)invertColorTypeComboBox.SelectedItem; targetBitmap = BitmapHelper.InvertColors(targetBitmap, colourInversionType); } if(this.swapColorCheckBox.Checked == true) { ColorSwapType colorSwapType = (ColorSwapType)this.colorSwapTypeComboBox.SelectedItem; targetBitmap = BitmapHelper.SwapColors(targetBitmap, colorSwapType, (byte)this.fixedValueTrackBar.Value); } if(targetBitmap != null) { if(preview == true) { this.targetPictureBox.Image = targetBitmap; } else { this.targetBitmap = targetBitmap; } } } #endregion } } |