这还没有使用 LockBits,但它足以让你看看它是否能满足你的需求:
public Bitmap getGrayOverlay(Bitmap bmpColor, Bitmap bmpGray)
{
Size s1 = bmpColor.Size;
Size s2 = bmpGray.Size;
if (s1 != s2) return null;
Bitmap bmpResult= new Bitmap(s1.Width, s1.Height);
for (int y = 0; y < s1.Height; y++)
for (int x = 0; x < s1.Width; x++)
{
Color c1 = bmpColor.GetPixel(x, y);
Color c2 = bmpGray.GetPixel(x, y);
bmpResult.SetPixel(x, y, Color.FromArgb((int)(255 * c2.GetBrightness()), c1 ) );
}
return bmpResult;
}
使用完所有三个位图后,请丢弃它们!
这里是LockBit版本,稍微长一点,但基本一样。大约快10倍..
我假设输入文件是 24bppRGB 或 32bppARGB,否则它们返回 null..:
public Bitmap getGrayOverlayLBA(Bitmap bmp1, Bitmap bmp2)
{
Size s1 = bmp1.Size;
Size s2 = bmp2.Size;
if (s1 != s2) return null;
PixelFormat fmt1 = bmp1.PixelFormat;
PixelFormat fmt2 = bmp2.PixelFormat;
PixelFormat fmt = new PixelFormat();
fmt = PixelFormat.Format32bppArgb;
Bitmap bmp3 = new Bitmap(s1.Width, s1.Height, fmt);
Rectangle rect = new Rectangle(0, 0, s1.Width, s1.Height);
BitmapData bmp1Data = bmp1.LockBits(rect, ImageLockMode.ReadOnly, fmt1);
BitmapData bmp2Data = bmp2.LockBits(rect, ImageLockMode.ReadOnly, fmt2);
BitmapData bmp3Data = bmp3.LockBits(rect, ImageLockMode.ReadWrite, fmt);
byte bpp1 = 4;
byte bpp2 = 4;
byte bpp3 = 4;
if (fmt1 == PixelFormat.Format24bppRgb) bpp1 = 3;
else if (fmt1 == PixelFormat.Format32bppArgb) bpp1 = 4; else return null;
if (fmt2 == PixelFormat.Format24bppRgb) bpp2 = 3;
else if (fmt2 == PixelFormat.Format32bppArgb) bpp2 = 4; else return null;
int size1 = bmp1Data.Stride * bmp1Data.Height;
int size2 = bmp2Data.Stride * bmp2Data.Height;
int size3 = bmp3Data.Stride * bmp3Data.Height;
byte[] data1 = new byte[size1];
byte[] data2 = new byte[size2];
byte[] data3 = new byte[size3];
System.Runtime.InteropServices.Marshal.Copy(bmp1Data.Scan0, data1, 0, size1);
System.Runtime.InteropServices.Marshal.Copy(bmp2Data.Scan0, data2, 0, size2);
System.Runtime.InteropServices.Marshal.Copy(bmp3Data.Scan0, data3, 0, size3);
for (int y = 0; y < s1.Height; y++)
{
for (int x = 0; x < s1.Width; x++)
{
int index1 = y * bmp1Data.Stride + x * bpp1;
int index2 = y * bmp2Data.Stride + x * bpp2;
int index3 = y * bmp3Data.Stride + x * bpp3;
Color c1, c2;
if (bpp1 == 4)
c1 = Color.FromArgb(data1[index1 + 3], data1[index1 + 2], data1[index1 + 1], data1[index1 + 0]);
else c1 = Color.FromArgb(255, data1[index1 + 2], data1[index1 + 1], data1[index1 + 0]);
if (bpp2 == 4)
c2 = Color.FromArgb(data2[index2 + 3], data2[index2 + 2], data2[index2 + 1], data2[index2 + 0]);
else c2 = Color.FromArgb(255, data2[index2 + 2], data2[index2 + 1], data2[index2 + 0]);
byte A = (byte)(255 * c2.GetBrightness());
data3[index3 + 0] = c1.B;
data3[index3 + 1] = c1.G;
data3[index3 + 2] = c1.R;
data3[index3 + 3] = A;
}
}
System.Runtime.InteropServices.Marshal.Copy(data3, 0, bmp3Data.Scan0, data3.Length);
bmp1.UnlockBits(bmp1Data);
bmp2.UnlockBits(bmp2Data);
bmp3.UnlockBits(bmp3Data);
return bmp3;
}
编辑:我修改了代码以允许 24bpp 图像作为源。