C#由指定数据生成灰度位图或者彩色位图


 

如果有一组图像数据保存在一维数组中,如下所示,图像数据保存在一维数组ImageData中,现在想要这组数据创建一幅灰度图像或者彩色图像,那该如何创建呢?不急,请往下看。

           //图像数据
            byte[] ImageData = new byte[8 * 3 * 20]{
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0
            };


1. 用ImageData创建一幅灰度图像,代码如下(StartDraw_Click为工程中按钮的点击事件):

        private void StartDraw_Click(object sender, EventArgs e)
        {
            //图像数据
            byte[] ImageData = new byte[8 * 3 * 20]{
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0
            };

          //确保图像的宽度是4的倍数
            int imageWidth  = 24;
          int imageHeight = 20;

          //数组转换为Intptr
          IntPtr pData = Marshal.AllocHGlobal(imageWidth * imageHeight);
          Marshal.Copy(ImageData, 0, pData, imageWidth * imageHeight);

          //创建灰度位图
            Bitmap bmp = new Bitmap(imageWidth, imageHeight, imageWidth, PixelFormat.Format8bppIndexed, pData);

            //修改位图的调色板
              ColorPalette palette = bmp.Palette;
            for (int i = 0; i < 256; i++)
            {
                palette.Entries[i] = Color.FromArgb(i, i, i);
            }
            bmp.Palette = palette;


            //显示
            Graphics dc = pictureBox1.CreateGraphics();
          dc.Clear(Color.White);
          dc.DrawImage(bmp, 0, 0, imageWidth * 10, imageHeight * 10);
        }
    }

代码中,用创建位图时用的是Bitmap的“Bitmap(int width, int height, int stride, PixelFormat format, IntPtr scan0)”构造函数。该构造函数可以用指定的大小、指定的图像格式和指定的数据初始化Bitmap类。

其中:

width表示位图的宽度;

height表示位图的高度;

stribe表示指定相邻扫描行开始处之间字节偏移量的整数。这通常是以像素的位数乘以位图的宽度。传递给此参数的值必须为4 的倍数。;

format值为Format8bppIndexed,表示创建每像素为8位的索引图像,颜色表中有256中颜色;

scan0为包含像素数据的指针。

由于Format8bppIndexed表示创建的是索引图像,所以我们需要修改调色板,为图像中每一位数据提供索引。


生成的灰度图像,在窗体上显示如下,由于图像比较小,所以在绘制时长宽都放大至10倍:

 

2. 用ImageData创建一幅彩色位图图像,代码如下:

        private void StartDraw_Click(object sender, EventArgs e)
        {
            //图像数据
            byte[] ImageData = new byte[8 * 3 * 20]{
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0, 
                100,100,0, 0,0,0, 100,0,100, 0,0,0, 0,100,100, 0,0,0, 100,100,100, 0,0,0
            };

            //确保图像的宽度是4的倍数
              int imageWidth   = 8;
            int imageHeight  = 20;

            //数组转换为Intptr
            IntPtr pData = Marshal.AllocHGlobal(imageWidth * imageHeight * 3);
            Marshal.Copy(ImageData, 0, pData, imageWidth * imageHeight * 3);
            Bitmap bmp = new Bitmap(imageWidth, imageHeight, imageWidth * 3, PixelFormat.Format24bppRgb, pData);

            //显示,长宽各拉伸至10倍显示
              Graphics dc = pictureBox1.CreateGraphics();
            dc.Clear(Color.White);
            dc.DrawImage(bmp, 0, 0, imageWidth * 10, imageHeight * 10);
            
        }
    }

创建24位彩色图像时,由于每个像素的三个分量R、G、B已经表示了当前像素的颜色,所以不需要调色板。


生成的灰度图像,放大显示如下:

 

 


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值