首页 > 解决方案 > 是否有使用 C# 比较两个 PNG 图像的一部分而不是完整图像的功能?

问题描述

我在 Visual Studio 2019 IDE 上使用 .NET Framework 来编写用于比较两个图像的算法。

虽然如果我比较整个 png 比较工作正常,但问题是作为主副本的图片(与新生成的图像进行比较)是过去拍摄的,左上角有较旧日期和时间的时间戳,因此生成的最新图片总是会生成失败结果,即图片不匹配,因为图片上时间戳的时间差异。目前我正在使用以下内容,它适用于相同的时间戳图像

System.Drawing
System.Drawing.Imaging
Bitmap original_image = (Bitmap)Bitmap.FromFile(imageFile);
Bitmap test_image = (Bitmap)Bitmap.FromFile(testimageFile);
if(object.Equals(original_image,test_image)
return true
BitmpaData bitmapDataMasterPicture = original_image.LockBits(new Rectangle(0,0,original_image.Width,original_image.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
BitmpaData bitmapDataTestPicture = test_image.LockBits(new Rectangle(0,0,test_image.Width,test_image.Height),ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);

for(int i = 0; i<totalBytes-1 ;i++)
if(bitmapDataMasterPicture[i] != bitmapDataTestPicture[i])
{
return false;
}
original_image.UnlockBits(bitmapDataMasterPicture);
test_image.UnlockBits(bitmapDataTestPicture);

是否有一个函数/算法来比较两个 png 图像的一部分而不是使用 C# 来比较完整?那就是跳过左上角的时间戳。

标签: c#algorithmbitmappng

解决方案


You could crop the image or draw a solid rectangle covering the timestamp.

Graphics oGraphics= Graphics.FromImage(original_image);
Rectangle rect = new Rectangle(0, 0, 200, 200);
oGraphics.DrawRectangle(new Pen(Color.Black, 3), rect);

推荐阅读