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

//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
}

猜你喜欢

转载自blog.csdn.net/qq_23107577/article/details/122490020
今日推荐