截取两张图片相同的地方生成的图片为什么还是不相同
川娃子 2017-05-10 04:31:18 我在做图片对比的时候发现这种情况:
首先截取两张时间不一样的桌面截屏(两张图只有右下角的时间分钟数不一样)
代码:
private void ScreenCapture(string imagePath)
{
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
using (Image bitmap = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bitmap.Size);
}
bitmap.Save(imagePath, ImageFormat.Jpeg);
}
GC.Collect();
}
然后截取这两张图片中除任务栏(排除掉时间不相同的部分)外的相同部分生成新的图片
代码:
public void CaptureImage(string fromFilePath, string toFilePath)
{
Image fromImage = Image.FromFile(sFromFilePath);
Bitmap bitmap = new Bitmap(fromImage.Width, fromImage.Height - 38);
Graphics graphic = Graphics.FromImage(bitmap);
graphic.DrawImage(fromImage, 0, 0, new Rectangle(0, 0, fromImage.Width, fromImage.Height - 38), GraphicsUnit.Point);
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
saveImage.Save(toFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
saveImage.Dispose();
bitmap.Dispose();
graphic.Dispose();
}
最后用提取文件MD5码值来判断文件是否相同
代码:
public string MD5Verify(string filePath)
{
string reslute = string.Empty;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] b = md5.ComputeHash(fs);
for (int i = 0; i < b.Length; i++)
{
reslute += b[i].ToString("x2");
}
fs.Close();
md5.Clear();
}
return reslute;
}
生成的图片看着是一样的了,大小也一样,但MD5码值不一样,用记事本打开查看到最后面的字符串的确不一样。
我想知道问什么不一样,麻烦解释一下,还有就是这么才能一样?