■ Bitmap 클래스에서 바이-톤 필터(Bi-tonal Filter)를 사용하는 방법을 보여준다.
▶ 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 |
using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Drawing.Drawing2D; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// 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 바이-톤 필터 적용하기 - ApplyBitonalFilter(sourceBitmap, darkColor, lightColor, threshold) /// <summary> /// 바이-톤 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="darkColor">어두운 색상</param> /// <param name="lightColor">밝은 색상</param> /// <param name="threshold">임계치</param> /// <returns>비트맵</returns> public static Bitmap ApplyBitonalFilter(Bitmap sourceBitmap, Color darkColor, Color lightColor, int threshold) { 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); for(int i = 0; i + 4 < targetByteArray.Length; i += 4) { if (targetByteArray[i] + targetByteArray[i + 1] + targetByteArray[i + 2] <= threshold) { targetByteArray[i ] = darkColor.B; targetByteArray[i + 1] = darkColor.G; targetByteArray[i + 2] = darkColor.R; } else { targetByteArray[i ] = lightColor.B; targetByteArray[i + 1] = lightColor.G; targetByteArray[i + 2] = lightColor.R; } } 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 } } |
▶ 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 |
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.darkColorPanel.Click += colorPanel_Click; this.lightColorPanel.Click += colorPanel_Click; this.thresholdTrackBar.ValueChanged += thresholdTrackBar_ValueChanged; this.loadImageButton.Click += localImageButton_Click; this.saveImageButton.Click += saveImageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 색상 패널 클릭시 처리하기 - colorPanel_Click(sender, e) /// <summary> /// 색상 패널 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void colorPanel_Click(object sender, EventArgs e) { if(sender is Panel) { Panel panel = (Panel)sender; ColorDialog colorDialog = new ColorDialog(); colorDialog.AllowFullOpen = true; colorDialog.Color = panel.BackColor; colorDialog.FullOpen = true; if(colorDialog.ShowDialog() == DialogResult.OK) { panel.BackColor = colorDialog.Color; ApplyFilter(true); } } } #endregion #region 임계치 트랙바 값 변경시 처리하기 - thresholdTrackBar_ValueChanged(sender, e) /// <summary> /// 임계치 트랙바 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void thresholdTrackBar_ValueChanged(object sender, EventArgs e) { this.thresholdValueLabel.Text = this.thresholdTrackBar.Value.ToString(); ApplyFilter(true); } #endregion #region 이미지 로드 버튼 클릭시 처리하기 - localImageButton_Click(sender, e) /// <summary> /// 이미지 로드 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void localImageButton_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.FromStream(streamReader.BaseStream) as Bitmap; streamReader.Close(); this.previewBitmap = BitmapHelper.GetBitmap(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 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) { if(this.previewBitmap == null) { return; } if(preview == true) { this.pictureBox.Image = BitmapHelper.ApplyBitonalFilter ( this.previewBitmap, this.darkColorPanel.BackColor, this.lightColorPanel.BackColor, this.thresholdTrackBar.Value ); } else { this.targetBitmap = BitmapHelper.ApplyBitonalFilter ( this.sourceBitmap, this.darkColorPanel.BackColor, this.lightColorPanel.BackColor, this.thresholdTrackBar.Value ); } } #endregion } } |