您的位置:首页 > 编程语言 > C#

C#由数组生成灰度位图或者彩色位图

2015-12-17 19:32 417 查看




如果有一组图像数据保存在一维数组中,如下所示,图像数据保存在一维数组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);
}
}



生成的灰度图像,在窗体上显示如下,由于图像比较小,所以在绘制时长宽都放大至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);

}
}


生成的灰度图像,在窗体上显示如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: