using
System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
public class MyClass
{
public static void Main()
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap("F:/1寸.jpg");
System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();
float[][] grayMatrix = new float[][]{
new float[]{0.299f, 0.299f, 0.299f, 0, 0},
new float[]{0.587f, 0.587f, 0.587f, 0, 0},
new float[]{0.114f, 0.114f, 0.114f, 0, 0},
new float[]{0, 0, 0, 1, 0},
new float[]{0, 0, 0, 0, 1}
};
ColorMatrix cm = new ColorMatrix(grayMatrix);
ia.SetColorMatrix(cm);
Bitmap Target = new Bitmap(bm.Width,bm.Height);
WL("开始转换!");
using (Graphics grap = Graphics.FromImage(Target)) {
grap.DrawImage(bm, new Rectangle(0, 0, bm.Width, bm.Height),
0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel, ia);
}
WL("装换完毕!");
Target.Save("F:/黑白一寸.jpg",ImageFormat.Jpeg);
RL();
}
Helper methods
}
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
public class MyClass
{
public static void Main()
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap("F:/1寸.jpg");
System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();
float[][] grayMatrix = new float[][]{
new float[]{0.299f, 0.299f, 0.299f, 0, 0},
new float[]{0.587f, 0.587f, 0.587f, 0, 0},
new float[]{0.114f, 0.114f, 0.114f, 0, 0},
new float[]{0, 0, 0, 1, 0},
new float[]{0, 0, 0, 0, 1}
};
ColorMatrix cm = new ColorMatrix(grayMatrix);
ia.SetColorMatrix(cm);
Bitmap Target = new Bitmap(bm.Width,bm.Height);
WL("开始转换!");
using (Graphics grap = Graphics.FromImage(Target)) {
grap.DrawImage(bm, new Rectangle(0, 0, bm.Width, bm.Height),
0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel, ia);
}
WL("装换完毕!");
Target.Save("F:/黑白一寸.jpg",ImageFormat.Jpeg);
RL();
}
Helper methods
}