CSharp 彩色图像转为灰度图像

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,  00},
                                                
new float[]{0.587f0.587f0.587f00},
                                                
new float[]{0.114f0.114f0.114f00},
                                                
new float[]{00010},
                                                
new float[]{00001}
                                            }
;
                                            
                                            
        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(00, bm.Width, bm.Height),
                
00, bm.Width, bm.Height,
                GraphicsUnit.Pixel, ia);
        }

        
        WL(
"装换完毕!");
        Target.Save(
"F:/黑白一寸.jpg",ImageFormat.Jpeg);
        RL();
    }

    
    
Helper methods
}
发布了38 篇原创文章 · 获赞 4 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/tomatozq/article/details/1892963