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);
}
}
生成的灰度图像,在窗体上显示如下:
相关文章推荐
- C# WinForm的练习
- 关于C#操作WPS和office兼容性的问题
- C#中Monitor和Lock以及区别
- C# - 引用类型、Class、Interface、泛型
- visual studio开发C#笔记02——颜色选择对话框ColorDialog
- C#笔试精简
- C#接口
- 二叉堆(C#)
- U3D C#脚本的生命周期
- .net平台 .net Framework 组织结构 .net Framework类库 CLR C# 介绍
- 【转】CSharp中的多线程——线程同步基础
- c#自定义控件
- C# eval()函数浅谈
- C#数字格式化
- C#中Socket占用CPU高,解决方案!
- c#监听用户输入调取系统键盘
- C#打开Adobe Reader进程打开pdf并传递页码参数跳转到指定页数
- c#securityexception不允许所请求的注册表访问权
- C#扩展方法实现 byte[] 输出为HEX字符串形式
- C#中判断字符串相等的方法