C# 图片对比是否一样,Comparing Two picture/images-教学
使用Byte Array做对比,Use Byte Array for picture/image comparison- private bool ImageCompareArray(Bitmap firstImage, Bitmap secondImage)
- {
- bool flag = true;
- string firstPixel;
- string secondPixel;
- if (firstImage.Width == secondImage.Width
- && firstImage.Height == secondImage.Height)
- {
- for (int i = 0; i < firstImage.Width; i++)
- {
- for (int j = 0; j < firstImage.Height; j++)
- {
- firstPixel = firstImage.GetPixel(i, j).ToString();
- secondPixel = secondImage.GetPixel(i, j).ToString();
- if (firstPixel != secondPixel)
- {
- flag = false;
- break;
- }
- }
- }
- if (flag == false)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
复制代码 使用Base 64做对比,Use Base 64 for picture/image comparison- public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
- {
- MemoryStream ms = new MemoryStream();
- firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
- String firstBitmap = Convert.ToBase64String(ms.ToArray());
- ms.Position = 0;
- secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
- String secondBitmap = Convert.ToBase64String(ms.ToArray());
- if (firstBitmap.Equals(secondBitmap))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
复制代码 |
|