■ Bitmap 클래스에서 회색조/투명/네거티브/세피아 톤 필터를 사용하는 방법을 보여준다.
▶ 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 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 투명 비트맵 구하기 - GetTransparentBitmap(sourceImage, alpha) /// <summary> /// 투명 비트맵 구하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <param name="alpha">알파</param> /// <returns>투명 비트맵</returns> public static Bitmap GetTransparentBitmap(Image sourceImage, byte alpha = 100) { Bitmap targetBitmap = GetBitmap(sourceImage); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); IntPtr sourceHandle = targetBitmapData.Scan0; byte[] targetByteArray = new byte[targetBitmapData.Stride * targetBitmap.Height]; Marshal.Copy(sourceHandle, targetByteArray, 0, targetByteArray.Length); for(int i = 3; i < targetByteArray.Length; i += 4) { targetByteArray[i] = alpha; } Marshal.Copy(targetByteArray, 0, sourceHandle, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); targetBitmapData = null; targetByteArray = null; return targetBitmap; } #endregion #region 회색조 비트맵 구하기 - GetGrayscaleBitmap(sourceImage) /// <summary> /// 회색조 비트맵 구하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <returns>회색조 비트맵</returns> public static Bitmap GetGrayscaleBitmap(Image sourceImage) { Bitmap targetBitmap = GetBitmap(sourceImage); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); IntPtr sourceHandle = targetBitmapData.Scan0; byte[] targetByteArray = new byte[targetBitmapData.Stride * targetBitmap.Height]; Marshal.Copy(sourceHandle, targetByteArray, 0, targetByteArray.Length); float rgb = 0; for(int i = 0; i < targetByteArray.Length; i += 4) { rgb = targetByteArray[i ] * 0.11f; rgb += targetByteArray[i + 1] * 0.59f; rgb += targetByteArray[i + 2] * 0.3f; targetByteArray[i ] = (byte)rgb; targetByteArray[i + 1] = targetByteArray[i]; targetByteArray[i + 2] = targetByteArray[i]; targetByteArray[i + 3] = 255; } Marshal.Copy(targetByteArray, 0, sourceHandle, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); targetBitmapData = null; targetByteArray = null; return targetBitmap; } #endregion #region 네거티브 비트맵 구하기 - GetNegativeBitmap(sourceImage) /// <summary> /// 네거티브 비트맵 구하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <returns>네거티브 비트맵</returns> public static Bitmap GetNegativeBitmap(Image sourceImage) { Bitmap targetBitmap = GetBitmap(sourceImage); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); IntPtr sourceHandle = targetBitmapData.Scan0; byte[] targetByteArray = new byte[targetBitmapData.Stride * targetBitmap.Height]; Marshal.Copy(sourceHandle, targetByteArray, 0, targetByteArray.Length); byte[] pixelByteArray = null; int pixel = 0; for(int i = 0; i < targetByteArray.Length; i += 4) { pixel = ~BitConverter.ToInt32(targetByteArray, i); pixelByteArray = BitConverter.GetBytes(pixel); targetByteArray[i ] = pixelByteArray[0]; targetByteArray[i + 1] = pixelByteArray[1]; targetByteArray[i + 2] = pixelByteArray[2]; } Marshal.Copy(targetByteArray, 0, sourceHandle, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); targetBitmapData = null; targetByteArray = null; return targetBitmap; } #endregion #region 세피아 톤 비트맵 구하기 - GetSepiaToneBitmap(sourceImage) /// <summary> /// 세피아 톤 비트맵 구하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <returns>세피아 톤 비트맵</returns> public static Bitmap GetSepiaToneBitmap(Image sourceImage) { Bitmap targetBitmap = GetBitmap(sourceImage); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); IntPtr sourceHandle = targetBitmapData.Scan0; byte[] targetByteArray = new byte[targetBitmapData.Stride * targetBitmap.Height]; Marshal.Copy(sourceHandle, targetByteArray, 0, targetByteArray.Length); byte maximumValue = 255; float red = 0; float green = 0; float blue = 0; for(int i = 0; i < targetByteArray.Length; i += 4) { red = targetByteArray[i] * 0.189f + targetByteArray[i + 1] * 0.769f + targetByteArray[i + 2] * 0.393f; green = targetByteArray[i] * 0.168f + targetByteArray[i + 1] * 0.686f + targetByteArray[i + 2] * 0.349f; blue = targetByteArray[i] * 0.131f + targetByteArray[i + 1] * 0.534f + targetByteArray[i + 2] * 0.272f; targetByteArray[i + 2] = (red > maximumValue ? maximumValue : (byte)red ); targetByteArray[i + 1] = (green > maximumValue ? maximumValue : (byte)green); targetByteArray[i ] = (blue > maximumValue ? maximumValue : (byte)blue ); } Marshal.Copy(targetByteArray, 0, sourceHandle, targetByteArray.Length); targetBitmap.UnlockBits(targetBitmapData); targetBitmapData = null; targetByteArray = null; return targetBitmap; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 비트맵 구하기 - GetBitmap(sourceImage) /// <summary> /// 비트맵 구하기 /// </summary> /// <param name="sourceImage">소스 이미지</param> /// <returns>비트맵</returns> private static Bitmap GetBitmap(Image sourceImage) { Bitmap targetBitmap = new Bitmap ( sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb ); using(Graphics graphics = Graphics.FromImage(targetBitmap)) { graphics.DrawImage ( sourceImage, new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), GraphicsUnit.Pixel ); graphics.Flush(); } 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 |
using System; using System.Drawing; 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.loadImageButton.Click += loadImageButton_Click; this.grayscaleImageRadioButton.CheckedChanged += imageRadioButton_CheckedChanged; this.transparencyImageRadioButton.CheckedChanged += imageRadioButton_CheckedChanged; this.negativeImageRadioButton.CheckedChanged += imageRadioButton_CheckedChanged; this.sepiaToneImageRadioButton.CheckedChanged += imageRadioButton_CheckedChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #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 files (*.png)|*.png|Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg"; if(openFileDialog.ShowDialog() == DialogResult.OK) { StreamReader streamReader = new StreamReader(openFileDialog.FileName); Bitmap sourceBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream); streamReader.Close(); this.inputImagePanel.BackgroundImage = sourceBitmap; imageRadioButton_CheckedChanged(sender, e); } } #endregion #region 이미지 라디오 버튼 클릭시 처리하기 - imageRadioButton_CheckedChanged(sender, e) /// <summary> /// 이미지 라디오 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void imageRadioButton_CheckedChanged(object sender, EventArgs e) { if(this.inputImagePanel.BackgroundImage != null) { if(this.grayscaleImageRadioButton.Checked == true) { this.outputImagePanel.BackgroundImage = BitmapHelper.GetGrayscaleBitmap(this.inputImagePanel.BackgroundImage); } else if(this.transparencyImageRadioButton.Checked == true) { this.outputImagePanel.BackgroundImage = BitmapHelper.GetTransparentBitmap(this.inputImagePanel.BackgroundImage); } else if(this.negativeImageRadioButton.Checked == true) { this.outputImagePanel.BackgroundImage = BitmapHelper.GetNegativeBitmap(this.inputImagePanel.BackgroundImage); } else if(this.sepiaToneImageRadioButton.Checked == true) { this.outputImagePanel.BackgroundImage = BitmapHelper.GetSepiaToneBitmap(this.inputImagePanel.BackgroundImage); } } } #endregion } } |