.net有什么比较图片的类库马 有大量图片,因为有各种格式,所以一律转成Image对象处理。想要找出其中相同的图片有什么类库马。另外可能存在同一个图片尺寸不一致的情况,因此要有一定的误差容错。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static int BitmapCompare(Bitmap bitmap1, Bitmap bitmap2) { int result = 0; if (bitmap1 == null || bitmap2 == null) return -1; if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height) { for (int i = 0; i < bitmap1.Width; i++) { for (int j = 0; j < bitmap1.Height; j++) { Color color1 = bitmap1.GetPixel(i, j); Color color2 = bitmap2.GetPixel(i, j); if (color1 != color2) { result = color1.ToArgb() - color2.ToArgb(); break; } } if (result != 0) break; } } else if (bitmap1.Width != bitmap2.Width) { result = bitmap1.Width - bitmap2.Width; } else if (bitmap1.Height != bitmap2.Height) { result = bitmap1.Height - bitmap2.Height; } return result; } public static int BitmapCompare2(Bitmap bitmap1, Bitmap bitmap2) { int result = 0; if (bitmap1 == null || bitmap2 == null) return -1; if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height) { BitmapData bmd1 = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat); BitmapData bmd2 = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat); int bytes = bmd1.Stride * bitmap1.Height; byte[] buff1 = new byte[bytes]; byte[] buff2 = new byte[bytes]; Marshal.Copy(bmd1.Scan0, buff1, 0, Marshal.SizeOf(typeof(byte)) * bytes); Marshal.Copy(bmd2.Scan0, buff2, 0, Marshal.SizeOf(typeof(byte)) * bytes); result = MemoryCompare(buff1, buff2); bitmap1.UnlockBits(bmd1); bitmap2.UnlockBits(bmd2); } else if (bitmap1.Width != bitmap2.Width) { result = bitmap1.Width - bitmap2.Width; } else if (bitmap1.Height != bitmap2.Height) { result = bitmap1.Height - bitmap2.Height; } return result; } public static int BitmapCompare3(Bitmap bitmap1, Bitmap bitmap2) { int result = 0; if (bitmap1 == null || bitmap2 == null) return -1; if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height) { BitmapData bmd1 = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat); BitmapData bmd2 = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat); IntPtr start1 = bmd1.Scan0; IntPtr start2 = bmd2.Scan0; int sizeOfByte = Marshal.SizeOf(typeof(byte)); for (int i = 0; i < sizeOfByte * bmd1.Stride * bitmap1.Height; i++) { byte b1 = Marshal.ReadByte(start1, i); byte b2 = Marshal.ReadByte(start2, i); if (b1 != b2) { result = (int)(b1 - b2); break; } } bitmap1.UnlockBits(bmd1); bitmap2.UnlockBits(bmd2); } else if (bitmap1.Width != bitmap2.Width) { result = bitmap1.Width - bitmap2.Width; } else if (bitmap1.Height != bitmap2.Height) { result = bitmap1.Height - bitmap2.Height; } return result; }参考 usercontrol.load时更改datagridview样式无效? 如何引用一个组件的父容器对象(Form对象)? 关于邮件发送的问题 控件改变parent的问题 组合问题 override的奇怪错误!!!!!!!!!! 新手 请教各位大大一个问题 关于ListBox问题 关于 DateSet 控件,急!望指教! 怎么实现类似vb中的collection对象? 在button_click事件中怎么启动一个新线程 求visual studio 2005 里面的测试教程
{
int result = 0;
if (bitmap1 == null || bitmap2 == null)
return -1;
if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height)
{
for (int i = 0; i < bitmap1.Width; i++)
{
for (int j = 0; j < bitmap1.Height; j++)
{
Color color1 = bitmap1.GetPixel(i, j);
Color color2 = bitmap2.GetPixel(i, j);
if (color1 != color2)
{
result = color1.ToArgb() - color2.ToArgb();
break;
}
}
if (result != 0)
break;
}
}
else if (bitmap1.Width != bitmap2.Width)
{
result = bitmap1.Width - bitmap2.Width;
}
else if (bitmap1.Height != bitmap2.Height)
{
result = bitmap1.Height - bitmap2.Height;
}
return result;
}
public static int BitmapCompare2(Bitmap bitmap1, Bitmap bitmap2)
{
int result = 0;
if (bitmap1 == null || bitmap2 == null)
return -1;
if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height)
{
BitmapData bmd1 = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat);
BitmapData bmd2 = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat);
int bytes = bmd1.Stride * bitmap1.Height;
byte[] buff1 = new byte[bytes];
byte[] buff2 = new byte[bytes];
Marshal.Copy(bmd1.Scan0, buff1, 0, Marshal.SizeOf(typeof(byte)) * bytes);
Marshal.Copy(bmd2.Scan0, buff2, 0, Marshal.SizeOf(typeof(byte)) * bytes);
result = MemoryCompare(buff1, buff2);
bitmap1.UnlockBits(bmd1);
bitmap2.UnlockBits(bmd2);
}
else if (bitmap1.Width != bitmap2.Width)
{
result = bitmap1.Width - bitmap2.Width;
}
else if (bitmap1.Height != bitmap2.Height)
{
result = bitmap1.Height - bitmap2.Height;
}
return result;
}
public static int BitmapCompare3(Bitmap bitmap1, Bitmap bitmap2)
{
int result = 0;
if (bitmap1 == null || bitmap2 == null)
return -1;
if (bitmap1.Width == bitmap2.Width && bitmap1.Height == bitmap2.Height)
{
BitmapData bmd1 = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat);
BitmapData bmd2 = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat);
IntPtr start1 = bmd1.Scan0;
IntPtr start2 = bmd2.Scan0;
int sizeOfByte = Marshal.SizeOf(typeof(byte));
for (int i = 0; i < sizeOfByte * bmd1.Stride * bitmap1.Height; i++)
{
byte b1 = Marshal.ReadByte(start1, i);
byte b2 = Marshal.ReadByte(start2, i);
if (b1 != b2)
{
result = (int)(b1 - b2);
break;
}
}
bitmap1.UnlockBits(bmd1);
bitmap2.UnlockBits(bmd2);
}
else if (bitmap1.Width != bitmap2.Width)
{
result = bitmap1.Width - bitmap2.Width;
}
else if (bitmap1.Height != bitmap2.Height)
{
result = bitmap1.Height - bitmap2.Height;
}
return result;
}
参考