zoukankan      html  css  js  c++  java
  • C# 图像处理(二)—— 黑白效果

     今天就说一下怎样把一幅图片做成黑白图片的效果,就是把图片黑白化。之前打开图片的方法已经发表了,具体地址是:

    C# 图像处理(一)

       再面对一幅要处理成黑白效果的图片,我为此写了以下一个函数,

        /// <summary>
        /// 将图片转为为黑白图片
        /// </summary>
        /// <param name="mybt">要进行处理的图片</param>
        /// <param name="width">图片的长度</param>
        /// <param name="height">图片的高度</param>
        /// <returns>已经被处理后的黑白图片</returns>
        public Bitmap BWPic(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);//初始化一个Bitmap对象,用来记录处理后的图片
            int x, y, result;//x,y是循环次数,result是记录处理后的像素值
            Color pixel;

            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
                    result = (pixel.R + pixel.G + pixel.B) / 3;//取红绿蓝三色的平均值

                    //绘图,把处理后的值赋值到刚才定义的bm对象里面
                    bm.SetPixel(x, y, Color.FromArgb(result, result, result));
                }
            }

            return bm;//返回黑白图片
        }

        最后利用一个pictureBox控件把处理后的图片呈现出来,利用的是下面这个语句,

        int width = this.pictureBox.Width;//图片容器的长度
        int height = this.pictureBox.Height;//图片容器的宽度
        this.pictureBox.Image = pix.BWPic((Bitmap)this.pictureBox.Image, width, height);//处理图片

  • 相关阅读:
    javascript实战演练,制作新按钮,‘新窗口打开网站’,点击打开新窗
    P1332 血色先锋队
    P4643 [国家集训队]阿狸和桃子的游戏
    T149876 公约数
    P1462 通往奥格瑞玛的道路
    P1083 借教室
    Tribles UVA
    Fence Repair POJ
    Crossing Rivers
    关于一轮
  • 原文地址:https://www.cnblogs.com/luluping/p/2585553.html
Copyright © 2011-2022 走看看