How to swap Red and Blue channels on bitmap without using unsafe methods and fast in c#?
Thanks in advance.
Edit: Thanks . Here is the code in c#:
private Bitmap SwapRedAndBlueChannels(Bitmap bitmap)
{
var imageAttr = new ImageAttributes()
imageAttr.SetColorMatrix(new ColorMatrix(
new[]
{
new[] {0.0F, 0.0F, 1.0F, 0.0F, 0.0F},
new[] {0.0F, 1.0F, 0.0F, 0.0F, 0.0F},
new[] {1.0F, 0.0F, 0.0F, 0.0F, 0.0F},
new[] {0.0F, 0.0F, 0.0F, 1.0F, 0.0F},
new[] {0.0F, 0.0F, 0.0F, 0.0F, 1.0F}
}
))
var temp = new Bitmap(bitmap.Width, bitmap.Height)
GraphicsUnit pixel = GraphicsUnit.Pixel
using (Graphics g = Graphics.FromImage(temp))
{
g.DrawImage(bitmap, Rectangle.Round(bitmap.GetBounds(ref pixel)), 0, 0, bitmap.Width, bitmap.Height,
GraphicsUnit.Pixel, imageAttr)
}
return temp
}