用户登录
用户注册

分享至

C#彩色图片灰度化算法实例

  • 作者: 想到你我就硬了唉
  • 来源: 51数据库
  • 2021-11-06

本文实例讲述了c#彩色图片灰度化实现方法。分享给大家供大家参考。具体方法如下:

主要功能代码如下:

复制代码 代码如下:
public static bitmap makegrayscale(bitmap original)

{

    //create a blank bitmap the same size as original

    bitmap newbitmap = new bitmap(original.width, original.height);

    //get a graphics object from the new image

    graphics g = graphics.fromimage(newbitmap);

    //create the grayscale colormatrix

    system.drawing.imaging.colormatrix colormatrix = new system.drawing.imaging.colormatrix(

       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}

      });

    //create some image attributes

    system.drawing.imaging.imageattributes attributes = new system.drawing.imaging.imageattributes();

    //set the color matrix attribute

    attributes.setcolormatrix(colormatrix);

    //draw the original image on the new image

    //using the grayscale color matrix

    g.drawimage(original, new rectangle(0, 0, original.width, original.height), 0, 0, original.width, original.height, graphicsunit.pixel, attributes);

    //dispose the graphics object

    g.dispose();

    return newbitmap;

}

希望本文所述对大家的c#程序设计有所帮助。

软件
前端设计
程序设计
Java相关