■ Bitmap 클래스를 사용해 2개의 비트맵에서 차이점을 찾는 방법을 보여준다.
▶ 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 |
using System.Drawing; using System.Drawing.Imaging; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public static class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 차이점 찾기 - FindDifference(sourceBitmap1, sourceBitmap2, matchingColor) /// <summary> /// 차이점 찾기 /// </summary> /// <param name="sourceBitmap1">소스 비트맵 1</param> /// <param name="sourceBitmap2">소스 비트맵 2</param> /// <param name="matchingColor">일치 색상</param> /// <returns>비트맵</returns> public static unsafe Bitmap FindDifference(Bitmap sourceBitmap1, Bitmap sourceBitmap2, Color matchingColor) { if(sourceBitmap1 == null | sourceBitmap2 == null) { return null; } if(sourceBitmap1.Height != sourceBitmap2.Height || sourceBitmap1.Width != sourceBitmap2.Width) { return null; } Bitmap targetBitmap = sourceBitmap2.Clone() as Bitmap; int height = sourceBitmap1.Height; int width = sourceBitmap1.Width; BitmapData sourceBitmapData1 = sourceBitmap1.LockBits ( new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb ); BitmapData sourceBitmapData2 = sourceBitmap2.LockBits ( new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb ); BitmapData targetBitmapData = targetBitmap.LockBits ( new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb ); byte* sourcePointer1 = (byte*)sourceBitmapData1.Scan0; byte* sourcePointer2 = (byte*)sourceBitmapData2.Scan0; byte* targetPointer = (byte*)targetBitmapData.Scan0; byte[] swapArray = new byte[3]; swapArray[0] = matchingColor.B; swapArray[1] = matchingColor.G; swapArray[2] = matchingColor.R; int rowPadding = sourceBitmapData1.Stride - (sourceBitmap1.Width * 3); for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { int same = 0; byte[] temporaryArray = new byte[3]; for(int x = 0; x < 3; x++) { temporaryArray[x] = sourcePointer2[0]; if(sourcePointer1[0] == sourcePointer2[0]) { same++; } sourcePointer1++; sourcePointer2++; } for(int x = 0; x < 3; x++) { targetPointer[0] = (same == 3) ? swapArray[x] : temporaryArray[x]; targetPointer++; } } if(rowPadding > 0) { sourcePointer1 += rowPadding; sourcePointer2 += rowPadding; targetPointer += rowPadding; } } sourceBitmap1.UnlockBits(sourceBitmapData1); sourceBitmap2.UnlockBits(sourceBitmapData2); 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 |
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 sourceBitmap1 = new Bitmap(400, 400); using(Graphics graphics = Graphics.FromImage(sourceBitmap1)) { graphics.FillRectangle(Brushes.Blue, new Rectangle(0 , 0 , 100, 100 )); graphics.FillRectangle(Brushes.Red , new Rectangle(80, 80, 200, 200)); } Bitmap sourceBitmap2 = (Bitmap)sourceBitmap1.Clone(); using(Graphics graphics = Graphics.FromImage(sourceBitmap2)) { graphics.FillRectangle(Brushes.Purple, new Rectangle(0, 0, 80, 80)); } Bitmap targetBitmap = BitmapHelper.FindDifference(sourceBitmap1, sourceBitmap2, Color.Pink); targetBitmap.MakeTransparent(Color.Pink); this.sourcePictureBox1.Image = sourceBitmap1; this.sourcePictureBox2.Image = sourceBitmap2; this.targetPictureBox.Image = targetBitmap; } #endregion } } |