■ Graphics 클래스의 DrawImage 메소드 사용시 이미지 어트리뷰트를 설정하는 방법을 보여준다.
▶ ImageExtension.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 |
using System.Drawing; using System.Drawing.Imaging; namespace TestProject { /// <summary> /// 이미지 확장 /// </summary> public static class ImageExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 이미지 복사하기 - CopyImage(sourceImage) /// <summary> /// 이미지 복사하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <returns>이미지</returns> public static Image CopyImage(this Image sourceImage) { return (Image)sourceImage.Clone(); } #endregion #region 이미지 복사하기 - CopyImage(sourceImage, imageAttributes) /// <summary> /// 이미지 복사하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <param name="imageAttributes">이미지 어트리뷰트</param> /// <returns>이미지</returns> public static Image CopyImage(this Image sourceImage, ImageAttributes imageAttributes) { Bitmap targetBitmap = new Bitmap(sourceImage.Width, sourceImage.Height); using(Graphics graphics = Graphics.FromImage(targetBitmap)) { Rectangle targetRectangle = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height); graphics.DrawImage ( sourceImage, targetRectangle, 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, imageAttributes ); } 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 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 |
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 TextBox[][] textBoxArrayArray; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.Load += Form_Load; this.openMenuItem.Click += openMenuItem_Click; this.saveAsMenuItem.Click += saveAsMenuItem_Click; this.exitMenuItem.Click += exitMenuItem_Click; this.identityMenuItem.Click += presetMenuItem_Click; this.redMenuItem.Click += presetMenuItem_Click; this.limeMenuItem.Click += presetMenuItem_Click; this.blueMenuItem.Click += presetMenuItem_Click; this.greenMenuItem.Click += presetMenuItem_Click; this.yellowMenuItem.Click += presetMenuItem_Click; this.orangeMenuItem.Click += presetMenuItem_Click; this.pinkMenuItem.Click += presetMenuItem_Click; this.lightGreenMenuItem.Click += presetMenuItem_Click; this.lightBlueMenuItem.Click += presetMenuItem_Click; this.cyanMenuItem.Click += presetMenuItem_Click; this.fuchsiaMenuItem.Click += presetMenuItem_Click; this.averageMenuItem.Click += presetMenuItem_Click; this.monochromeMenuItem.Click += presetMenuItem_Click; this.sepiaMenuItem.Click += presetMenuItem_Click; this.brighterButton.Click += brighterButton_Click; this.darkerButton.Click += darkerButton_Click; this.colorizeButton.Click += colorizeButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.textBoxArrayArray = new TextBox[][] { new TextBox[] { this.value00TextBox, this.value01TextBox, this.value02TextBox, this.value03TextBox, this.value04TextBox }, new TextBox[] { this.value10TextBox, this.value11TextBox, this.value12TextBox, this.value13TextBox, this.value14TextBox }, new TextBox[] { this.value20TextBox, this.value21TextBox, this.value22TextBox, this.value23TextBox, this.value24TextBox }, new TextBox[] { this.value30TextBox, this.value31TextBox, this.value32TextBox, this.value33TextBox, this.value34TextBox }, new TextBox[] { this.value40TextBox, this.value41TextBox, this.value42TextBox, this.value43TextBox, this.value44TextBox } }; } #endregion #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); Colorize(); } } #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) { SaveImage(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 기정의 세트 메뉴 항목 클릭시 처리하기 - presetMenuItem_Click(sender, e) /// <summary> /// 기정의 세트 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void presetMenuItem_Click(object sender, EventArgs e) { foreach(TextBox[] textBoxArray in this.textBoxArrayArray) { foreach(TextBox textBox in textBoxArray) { textBox.Text = "0.0000"; } } for(int i = 3; i < 5; i++) { this.textBoxArrayArray[i][i].Text = "1.0000"; } ToolStripMenuItem menuItem = sender as ToolStripMenuItem; if(menuItem.Tag.ToString().ToLower() == "배경색 사용") { Color color = menuItem.BackColor; this.value00TextBox.Text = (color.R / 255f).ToString("f4"); this.value11TextBox.Text = (color.G / 255f).ToString("f4"); this.value22TextBox.Text = (color.B / 255f).ToString("f4"); this.value33TextBox.Text = "1.0000"; this.value44TextBox.Text = "1.0000"; } else { string colorName = menuItem.Text.Replace("&", "").ToLower(); if(colorName == "identity") { for(int i = 0; i < 3; i++) { this.textBoxArrayArray[i][i].Text = "1.0000"; } } else if(colorName == "average") { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { this.textBoxArrayArray[i][j].Text = "0.3333"; } } } else if(colorName == "monochrome") { for(int i = 0; i < 3; i++) { this.textBoxArrayArray[0][i].Text = "0.2990"; } for(int i = 0; i < 3; i++) { this.textBoxArrayArray[1][i].Text = "0.5870"; } for(int i = 0; i < 3; i++) { this.textBoxArrayArray[2][i].Text = "0.1140"; } } else if(colorName == "sepia") { this.value00TextBox.Text = "0.3930"; this.value01TextBox.Text = "0.3490"; this.value02TextBox.Text = "0.2720"; this.value10TextBox.Text = "0.7690"; this.value11TextBox.Text = "0.6860"; this.value12TextBox.Text = "0.5340"; this.value20TextBox.Text = "0.1890"; this.value20TextBox.Text = "0.1680"; this.value20TextBox.Text = "0.1310"; } else { MessageBox.Show("Unknown preset"); return; } } Colorize(); } #endregion #region 밝게 버튼 클릭시 처리하기 - brighterButton_Click(sender, e) /// <summary> /// 밝게 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void brighterButton_Click(object sender, EventArgs e) { float[][] valueArray = GetMatrix(); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(Math.Abs(valueArray[i][j]) < 0.01f) { continue; } valueArray[i][j] += 0.1f; } } SaveMatrix(valueArray); Colorize(); } #endregion #region 어둡게 버튼 클릭시 처리하기 - darkerButton_Click(sender, e) /// <summary> /// 어둡게 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void darkerButton_Click(object sender, EventArgs e) { float[][] valueArrayArray = GetMatrix(); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(Math.Abs(valueArrayArray[i][j]) < 0.01f) { continue; } valueArrayArray[i][j] -= 0.1f; if(valueArrayArray[i][j] < 0f) { valueArrayArray[i][j] = 0f; } } } SaveMatrix(valueArrayArray); Colorize(); } #endregion #region 채색하기 버튼 클릭시 처리하기 - colorizeButton_Click(sender, e) /// <summary> /// 채색하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void colorizeButton_Click(object sender, EventArgs e) { Colorize(); } #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 매트릭스 구하기 - GetMatrix() /// <summary> /// 매트릭스 구하기 /// </summary> /// <returns>매트릭스</returns> private float[][] GetMatrix() { float[][] valueArray = new float[][] { new float[5], new float[5], new float[5], new float[5], new float[5], }; for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { float value; if(!float.TryParse(this.textBoxArrayArray[i][j].Text, out value)) { MessageBox.Show("Invalid entry"); this.textBoxArrayArray[i][j].Focus(); return null; } valueArray[i][j] = value; } } return valueArray; } #endregion #region 색상 매트릭스 구하기 - GetColorMatrix() /// <summary> /// 색상 매트릭스 구하기 /// </summary> /// <returns>색상 매트릭스</returns> private ColorMatrix GetColorMatrix() { float[][] valueArray = GetMatrix(); if(valueArray == null) { return null; } return new ColorMatrix(valueArray); } #endregion #region 이미지 구하기 - GetImage(image, colorMatrix) /// <summary> /// 이미지 구하기 /// </summary> /// <param name="image">이미지</param> /// <param name="colorMatrix">색상 매트릭스</param> /// <returns>이미지</returns> private Image GetImage(Image image, ColorMatrix colorMatrix) { ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix(colorMatrix); return image.CopyImage(imageAttributes); } #endregion #region 채색하기 - Colorize() /// <summary> /// 채색하기 /// </summary> private void Colorize() { ColorMatrix colorMatrix = GetColorMatrix(); this.targetPictureBox.Image = GetImage(this.sourcePictureBox.Image, colorMatrix); this.saveAsMenuItem.Enabled = (this.targetPictureBox.Image != null); } #endregion #region 이미지 저장하기 - SaveImage(image, filePath) /// <summary> /// 이미지 저장하기 /// </summary> /// <param name="image">이미지</param> /// <param name="filePath">파일 경로</param> private void SaveImage(Image image, string filePath) { string fileExtension = Path.GetExtension(filePath); switch(fileExtension.ToLower()) { case ".bmp" : image.Save(filePath, ImageFormat.Bmp ); break; case ".exif" : image.Save(filePath, ImageFormat.Exif); break; case ".gif" : image.Save(filePath, ImageFormat.Gif ); break; case ".jpg" : case ".jpeg" : image.Save(filePath, ImageFormat.Jpeg); break; case ".png" : image.Save(filePath, ImageFormat.Png ); break; case ".tif" : case ".tiff" : image.Save(filePath, ImageFormat.Tiff); break; default : throw new NotSupportedException("Unknown file extension " + fileExtension); } } #endregion #region 매트릭스 저장하기 - SaveMatrix(valueArrayArray) /// <summary> /// 매트릭스 저장하기 /// </summary> /// <param name="valueArrayArray">값 배열 배열</param> private void SaveMatrix(float[][] valueArrayArray) { for(int i = 0; i < 5; i++) { for(int j = 0; j < 5; j++) { this.textBoxArrayArray[i][j].Text = valueArrayArray[i][j].ToString("f4"); } } } #endregion } } |