c# 关于找图问题 -------问题代码 求修改 求解 F5 已烂
public Point GetImageContains(Bitmap p_SourceBitmap, Bitmap p_PartBitmap, int p_Float,int pointnums)
{
Point P_res = new Point(-1, -1);
int Height_S = p_SourceBitmap.Height;
int Width_S = p_SourceBitmap.Width;
int Height_P = p_PartBitmap.Height;
int Width_P = p_PartBitmap.Width;
//原图转换成 byte[]
BitmapData _SourceData = p_SourceBitmap.LockBits(new Rectangle(0, 0, p_SourceBitmap.Width, p_SourceBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte[] DataSource = new byte[_SourceData.Stride * p_SourceBitmap.Height];
Marshal.Copy(_SourceData.Scan0, DataSource, 0, DataSource.Length);
p_SourceBitmap.UnlockBits(_SourceData);
//子图转换成 byte[]
BitmapData _PartData = p_PartBitmap.LockBits(new Rectangle(0, 0, p_PartBitmap.Width, p_PartBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte[] DataPart = new byte[_PartData.Stride * p_PartBitmap.Height];
Marshal.Copy(_PartData.Scan0, DataPart, 0, DataPart.Length);
p_PartBitmap.UnlockBits(_PartData);
int index=0;
for (int b = 0; b < Height_S - Height_P; b++)
for (int a = 0; a < Width_S - Width_P; a++)
{
//5点 分别为左上 右上 中间 左下 右下
index = (b * Width_S + a) * 4;
bool b1 = ScanColor(DataSource[index], DataSource[index + 1], DataSource[index + 2], DataPart[0], DataPart[1], DataPart[2], p_Float);
if (!b1) continue;
Point tempp = new Point(a, b);
int p2 = (Width_P - 1) * 4;
b1 = ScanColor(DataSource[index + p2], DataSource[index + 1 + p2], DataSource[index + 2 + p2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
tempp = new Point(a, b + Width_P);
p2 = ((Height_P / 2) * Width_P + (Width_P / 2)) * 4;
int p3 = ((b + (Height_P / 2)) * Width_S + (a + (Width_P / 2))) * 4;
b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
p2 = ((Height_P - 1) * Width_P) * 4;
p3 = ((b + (Height_P - 1)) * Width_S + a) * 4;
b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
p2 = (Height_P * Width_P - 1) * 4;
p3 = ((b + Height_P) * Width_S + (a + Width_P) - 1) * 4;
b1 = ScanColor(DataSource[p3], DataSource[p3 + 1], DataSource[p3 + 2], DataPart[p2], DataPart[p2 + 1], DataPart[p2 + 2], p_Float);
if (!b1) continue;
else
{
//5点匹配 则匹配其余的点
if (ImgDb(index, DataSource, DataPart, p_Float, pointnums))
{
P_res = new Point(a, b);
break;
}
else
P_res = new Point(-1, -1);
}
}
return P_res;
}
private bool ImgDb(int index, byte[] DataImage, byte[] DataPart, int p_Float, int pointnums)
{
int tempint = pointnums;
for (int i = index; i < (DataPart.Length + index); i+=4)
{
bool b3 = ScanColor(DataImage[index], DataImage[index + 1], DataImage[index + 2], DataPart[i - index], DataPart[i - index + 1], DataPart[i - index + 2], p_Float);
if (!b3 && tempint > 0)
{
tempint--;
}
else if (!b3)
return false;
}
return true;
}
private static bool ScanColor(int r1,int g1,int b1,int r2,int g2,int b2, int p_Float)
{
return (r1 <= (r2 + p_Float) && (r1 >= r2 - p_Float)) && ((g1 <= g2 + p_Float) && g1 >= (g2 - p_Float)) && (b1 <= (b2 + p_Float) && (b1 >= b2 - p_Float));
}