实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace datuzhaoxiaotu
{
class FindSPic
{
/// <summary>
/// 在大图里找小图
/// </summary>
/// <param name="S_bmp">大图</param>
/// <param name="P_bmp">小图</param>
/// <param name="similar">容错值 取值0--255,数值越高效率越低,不建议超过50</param>
/// <returns></returns>
public static List<Point> FindPic(int left, int top, int width, int height, Bitmap S_bmp, Bitmap P_bmp, int similar)
{
if (S_bmp.PixelFormat != PixelFormat.Format24bppRgb) { throw new Exception("颜色格式只支持24位bmp"); }
if (P_bmp.PixelFormat != PixelFormat.Format24bppRgb) { throw new Exception("颜色格式只支持24位bmp"); }
int S_Width = S_bmp.Width;
int S_Height = S_bmp.Height;
int P_Width = P_bmp.Width;
int P_Height = P_bmp.Height;
//取出4个角的颜色
int px1 = P_bmp.GetPixel(0, 0).ToArgb(); //左上角
int px2 = P_bmp.GetPixel(P_Width - 1, 0).ToArgb(); //右上角
int px3 = P_bmp.GetPixel(0, P_Height - 1).ToArgb(); //左下角
int px4 = P_bmp.GetPixel(P_Width - 1, P_Height - 1).ToArgb(); //右下角
Color BackColor = P_bmp.GetPixel(0, 0); //背景色
BitmapData S_Data = S_bmp.LockBits(new Rectangle(0, 0, S_Width, S_Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData P_Data = P_bmp.LockBits(new Rectangle(0, 0, P_Width, P_Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
List<Point> List;
if (px1 == px2 && px1 == px3 && px1 == px4) //如果4个角的颜色相同
{
//透明找图
List = _FindPic(left, top, width, height, S_Data, P_Data, GetPixelData(P_Data, BackColor), similar);
}
else if (similar > 0)
{
//相似找图
List = _FindPic(left, top, width, height, S_Data, P_Data, similar);
}
else
{
//全匹配找图效率最高
List = _FindPic(left, top, width, height, S_Data, P_Data);
}
S_bmp.UnlockBits(S_Data);
P_bmp.UnlockBits(P_Data);
return List;
}
/// <summary>
/// 全匹配找图
/// </summary>
/// <param name="S_Data">大图数据</param>
/// <param name="P_Data">小图数据</param>
/// <returns></returns>
private static unsafe List<Point> _FindPic(int left, int top, int width, int height, BitmapData S_Data, BitmapData P_Data)
{
List<Point> List = new List<Point>();
int S_stride = S_Data.Stride;
int P_stride = P_Data.Stride;
IntPtr S_Iptr = S_Data.Scan0;
IntPtr P_Iptr = P_Data.Scan0;
byte* S_ptr;
byte* P_ptr;
bool IsOk = false;
int _BreakW = width - P_Data.Width 1;
int _BreakH = height - P_Data.Height 1;
for (int h = top; h < _BreakH; h )
{
for (int w = left; w < _BreakW; w )
{
P_ptr = (byte*)(P_Iptr);
for (int y = 0; y < P_Data.Height; y )
{
for (int x = 0; x < P_Data.Width; x )
{
S_ptr = (byte*)((int)S_Iptr S_stride * (h y) (w x) * 3);
P_ptr = (byte*)((int)P_Iptr P_stride * y x * 3);
if (S_ptr[0] == P_ptr[0] && S_ptr[1] == P_ptr[1] && S_ptr[2] == P_ptr[2])
{
IsOk = true;
}
else
{
IsOk = false; break;
}
}
if (IsOk == false) { break; }
}
if (IsOk) { List.Add(new Point(w, h)); }
IsOk = false;
}
}
return List;
}
/// <summary>
/// 相似找图
/// </summary>
/// <param name="S_Data">大图数据</param>
/// <param name="P_Data">小图数据</param>
/// <param name="similar">误差值</param>
/// <returns></returns>
private static unsafe List<Point> _FindPic(int left, int top, int width, int height, BitmapData S_Data, BitmapData P_Data, int similar)
{
List<Point> List = new List<Point>();
int S_stride = S_Data.Stride;
int P_stride = P_Data.Stride;
IntPtr S_Iptr = S_Data.Scan0;
IntPtr P_Iptr = P_Data.Scan0;
byte* S_ptr;
byte* P_ptr;
bool IsOk = false;
int _BreakW = width - P_Data.Width 1;
int _BreakH = height - P_Data.Height 1;
for (int h = top; h < _BreakH; h )
{
for (int w = left; w < _BreakW; w )
{
P_ptr = (byte*)(P_Iptr);
for (int y = 0; y < P_Data.Height; y )
{
for (int x = 0; x < P_Data.Width; x )
{
S_ptr = (byte*)((int)S_Iptr S_stride * (h y) (w x) * 3);
P_ptr = (byte*)((int)P_Iptr P_stride * y x * 3);
if (ScanColor(S_ptr[0], S_ptr[1], S_ptr[2], P_ptr[0], P_ptr[1], P_ptr[2], similar)) //比较颜色
{
IsOk = true;
}
else
{
IsOk = false; break;
}
}
if (IsOk == false) { break; }
}
if (IsOk) { List.Add(new Point(w, h)); }
IsOk = false;
}
}
return List;
}
/// <summary>
/// 透明找图
/// </summary>
/// <param name="S_Data">大图数据</param>
/// <param name="P_Data">小图数据</param>
/// <param name="PixelData">小图中需要比较的像素数据</param>
/// <param name="similar">误差值</param>
/// <returns></returns>
private static unsafe List<Point> _FindPic(int left, int top, int width, int height, BitmapData S_Data, BitmapData P_Data, int[,] PixelData, int similar)
{
List<Point> List = new List<Point>();
int Len = PixelData.GetLength(0);
int S_stride = S_Data.Stride;
int P_stride = P_Data.Stride;
IntPtr S_Iptr = S_Data.Scan0;
IntPtr P_Iptr = P_Data.Scan0;
byte* S_ptr;
byte* P_ptr;
bool IsOk = false;
int _BreakW = width - P_Data.Width 1;
int _BreakH = height - P_Data.Height 1;
for (int h = top; h < _BreakH; h )
{
for (int w = left; w < _BreakW; w )
{
for (int i = 0; i < Len; i )
{
S_ptr = (byte*)((int)S_Iptr S_stride * (h PixelData[i, 1]) (w PixelData[i, 0]) * 3);
P_ptr = (byte*)((int)P_Iptr P_stride * PixelData[i, 1] PixelData[i, 0] * 3);
if (ScanColor(S_ptr[0], S_ptr[1], S_ptr[2], P_ptr[0], P_ptr[1], P_ptr[2], similar)) //比较颜色
{
IsOk = true;
}
else
{
IsOk = false; break;
}
}
if (IsOk) { List.Add(new Point(w, h)); }
IsOk = false;
}
}
return List;
}
#region FindColor
/// <summary>
/// 找色
/// </summary>
public static unsafe List<Point> FindColor(int left, int top, int width, int height, Bitmap S_bmp, Color clr, int similar)
{
if (S_bmp.PixelFormat != PixelFormat.Format24bppRgb) { throw new Exception("颜色格式只支持24位bmp"); }
BitmapData S_Data = S_bmp.LockBits(new Rectangle(0, 0, S_bmp.Width, S_bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr _Iptr = S_Data.Scan0;
byte* _ptr;
List<Point> List = new List<Point>();
for (int y = top; y < height; y )
{
for (int x = left; x < width; x )
{
_ptr = (byte*)((int)_Iptr S_Data.Stride * (y) (x) * 3);
if (ScanColor(_ptr[0], _ptr[1], _ptr[2], clr.B, clr.G, clr.R, similar))
{
List.Add(new Point(x, y));
}
}
}
S_bmp.UnlockBits(S_Data);
return List;
}
#endregion
#region IsColor
/// <summary>
/// 比较两个 Color
/// </summary>
/// <param name="similar">容错值</param>
/// <returns></returns>
public static bool IsColor(Color clr1, Color clr2, int similar = 0)
{
if (ScanColor(clr1.B, clr1.G, clr1.R, clr2.B, clr2.G, clr2.R, similar))
{
return true;
}
return false;
}
#endregion
#region CopyScreen
/// <summary>
/// 屏幕截图
/// </summary>
/// <param name="rect"></param>
/// <returns></returns>
public static Bitmap CopyScreen(Rectangle rect)
{
Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size);
g.Dispose();
}
System.GC.Collect();
return bitmap;
}
#endregion
[DllImport("gdi32.dll")]
public static extern IntPtr DeleteObject(IntPtr hObject);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, // driver name驱动名
string lpszDevice, // device name设备名
string lpszOutput, // not used; should be NULL
IntPtr lpInitData // optional printer data
);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC目标设备的句柄
int nXDest, // x-coord of destination upper-left corner目标对象的左上角的X坐标
int nYDest, // y-coord of destination upper-left corner目标对象的左上角的Y坐标
int nWidth, // width of destination rectangle目标对象的矩形宽度
int nHeight, // height of destination rectangle目标对象的矩形长度
IntPtr hdcSrc, // handle to source DC源设备的句柄
int nXSrc, // x-coordinate of source upper-left corner源对象的左上角的X坐标
int nYSrc, // y-coordinate of source upper-left corner源对象的左上角的Y坐标
UInt32 dwRop // raster operation code光栅的操作值
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc // handle to DC
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, // handle to DC
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels
);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, // handle to DC
IntPtr hgdiobj // handle to object
);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(
IntPtr hdc // handle to DC
);
[DllImport("user32.dll")]
public static extern bool PrintWindow(
IntPtr hwnd, // Window to copy,Handle to the window that will be copied.
IntPtr hdcBlt, // HDC to print into,Handle to the device context.
UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values.
);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(
IntPtr hwnd
);
#region 私有方法
private static unsafe int[,] GetPixelData(BitmapData P_Data, Color BackColor)
{
byte B = BackColor.B, G = BackColor.G, R = BackColor.R;
int Width = P_Data.Width, Height = P_Data.Height;
int P_stride = P_Data.Stride;
IntPtr P_Iptr = P_Data.Scan0;
byte* P_ptr;
int[,] PixelData = new int[Width * Height, 2];
int i = 0;
for (int y = 0; y < Height; y )
{
for (int x = 0; x < Width; x )
{
P_ptr = (byte*)((int)P_Iptr P_stride * y x * 3);
if (B == P_ptr[0] & G == P_ptr[1] & R == P_ptr[2])
{
}
else
{
PixelData[i, 0] = x;
PixelData[i, 1] = y;
i ;
}
}
}
int[,] PixelData2 = new int[i, 2];
Array.Copy(PixelData, PixelData2, i * 2);
return PixelData2;
}
//找图BGR比较
private static unsafe bool ScanColor(byte b1, byte g1, byte r1, byte b2, byte g2, byte r2, int similar)
{
if ((Math.Abs(b1 - b2)) > similar) { return false; } //B
if ((Math.Abs(g1 - g2)) > similar) { return false; } //G
if ((Math.Abs(r1 - r2)) > similar) { return false; } //R
return true;
}
#endregion
}
}
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论