工业相机图像转Bitmap数据(C#)

本文介绍了一种将不同类型的图像数据转换为Bitmap的方法,包括黑白图像和彩色图像的处理过程。对于黑白图像,通过设置调色板来实现转换;对于彩色图像,则涉及颜色通道的交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//pImage 图像数据的指针首地址,不要填错!!!
if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)//黑白图像格式
{
	//************************Mono8 转 Bitmap*******************************
	Bitmap bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);

	ColorPalette cp = bmp.Palette;
	// init palette
	for (int i = 0; i < 256; i++)
	{
		cp.Entries[i] = Color.FromArgb(i, i, i);
	}
	// set palette back
	bmp.Palette = cp;
	bmp.Save("image.bmp", ImageFormat.Bmp);//存图验证是否ok
}
else//彩色RGB或者BGR格式
{
	//*********************RGB8 转 Bitmap**************************
	//RGB与BGR的转换,如果原始数据就是BGR了,也可以省去这个循环,避免浪费时间
	for (int i = 0; i < stFrameInfo.nHeight; i++)
	{
		for (int j = 0; j < stFrameInfo.nWidth; j++)
		{
			byte chRed = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3];
			m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3] = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2];
			m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
		}
	}
	Bitmap bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
	bmp.Save("image.bmp", ImageFormat.Bmp);//存图验证是否ok
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值