110,567
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获取经容差计算后的最终图,参考色彩点为源图坐标(1,1)
/// </summary>
/// <param name="SourceBitmap">源图</param>
/// <param name="Tolerance">容差</param>
/// <returns></returns>
/// <remarks></remarks>
private Bitmap GetFinalBitmap_Forli_007(Bitmap SourceBitmap, int Tolerance)
{
try {
if (_tmpB != null)
_tmpB.Dispose();
//释放旧临时图内存
Color SourceColor = SourceBitmap.GetPixel(1, 1);
_tmpB = new Bitmap(SourceBitmap);
System.Drawing.Imaging.BitmapData bmpDATA = new System.Drawing.Imaging.BitmapData();
bmpDATA = _tmpB.LockBits(new Rectangle(0, 0, _tmpB.Width, _tmpB.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb);
byte[] BTS = new byte[bmpDATA.Stride * bmpDATA.Height + 1];
System.Runtime.InteropServices.Marshal.Copy(bmpDATA.Scan0, BTS, 0, BTS.Length - 1);
for (int I = 0; I <= BTS.Length - 4; I += 4) {
if (IsNearValue(BTS[I], BTS[I + 1], BTS[I + 2], SourceColor, Tolerance) == true) {
BTS[I + 3] = 0;
}
}
System.Runtime.InteropServices.Marshal.Copy(BTS, 0, bmpDATA.Scan0, BTS.Length - 1);
_tmpB.UnlockBits(bmpDATA);
return _tmpB;
} catch {
return null;
}
}
private bool IsNearValue(int B, int G, int R, Color Cr, int Tol)
{
try {
if (Math.Max(Cr.B, B) - Math.Min(Cr.B, B) <= Tol && Math.Max(Cr.G, G) - Math.Min(Cr.G, G) <= Tol && Math.Max(Cr.R, R) - Math.Min(Cr.R, R) <= Tol) {
return true;
} else {
return false;
}
} catch {
return false;
}
}