■ Bitmap 클래스에서 평균 색상 필터(Average Color Filter)를 사용하는 방법을 보여준다.
▶ ColorShiftType.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 |
namespace TestProject { /// <summary> /// 색상 시프트 타입 /// </summary> public enum ColorShiftType { /// <summary> /// 해당 무 /// </summary> None, /// <summary> /// 왼쪽 시프트 /// </summary> ShiftLeft, /// <summary> /// 오른쪽 시프트 /// </summary> ShiftRight } } |
▶ 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 |
using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 로드하기 - LoadBitmap(filePath) /// <summary> /// 비트맵 로드하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>비트맵</returns> public static Bitmap LoadBitmap(string filePath) { using(Bitmap bitmap = new Bitmap(filePath)) { return new Bitmap(bitmap); } } #endregion #region 평균 색상 필터 적용하기 - ApplyAverageColorFilter(sourceBitmap, matrixSize, applyBlue, applyGreen, applyRed, shiftType) /// <summary> /// 평균 색상 필터 적용하기 /// </summary> /// <param name="sourceBitmap">소스 비트맵</param> /// <param name="matrixSize">매트릭스 크기</param> /// <param name="applyBlue">청색 채널 적용 여부</param> /// <param name="applyGreen">녹색 채널 적용 여부</param> /// <param name="applyRed">적색 채널 적용 여부</param> /// <param name="shiftType">색상 시프트 타입</param> /// <returns>비트맵</returns> public static Bitmap ApplyAverageColorFilter ( Bitmap sourceBitmap, int matrixSize, bool applyBlue = true, bool applyGreen = true, bool applyRed = true, ColorShiftType shiftType = ColorShiftType.None ) { BitmapData sourceBitmapData = sourceBitmap.LockBits ( new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); byte[] sourceByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; byte[] targetByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height]; Marshal.Copy(sourceBitmapData.Scan0, sourceByteArray, 0, sourceByteArray.Length); sourceBitmap.UnlockBits(sourceBitmapData); int filterOffset = (matrixSize - 1) / 2; int sourceOffset = 0; int targetOffset = 0; int blue = 0; int green = 0; int red = 0; for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++) { for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++) { targetOffset = offsetY * sourceBitmapData.Stride + offsetX * 4; blue = 0; green = 0; red = 0; for(int filterY = -filterOffset; filterY <= filterOffset; filterY++) { for(int filterX = -filterOffset; filterX <= filterOffset; filterX++) { sourceOffset = targetOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride); blue += sourceByteArray[sourceOffset ]; green += sourceByteArray[sourceOffset + 1]; red += sourceByteArray[sourceOffset + 2]; } } blue = blue / matrixSize; green = green / matrixSize; red = red / matrixSize; if(applyBlue == false) { blue = sourceByteArray[targetOffset]; } if(applyGreen == false) { green = sourceByteArray[targetOffset + 1]; } if(applyRed == false) { red = sourceByteArray[targetOffset + 2]; } if(shiftType == ColorShiftType.None) { targetByteArray[targetOffset ] = (byte)blue; targetByteArray[targetOffset + 1] = (byte)green; targetByteArray[targetOffset + 2] = (byte)red; targetByteArray[targetOffset + 3] = 255; } else if(shiftType == ColorShiftType.ShiftLeft) { targetByteArray[targetOffset ] = (byte)green; targetByteArray[targetOffset + 1] = (byte)red; targetByteArray[targetOffset + 2] = (byte)blue; targetByteArray[targetOffset + 3] = 255; } else if(shiftType == ColorShiftType.ShiftRight) { targetByteArray[targetOffset ] = (byte)red; targetByteArray[targetOffset + 1] = (byte)blue; targetByteArray[targetOffset + 2] = (byte)green; targetByteArray[targetOffset + 3] = 255; } } } 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 |
using System.Drawing; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Bitmap sourceBitmap = BitmapHelper.LoadBitmap("IMAGE\\sample.jpg"); Bitmap targetBitmap = BitmapHelper.ApplyAverageColorFilter ( sourceBitmap, 3, true, true, true, ColorShiftType.ShiftLeft ); this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom; this.pictureBox.Image = targetBitmap; } #endregion } } |