C#中将图像转换为黑白
#region Usings
using System.Drawing;
using System.Drawing.Imaging;
#endregion
namespace Utilities.Media.Image
{
public class ColorMatrix
{
#region Constructor
public ColorMatrix()
{
}
#endregion
#region Properties
public float[][] Matrix { get; set; }
#endregion
#region Public Functions
public Bitmap Apply(Bitmap OriginalImage)
{
using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
{
System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
using (ImageAttributes Attributes = new ImageAttributes())
{
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(OriginalImage,
new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
0, 0, OriginalImage.Width, OriginalImage.Height,
GraphicsUnit.Pixel,
Attributes);
}
}
return NewBitmap;
}
#endregion
}
}
public static Bitmap ConvertBlackAndWhite(Bitmap Image)
{
ColorMatrix TempMatrix = new ColorMatrix();
TempMatrix.Matrix = new float[][]{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
};
return TempMatrix.Apply(Image);
}
float[][] FloatColorMatrix ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
};