我正在尝试一种方法,该方法应该返回字节数组(rgb 值)中两个图像之间的差异并通过 UDP 发送。代码:(发送此数组无关紧要,因为现在无关紧要)
public void getdiff(Bitmap lol,Bitmap lol2)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Rectangle rect = new Rectangle(0, 0, lol.Width, lol.Height);
BitmapData bmpData = lol.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr ptr = bmpData.Scan0;
Rectangle rect2 = new Rectangle(0, 0, lol2.Width, lol2.Height);
BitmapData bmpData2 = lol2.LockBits(rect2, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
IntPtr ptr2 = bmpData2.Scan0;
int numBytes = bmpData.Stride * lol.Height;
byte[] rgbValues = new byte[numBytes];
int numBytes2 = bmpData2.Stride * lol2.Height;
byte[] rgbValues2 = new byte[numBytes2];
byte[] difference = new byte[numBytes];
Marshal.Copy(ptr, rgbValues, 0, numBytes);
Marshal.Copy(ptr2, rgbValues2, 0, numBytes2);
for (int counter = 0; counter < rgbValues.Length; counter++)
{
if (rgbValues[counter] != rgbValues2[counter])
{
difference[counter] = rgbValues[counter];
}
}
Marshal.Copy(rgbValues, 0, ptr, numBytes);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
textBox1.Text = ts.Milliseconds.ToString();
lol.UnlockBits(bmpData);
lol2.UnlockBits(bmpData2);
}
现在我有来自 2 个图像的 2 个字节数组,我只是比较它们,当图像不同时,我从所选图像写入足够的 RGB 值到差异 []。问题是,当两个图像中的 RGB 值相等时,此差异数组中的适当位置将填充 0(三个 0 表示白色)。所以问题是,如果我将这种差异[]“强加”到目标图像上,它可能主要是白色的。我真的很想使用 Marshal.Copy 和 Lockbits,因为它真的很有效。
问题是:如何存储此图像差异以避免在读取/强加它时乘以“for”循环?也许我错过了一些方法?我想继续使用 LockBits 和 Marshal.Copy,但如果您有更好的想法 - 请与我分享。