■ 비트맵 이진화를 적용하는 방법을 보여준다.
▶ 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 |
using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 이미지 바이트 배열 /// </summary> public byte[] ImageByteArray; /// <summary> /// 행 크기 바이트 수 /// </summary> public int RowSizeByteCount; /// <summary> /// 픽셀 데이터 크기 /// </summary> public const int PixelDataSize = 32; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 비트맵 /// </summary> private Bitmap bitmap; /// <summary> /// 비트맵 데이터 /// </summary> private BitmapData bitmapData; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 너비 - Width /// <summary> /// 너비 /// </summary> public int Width { get { return this.bitmap.Width; } } #endregion #region 높이 - Height /// <summary> /// 높이 /// </summary> public int Height { get { return this.bitmap.Height; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - BitmapHelper(bitmap) /// <summary> /// 생성자 /// </summary> /// <param name="bitmap">비트맵</param> public BitmapHelper(Bitmap bitmap) { this.bitmap = bitmap; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 잠그기 - LockBitmap() /// <summary> /// 비트맵 잠그기 /// </summary> public void LockBitmap() { Rectangle boundRectangle = new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height); this.bitmapData = this.bitmap.LockBits ( boundRectangle, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb ); RowSizeByteCount = this.bitmapData.Stride; int totalSize = this.bitmapData.Stride * this.bitmapData.Height; ImageByteArray = new byte[totalSize]; Marshal.Copy(this.bitmapData.Scan0, ImageByteArray, 0, totalSize); } #endregion #region 비트맵 잠금 해제하기 - UnlockBitmap() /// <summary> /// 비트맵 잠금 해제하기 /// </summary> public void UnlockBitmap() { int totalSize = this.bitmapData.Stride * this.bitmapData.Height; Marshal.Copy(ImageByteArray, 0, this.bitmapData.Scan0, totalSize); this.bitmap.UnlockBits(this.bitmapData); ImageByteArray = null; this.bitmapData = null; } #endregion #region 픽셀 구하기 - GetPixel(x, y, red, green, blue, alpha) /// <summary> /// 픽셀 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="red">빨강색</param> /// <param name="green">녹색</param> /// <param name="blue">파랑색</param> /// <param name="alpha">불투명도</param> public void GetPixel(int x, int y, out byte red, out byte green, out byte blue, out byte alpha) { int i = y * this.bitmapData.Stride + x * 4; blue = ImageByteArray[i++]; green = ImageByteArray[i++]; red = ImageByteArray[i++]; alpha = ImageByteArray[i ]; } #endregion #region 픽셀 설정하기 - SetPixel(x, y, red, green, blue, alpha) /// <summary> /// 픽셀 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="red">빨강색</param> /// <param name="green">녹색</param> /// <param name="blue">파랑색</param> /// <param name="alpha">불투명도</param> public void SetPixel(int x, int y, byte red, byte green, byte blue, byte alpha) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i++] = blue; ImageByteArray[i++] = green; ImageByteArray[i++] = red; ImageByteArray[i ] = alpha; } #endregion #region 빨강색 구하기 - GetRed(x, y) /// <summary> /// 빨강색 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>빨강색</returns> public byte GetRed(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i + 2]; } #endregion #region 빨강색 설정하기 - SetRed(x, y, red) /// <summary> /// 빨강색 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="red">빨강색</param> public void SetRed(int x, int y, byte red) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i + 2] = red; } #endregion #region 녹색 구하기 - GetGreen(x, y) /// <summary> /// 녹색 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>녹색</returns> public byte GetGreen(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i + 1]; } #endregion #region 녹색 설정하기 - SetGreen(x, y, green) /// <summary> /// 녹색 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="green">녹색</param> public void SetGreen(int x, int y, byte green) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i + 1] = green; } #endregion #region 파랑색 구하기 - GetBlue(x, y) /// <summary> /// 파랑색 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>파랑색</returns> public byte GetBlue(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i]; } #endregion #region 파랑색 설정하기 - SetBlue(x, y, blue) /// <summary> /// 파랑색 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="blue">파랑색</param> public void SetBlue(int x, int y, byte blue) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i] = blue; } #endregion #region 붙투명도 구하기 - GetAlpha(x, y) /// <summary> /// 붙투명도 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>불투명도</returns> public byte GetAlpha(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i + 3]; } #endregion #region 불투명도 설정하기 - SetAlpha(x, y, alpha) /// <summary> /// 불투명도 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="alpha">불투명도</param> public void SetAlpha(int x, int y, byte alpha) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i + 3] = alpha; } #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 |
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 { //////////////////////////////////////////////////////////////////////////////////////////////////// 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.cutoffScrollBar.Scroll += cutoffScrollBar_Scroll; } #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.openFileDialog.ShowDialog() == DialogResult.OK) { this.sourcePictureBox.Image = GetBitmap(this.openFileDialog.FileName); ProcessBinaryContrast(); } } #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) { SaveBitmap((Bitmap)this.targetPictureBox.Image, 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 컷오프 스크롤바 스크롤시 처리하기 - cutoffScrollBar_Scroll(sender, e) /// <summary> /// 컷오프 스크롤바 스크롤시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cutoffScrollBar_Scroll(object sender, ScrollEventArgs e) { this.cutoffValueLabel.Text = this.cutoffScrollBar.Value.ToString(); ProcessBinaryContrast(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 비트맵 구하기 - GetBitmap(filePath) /// <summary> /// 비트맵 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>비트맵</returns> private Bitmap GetBitmap(string filePath) { using(Bitmap bitmap = new Bitmap(filePath)) { return new Bitmap(bitmap); } } #endregion #region 이진 대비 적용하기 - ApplyBinaryContrast(targetBitmap, cutoff) /// <summary> /// 이진 대비 적용하기 /// </summary> /// <param name="targetBitmap">타겟 비트맵</param> /// <param name="cutoff">컷오프</param> private void ApplyBinaryContrast(Bitmap targetBitmap, int cutoff) { BitmapHelper helper = new BitmapHelper(targetBitmap); helper.LockBitmap(); for(int y = 0; y < targetBitmap.Height; y++) { for(int x = 0; x < targetBitmap.Width; x++) { byte red; byte green; byte blue; byte alpha; helper.GetPixel(x, y, out red, out green, out blue, out alpha); if(red + green + blue > cutoff) { helper.SetPixel(x, y, 255, 255, 255, 255); } else { helper.SetPixel(x, y, 0, 0, 0, 255); } } } helper.UnlockBitmap(); } #endregion #region 이진 대비 처리하기 - ProcessBinaryContrast() /// <summary> /// 이진 대비 처리하기 /// </summary> private void ProcessBinaryContrast() { if(this.sourcePictureBox.Image == null) { return; } Cursor = Cursors.WaitCursor; Bitmap bitmap = new Bitmap(this.sourcePictureBox.Image); ApplyBinaryContrast(bitmap, 3 * this.cutoffScrollBar.Value); if(this.targetPictureBox.Image != null) { this.targetPictureBox.Image.Dispose(); } this.targetPictureBox.Image = bitmap; this.targetPictureBox.Left = this.sourcePictureBox.Right + 4; Cursor = Cursors.Default; } #endregion #region 비트맵 저장하기 - SaveBitmap(bitmap, filePath) /// <summary> /// 비트맵 저장하기 /// </summary> /// <param name="bitmap">비트맵</param> /// <param name="filePath">파일 경로</param> public void SaveBitmap(Bitmap bitmap, string filePath) { string extension = Path.GetExtension(filePath); switch(extension.ToLower()) { case ".bmp" : bitmap.Save(filePath, ImageFormat.Bmp ); break; case ".exif" : bitmap.Save(filePath, ImageFormat.Exif); break; case ".gif" : bitmap.Save(filePath, ImageFormat.Gif ); break; case ".jpg" : case ".jpeg" : bitmap.Save(filePath, ImageFormat.Jpeg); break; case ".png" : bitmap.Save(filePath, ImageFormat.Png ); break; case ".tif" : case ".tiff" : bitmap.Save(filePath, ImageFormat.Tiff); break; default : throw new NotSupportedException("Unknown file extension " + extension); } } #endregion } } |