■ Bitmap 클래스를 사용해 ARGB 색상 채널을 교환하는 방법을 보여준다.
▶ 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 |
namespace TestProject { /// <summary> /// 색상 교환 타입 /// </summary> public enum ColorSwapType { /// <summary> /// ShiftRight /// </summary> ShiftRight, /// <summary> /// ShiftLeft /// </summary> ShiftLeft, /// <summary> /// SwapBlueAndRed /// </summary> SwapBlueAndRed, /// <summary> /// SwapBlueAndGreen /// </summary> SwapBlueAndGreen, /// <summary> /// SwapRedAndGreen /// </summary> SwapRedAndGreen } } |
▶ ColorSwapFilter.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 |
namespace TestProject { /// <summary> /// 색상 교환 필터 /// </summary> public class ColorSwapFilter { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 색상 교환 타입 /// </summary> private ColorSwapType swapType = ColorSwapType.ShiftRight; /// <summary> /// 중간 색상 값 교환 여부 /// </summary> private bool swapHalfColorValue = false; /// <summary> /// 교환시 색상 반전 여부 /// </summary> private bool invertColorWhenSwapping = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 색상 교환 타입 - SwapType /// <summary> /// 색상 교환 타입 /// </summary> public ColorSwapType SwapType { get { return this.swapType; } set { this.swapType = value; } } #endregion #region 중간 색상 값 교환 여부 - SwapHalfColorValue /// <summary> /// 중간 색상 값 교환 여부 /// </summary> public bool SwapHalfColorValue { get { return this.swapHalfColorValue; } set { this.swapHalfColorValue = value; } } #endregion #region 교환시 색상 반전 여부 - InvertColorWhenSwapping /// <summary> /// 교환시 색상 반전 여부 /// </summary> public bool InvertColorWhenSwapping { get { return this.invertColorWhenSwapping; } set { this.invertColorWhenSwapping = value; } } #endregion } } |
▶ 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 |
using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 색상 교환하기 - SwapColor(sourceBitmap, colorSwapFilter) /// <summary> /// 색상 교환하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="colorSwapFilter">색상 교환 필터</param> /// <returns>비트맵</returns> public static Bitmap SwapColor(Bitmap sourceBitmap, ColorSwapFilter colorSwapFilter) { 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); byte sourceBlue = 0; byte sourceGreen = 0; byte sourceRed = 0; byte targetBlue = 0; byte targetGreen = 0; byte targetRed = 0; byte byte2 = 2; byte maximumValue = 255; for(int k = 0; k < targetByteArray.Length; k += 4) { sourceBlue = targetByteArray[k ]; sourceGreen = targetByteArray[k + 1]; sourceRed = targetByteArray[k + 2]; if(colorSwapFilter.InvertColorWhenSwapping) { sourceBlue = (byte)(maximumValue - sourceBlue ); sourceGreen = (byte)(maximumValue - sourceGreen); sourceRed = (byte)(maximumValue - sourceRed ); } if(colorSwapFilter.SwapHalfColorValue) { sourceBlue = (byte)(sourceBlue / byte2); sourceGreen = (byte)(sourceGreen / byte2); sourceRed = (byte)(sourceRed / byte2); } switch(colorSwapFilter.SwapType) { case ColorSwapType.ShiftRight : { targetBlue = sourceGreen; targetRed = sourceBlue; targetGreen = sourceRed; break; } case ColorSwapType.ShiftLeft : { targetBlue = sourceRed; targetRed = sourceGreen; targetGreen = sourceBlue; break; } case ColorSwapType.SwapBlueAndRed: { targetBlue = sourceRed; targetRed = sourceBlue; break; } case ColorSwapType.SwapBlueAndGreen : { targetBlue = sourceGreen; targetGreen = sourceBlue; break; } case ColorSwapType.SwapRedAndGreen : { targetRed = sourceGreen; targetGreen = sourceGreen; break; } } targetByteArray[k ] = targetBlue; targetByteArray[k + 1] = targetGreen; targetByteArray[k + 2] = targetRed; } Bitmap targetBitmap = new Bitmap ( sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format32bppArgb ); 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 |
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.colorFilterTypeComboBox.Items.Add(ColorSwapType.ShiftLeft ); this.colorFilterTypeComboBox.Items.Add(ColorSwapType.ShiftRight ); this.colorFilterTypeComboBox.Items.Add(ColorSwapType.SwapBlueAndGreen); this.colorFilterTypeComboBox.Items.Add(ColorSwapType.SwapBlueAndRed ); this.colorFilterTypeComboBox.Items.Add(ColorSwapType.SwapRedAndGreen ); this.colorFilterTypeComboBox.SelectedIndex = 0; this.colorFilterTypeComboBox.SelectedIndexChanged += colorFilterTypeComboBox_SelectedIndexChanged; this.invertColorCheckBox.CheckedChanged += invertColorCheckBox_CheckedChanged; this.swapHalfColorCheckBox.CheckedChanged += swapHalfColorCheckBox_CheckedChanged; this.loadImageButton.Click += loadImageButton_Click; this.saveImageButton.Click += saveImageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 색상 필터 타입 콤보 박스 선택 인덱스 변경시 처리하기 - colorFilterTypeComboBox_SelectedIndexChanged(sender, e) /// <summary> /// 색상 필터 타입 콤보 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void colorFilterTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) { ApplyFilter(); } #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(); } #endregion #region 중간 색상 교환 체크 박스 체크 변경시 처리하기 - swapHalfColorCheckBox_CheckedChanged(sender, e) /// <summary> /// 중간 색상 교환 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void swapHalfColorCheckBox_CheckedChanged(object sender, EventArgs e) { ApplyFilter(); } #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|비트맵 파일 (*.bmp)|*.bmp|JPEG 파일 (*.jpg)|*.jpg"; if(openFileDialog.ShowDialog() == DialogResult.OK) { StreamReader streamReader = new StreamReader(openFileDialog.FileName); Bitmap sourceBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream); streamReader.Close(); this.sourcePictureBox.Image = sourceBitmap; ApplyFilter(); } } #endregion #region 이미지 저장 버튼 클릭시 처리하기 - saveImageButton_Click(sender, e) /// <summary> /// 이미지 저장 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveImageButton_Click(object sender, EventArgs e) { if(this.targetPictureBox.Image != null) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "이미지 파일 선택"; saveFileDialog.Filter = "PNG 파일 (*.png)|*.png|비트맵 파일 (*.bmp)|*.bmp|JPEG 파일 (*.jpg)|*.jpg"; 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.targetPictureBox.Image.Save(streamWriter.BaseStream, imageFormat); streamWriter.Flush(); streamWriter.Close(); } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 필터 적용하기 - ApplyFilter() /// <summary> /// 필터 적용하기 /// </summary> private void ApplyFilter() { if(this.sourcePictureBox.Image != null) { ColorSwapFilter colorSwapFilter = new ColorSwapFilter(); colorSwapFilter.SwapType = (ColorSwapType)this.colorFilterTypeComboBox.SelectedItem; colorSwapFilter.InvertColorWhenSwapping = this.invertColorCheckBox.Checked; colorSwapFilter.SwapHalfColorValue = this.swapHalfColorCheckBox.Checked; Bitmap sourceBitmap = this.sourcePictureBox.Image as Bitmap; this.targetPictureBox.Image = BitmapHelper.SwapColor(sourceBitmap, colorSwapFilter); } } #endregion } } |