■ Bitmap 클래스에서 이미지 추상 색상 필터를 사용하는 방법을 보여준다.
▶ ColorShiftType.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 |
namespace TestProject { /// <summary> /// 색상 시프트 타입 /// </summary> public enum ColorShiftType { /// <summary> /// 해당 무 /// </summary> None, /// <summary> /// 왼쪽 시프트 /// </summary> ShiftLeft, /// <summary> /// 오른쪽 시프트 /// </summary> ShiftRight } } |
▶ EdgeTracingType.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 |
namespace TestProject { /// <summary> /// 가장자리 추적 타입 /// </summary> public enum EdgeTracingType { /// <summary> /// 검정색 /// </summary> Black, /// <summary> /// 흰색 /// </summary> White, /// <summary> /// 1/2 강도 /// </summary> HalfIntensity, /// <summary> /// 2배 강도 /// </summary> DoubleIntensity, /// <summary> /// 색상 반전 /// </summary> ColorInversion } } |
▶ 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 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 복사하기 - Copy(sourceBitmap, squareSize) /// <summary> /// 복사하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="squareSize">정사각형 크기</param> /// <returns>비트맵</returns> public static Bitmap Copy(Bitmap sourceBitmap, int squareSize) { float ratio = 1.0f; int maximumSide = sourceBitmap.Width > sourceBitmap.Height ? sourceBitmap.Width : sourceBitmap.Height; ratio = (float)maximumSide / (float)squareSize; Bitmap targetBitmap; if(sourceBitmap.Width > sourceBitmap.Height) { targetBitmap = new Bitmap(squareSize, (int)(sourceBitmap.Height / ratio)); } else { targetBitmap = new Bitmap((int)(sourceBitmap.Width / ratio), squareSize); } 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 바이트 배열 구하기 - GetByteArray(sourceBitmap) /// <summary> /// 바이트 배열 구하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <returns>바이트 배열</returns> public static byte[] GetByteArray(Bitmap sourceBitmap) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] targetByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; Marshal.Copy(sourceBitmapData.Scan0, targetByteArray, 0, targetByteArray.Length); sourceBitmap.UnlockBits(sourceBitmapData); return targetByteArray; } #endregion #region 평균 색상 필터 적용하기 - ApplyAverageColorFilter(sourceBitmap, matrixSize, applyBlue, applyGreen, applyRed, colorShiftType) /// <summary> /// 평균 색상 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="matrixSize">매트릭스 크기</param> /// <param name="applyBlue">청색 채널 적용 여부</param> /// <param name="applyGreen">녹색 채널 적용 여부</param> /// <param name="applyRed">적색 채널 적용 여부</param> /// <param name="colorShiftType">색상 시프트 타입</param> /// <returns>비트맵</returns> public static Bitmap ApplyAverageColorFilter ( Bitmap sourceBitmap, int matrixSize, bool applyBlue = true, bool applyGreen = true, bool applyRed = true, ColorShiftType colorShiftType = ColorShiftType.None ) { byte[] sourceByteArray = GetByteArray(sourceBitmap); byte[] targetByteArray = new byte[sourceByteArray.Length]; int sourceOffset = 0; int targetOffset = 0; int blue = 0; int green = 0; int red = 0; int filterOffset = (matrixSize - 1) / 2; for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { targetOffset = offsetY * sourceBitmap.Width * 4 + offsetX * 4; blue = 0; green = 0; red = 0; for(int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for(int filterX = -filterOffset; filterX <= filterOffset; filterX++) { sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmap.Width * 4); blue += sourceByteArray[sourceOffset ]; green += sourceByteArray[sourceOffset + 1]; red += sourceByteArray[sourceOffset + 2]; } } blue = blue / matrixSize; green = green / matrixSize; red = red / matrixSize; if(applyBlue == false) { blue = sourceByteArray[targetOffset]; } if(applyGreen == false) { green = sourceByteArray[targetOffset + 1]; } if(applyRed == false) { red = sourceByteArray[targetOffset + 2]; } if(colorShiftType == ColorShiftType.None) { targetByteArray[targetOffset ] = (byte)blue; targetByteArray[targetOffset + 1] = (byte)green; targetByteArray[targetOffset + 2] = (byte)red; targetByteArray[targetOffset + 3] = 255; } else if(colorShiftType == ColorShiftType.ShiftLeft) { targetByteArray[targetOffset ] = (byte)green; targetByteArray[targetOffset + 1] = (byte)red; targetByteArray[targetOffset + 2] = (byte)blue; targetByteArray[targetOffset + 3] = 255; } else if(colorShiftType == ColorShiftType.ShiftRight) { targetByteArray[targetOffset ] = (byte)red; targetByteArray[targetOffset + 1] = (byte)blue; targetByteArray[targetOffset + 2] = (byte)green; targetByteArray[targetOffset + 3] = 255; } } } Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb ); Marshal.Copy(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); return targetBitmap; } #endregion #region 이미지 추상 색상 필터 적용하기 - ApplyImageAbstractColorFilter(sourceBitmap, matrixSize, edgeThreshold, applyBlue, applyGreen, applyRed, edgeTracingType, colorShiftType) /// <summary> /// 이미지 추상 색상 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="matrixSize">매트릭스 크기</param> /// <param name="edgeThreshold">가장자리 임계치</param> /// <param name="applyBlue">청색 채널 적용 여부</param> /// <param name="applyGreen">녹색 채널 적용 여부</param> /// <param name="applyRed">적색 채널 적용 여부</param> /// <param name="edgeTracingType">가장자리 추적 타입</param> /// <param name="colorShiftType">색상 시프트 타입</param> /// <returns>비트맵</returns> public static Bitmap ApplyImageAbstractColorFilter ( Bitmap sourceBitmap, int matrixSize, byte edgeThreshold, bool applyBlue = true, bool applyGreen = true, bool applyRed = true, EdgeTracingType edgeTracingType = EdgeTracingType.Black, ColorShiftType colorShiftType = ColorShiftType.None ) { Bitmap edgeBitmap = ApplyGradientBasedEdgeDetectionFilter(sourceBitmap, edgeThreshold); Bitmap colorBitmap = ApplyAverageColorFilter ( sourceBitmap, matrixSize, applyBlue, applyGreen, applyRed, colorShiftType ); byte[] edgeByteArray = GetByteArray(edgeBitmap); byte[] colorByteArray = GetByteArray(colorBitmap); byte[] targetByteArray = GetByteArray(colorBitmap); for(int k = 0; k + 4 < edgeByteArray.Length; k += 4) { if(edgeByteArray[k] == 255) { switch(edgeTracingType) { case EdgeTracingType.Black: targetByteArray[k ] = 0; targetByteArray[k + 1] = 0; targetByteArray[k + 2] = 0; break; case EdgeTracingType.White : targetByteArray[k ] = 255; targetByteArray[k + 1] = 255; targetByteArray[k + 2] = 255; break; case EdgeTracingType.HalfIntensity : targetByteArray[k ] = ClipByte(targetByteArray[k ] * 0.5); targetByteArray[k + 1] = ClipByte(targetByteArray[k + 1] * 0.5); targetByteArray[k + 2] = ClipByte(targetByteArray[k + 2] * 0.5); break; case EdgeTracingType.DoubleIntensity : targetByteArray[k ] = ClipByte(targetByteArray[k ] * 2); targetByteArray[k + 1] = ClipByte(targetByteArray[k + 1] * 2); targetByteArray[k + 2] = ClipByte(targetByteArray[k + 2] * 2); break; case EdgeTracingType.ColorInversion : targetByteArray[k ] = ClipByte(255 - targetByteArray[k ]); targetByteArray[k + 1] = ClipByte(255 - targetByteArray[k + 1]); targetByteArray[k + 2] = ClipByte(255 - targetByteArray[k + 2]); break; } } } Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0,targetBitmap.Width, targetBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb ); Marshal.Copy(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); return targetBitmap; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 임계치 체크하기 - CheckThreshold(sourceByteArray, offset1, offset2, gradientValue, threshold, divideBy) /// <summary> /// 임계치 체크하기 /// </summary> /// <param name="sourceByteArray">소스 바이트 배열</param> /// <param name="offset1">오프셋 1</param> /// <param name="offset2">오프셋 2</param> /// <param name="gradientValue">그라디언트 값</param> /// <param name="threshold">임계치</param> /// <param name="divideBy">젯수</param> /// <returns>임계치 체크 결과</returns> private static bool CheckThreshold ( byte[] sourceByteArray, int offset1, int offset2, ref int gradientValue, byte threshold, int divideBy = 1 ) { gradientValue += Math.Abs(sourceByteArray[offset1] - sourceByteArray[offset2]) / divideBy; gradientValue += Math.Abs(sourceByteArray[offset1 + 1] - sourceByteArray[offset2 + 1]) / divideBy; gradientValue += Math.Abs(sourceByteArray[offset1 + 2] - sourceByteArray[offset2 + 2]) / divideBy; return (gradientValue >= threshold); } #endregion #region 그라디언트 기반 탐지 필터 적용하기 - ApplyGradientBasedEdgeDetectionFilter(sourceBitmap, threshold) /// <summary> /// 그라디언트 기반 탐지 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="threshold">임계치</param> /// <returns>비트맵</returns> public static Bitmap ApplyGradientBasedEdgeDetectionFilter(Bitmap sourceBitmap, byte threshold = 0) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] sourceByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; byte[] targetByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; Marshal.Copy(sourceBitmapData.Scan0, sourceByteArray, 0, sourceByteArray.Length); sourceBitmap.UnlockBits(sourceBitmapData); int sourceOffset = 0; int gradientValue = 0; bool exceedThreshold = false; for(int offsetY = 1; offsetY < sourceBitmap.Height - 1; offsetY++) { for(int offsetX = 1; offsetX < sourceBitmap.Width - 1; offsetX++) { sourceOffset = offsetY * sourceBitmapData.Stride + offsetX * 4; gradientValue = 0; exceedThreshold = true; CheckThreshold ( sourceByteArray, sourceOffset - 4, sourceOffset + 4, ref gradientValue, threshold, 2 ); exceedThreshold = CheckThreshold ( sourceByteArray, sourceOffset - sourceBitmapData.Stride, sourceOffset + sourceBitmapData.Stride, ref gradientValue, threshold, 2 ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, sourceOffset - 4, sourceOffset + 4, ref gradientValue, threshold ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, sourceOffset - sourceBitmapData.Stride, sourceOffset + sourceBitmapData.Stride, ref gradientValue, threshold ); if(exceedThreshold == false) { gradientValue = 0; CheckThreshold ( sourceByteArray, sourceOffset - 4 - sourceBitmapData.Stride, sourceOffset + 4 + sourceBitmapData.Stride, ref gradientValue, threshold, 2 ); exceedThreshold = CheckThreshold ( sourceByteArray, sourceOffset - sourceBitmapData.Stride + 4, sourceOffset - 4 + sourceBitmapData.Stride, ref gradientValue, threshold, 2 ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, sourceOffset - 4 - sourceBitmapData.Stride, sourceOffset + 4 + sourceBitmapData.Stride, ref gradientValue, threshold ); if(exceedThreshold == false) { gradientValue = 0; exceedThreshold = CheckThreshold ( sourceByteArray, sourceOffset - sourceBitmapData.Stride + 4, sourceOffset + sourceBitmapData.Stride - 4, ref gradientValue, threshold ); } } } } } targetByteArray[sourceOffset ] = (byte)(exceedThreshold ? 255 : 0); targetByteArray[sourceOffset + 1] = targetByteArray[sourceOffset]; targetByteArray[sourceOffset + 2] = targetByteArray[sourceOffset]; targetByteArray[sourceOffset + 3] = 255; } } Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb ); Marshal.Copy(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); return targetBitmap; } #endregion #region 바이트 자르기 - ClipByte(value) /// <summary> /// 바이트 자르기 /// </summary> /// <param name="value">값</param> /// <returns>바이트</returns> private static byte ClipByte(double value) { return (byte)(value > 255 ? 255 : (value < 0 ? 0 : value)); } #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 |
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 previewBitmap = null; /// <summary> /// 타겟 비트맵 /// </summary> private Bitmap targetBitmap = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.filterSizeComboBox.SelectedIndex = 0; this.colorShiftTypeComboBox.Items.Add(ColorShiftType.None ); this.colorShiftTypeComboBox.Items.Add(ColorShiftType.ShiftLeft ); this.colorShiftTypeComboBox.Items.Add(ColorShiftType.ShiftRight); this.colorShiftTypeComboBox.SelectedIndex = 0; this.edgeTracingTypeComboBox.Items.Add(EdgeTracingType.Black ); this.edgeTracingTypeComboBox.Items.Add(EdgeTracingType.DoubleIntensity); this.edgeTracingTypeComboBox.Items.Add(EdgeTracingType.HalfIntensity ); this.edgeTracingTypeComboBox.Items.Add(EdgeTracingType.ColorInversion ); this.edgeTracingTypeComboBox.Items.Add(EdgeTracingType.White ); this.edgeTracingTypeComboBox.SelectedIndex = 0; this.applyRedCheckBox.CheckedChanged += filterValueChanged; this.applyGreenCheckBox.CheckedChanged += filterValueChanged; this.applyBlueCheckBox.CheckedChanged += filterValueChanged; this.filterSizeComboBox.SelectedIndexChanged += filterValueChanged; this.colorShiftTypeComboBox.SelectedIndexChanged += filterValueChanged; this.edgeTracingTypeComboBox.SelectedIndexChanged += filterValueChanged; this.edgeThresholdNumericUpDown.ValueChanged += filterValueChanged; this.loadImageButton.Click += loadImageButton_Click; this.saveImageButton.Click += saveImageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 필터 값 변경시 처리하기 - filterValueChanged(sender, e) /// <summary> /// 필터 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void filterValueChanged(object sender, EventArgs e) { ApplyFilter(true); } #endregion #region 이미지 로드 버튼 클릭시 처리하기 - loadImageButton_Click(sender, e) /// <summary> /// 이미지 로드 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void loadImageButton_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 reader = new StreamReader(openFileDialog.FileName); this.sourceBitmap = Bitmap.FromStream(reader.BaseStream) as Bitmap; reader.Close(); this.previewBitmap = BitmapHelper.Copy(this.sourceBitmap, this.pictureBox.Width); this.pictureBox.Image = this.previewBitmap; ApplyFilter(true); } } #endregion #region 이미지 저장 버튼 클릭시 처리하기 - saveImageButton_Click(sender, e) /// <summary> /// 이미지 저장 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveImageButton_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 writer = new StreamWriter(saveFileDialog.FileName, false); this.targetBitmap.Save(writer.BaseStream, imageFormat); writer.Flush(); writer.Close(); this.targetBitmap = null; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 필터 적용하기 - ApplyFilter(preview) /// <summary> /// 필터 적용하기 /// </summary> /// <param name="preview">미리보기 여부</param> private void ApplyFilter(bool preview) { if(this.previewBitmap == null || this.filterSizeComboBox.SelectedIndex == -1) { return; } Bitmap sourceBitmap = null; Bitmap targetBitmap = null; if(preview == true) { sourceBitmap = this.previewBitmap; } else { sourceBitmap = this.sourceBitmap; } if(sourceBitmap != null) { if(this.filterSizeComboBox.SelectedItem.ToString() == "None") { targetBitmap = sourceBitmap; } else { int filterSize = 0; ColorShiftType colorShiftType = (ColorShiftType )this.colorShiftTypeComboBox.SelectedItem; EdgeTracingType edgeTracingType = (EdgeTracingType)this.edgeTracingTypeComboBox.SelectedItem; if(int.TryParse(this.filterSizeComboBox.SelectedItem.ToString(), out filterSize)) { targetBitmap = BitmapHelper.ApplyImageAbstractColorFilter ( sourceBitmap, filterSize, (byte)this.edgeThresholdNumericUpDown.Value, applyBlueCheckBox.Checked, applyGreenCheckBox.Checked, applyRedCheckBox.Checked, edgeTracingType, colorShiftType ); } } } if(targetBitmap != null) { if(preview == true) { this.pictureBox.Image = targetBitmap; } else { this.targetBitmap = targetBitmap; } } } #endregion } } |