Skip to content

Instantly share code, notes, and snippets.

@Jiiks
Last active May 25, 2017 05:43
Show Gist options
  • Save Jiiks/c42cbc86b198c9947fe4d956549229d5 to your computer and use it in GitHub Desktop.
Save Jiiks/c42cbc86b198c9947fe4d956549229d5 to your computer and use it in GitHub Desktop.
C# Bitmap multiply colour blend
public static Bitmap Multiply(this Bitmap bitmap, byte r, byte g, byte b, PixelFormat format = PixelFormat.Format32bppArgb) {
var size = new Rectangle(0,0, bitmap.Width, bitmap.Height);
var bitmapData = bitmap.LockBits(size, ImageLockMode.ReadOnly, format);
var buffer = new byte[bitmapData.Stride * bitmapData.Height];
Marshal.Copy(bitmapData.Scan0, buffer, 0, buffer.Length);
bitmap.UnlockBits(bitmapData);
byte Calc(byte c1, byte c2) {
var cr = c1 / 255d * c2 / 255d * 255d;
return (byte) (cr > 255 ? 255 : cr);
}
for (var i = 0; i < buffer.Length ; i += 4) {
buffer[i] = Calc(buffer[i], b);
buffer[i + 1] = Calc(buffer[i + 1], g);
buffer[i + 2] = Calc(buffer[i + 2], r);
}
var result = new Bitmap(bitmap.Width, bitmap.Height);
var resultData = result.LockBits(size, ImageLockMode.WriteOnly, format);
Marshal.Copy(buffer, 0, resultData.Scan0, buffer.Length);
result.UnlockBits(resultData);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment