概述
/// <summary>
/// 原始图像buf转C#图像,转出格式均为彩色
/// </summary>
/// <param name="nWidth">图像宽</param>
/// <param name="nHeight">图像高</param>
/// <param name="nBpp">原始图像类型。0=Gray;1=Color DataBuf(RRRGGGBBB); 2=Color DataBuf(RGBRGBRGB)</param>
/// <param name="DataBuf">Buf,注意RGB排列格式。C#:RGBRGBRGB,我的格式:BBBGGGRRR</param>
/// <returns></returns>
public Bitmap GetBitmap(int nWidth, int nHeight, int nBpp, byte[] DataBuf)
{
Bitmap BitmapImage = new Bitmap(nWidth, nHeight, PixelFormat.Format24bppRgb);
BitmapData srcBmpData = BitmapImage.LockBits(new Rectangle(0, 0, BitmapImage.Width, BitmapImage.Height),
ImageLockMode.ReadWrite, BitmapImage.PixelFormat);
switch (nBpp)
{
case 0:
unsafe
{
byte* psrcBuffer = (byte*)srcBmpData.Scan0.ToPointer();
int nCount = srcBmpData.Width * srcBmpData.Height;
int nIndex = 0;
for (int y = 0; y < nCount; y++)
{
psrcBuffer[nIndex++] = DataBuf[y];
psrcBuffer[nIndex++] = DataBuf[y];
psrcBuffer[nIndex++] = DataBuf[y];
}
int k = nIndex;
}
break;
case 1:
unsafe
{
byte* psrcBuffer = (byte*)srcBmpData.Scan0.ToPointer();
int nCount = srcBmpData.Width * srcBmpData.Height;
int nIndex = 0;
// 这个地方注意通道,否则颜色不对,图像变成九宫格。
fixed ( byte* pB = &DataBuf[0])
{
fixed (byte* pG = &DataBuf[nCount])
{
fixed (byte* pR = &DataBuf[nCount*2])
{
for (int y = 0; y < nCount; y++)
{
psrcBuffer[nIndex++] = pR[y];
psrcBuffer[nIndex++] = pG[y];
psrcBuffer[nIndex++] = pB[y];
}
}
}
}
}
break;
case 2:
unsafe
{
byte* psrcBuffer = (byte*)srcBmpData.Scan0.ToPointer();
int nCount = DataBuf.Length;
int nIndex = 0;
for (int y = 0; y < nCount; )
{
psrcBuffer[nIndex++] = DataBuf[y++];
psrcBuffer[nIndex++] = DataBuf[y++];
psrcBuffer[nIndex++] = DataBuf[y++];
}
int k = nIndex;
}
break;
}
BitmapImage.UnlockBits(srcBmpData);
return BitmapImage;
}
后来改了下,增加缩放支持,改了 fix 块,觉得太累赘。
/// <summary>
/// 原始图像buf转C#图像,转出格式均为彩色
/// </summary>
/// <param name="nWidth">图像宽</param>
/// <param name="nHeight">图像高</param>
/// <param name="nBpp">原始图像类型。0=Gray;1=Color</param>
/// <param name="DataBuf">Buf,注意RGB排列格式。C#:RGBRGBRGB,我的格式:BBBGGGRRR</param>
/// <returns></returns>
public Bitmap GetBitmap(int nWidth, int nHeight, int nBpp, byte[] DataBuf, int nCompress = 1)
{
int nNewH = nHeight / nCompress ;
int nNewW = nWidth / nCompress ;
Bitmap BitmapImage = new Bitmap(nNewW, nNewH, PixelFormat.Format24bppRgb);
BitmapData srcBmpData =
BitmapImage.LockBits(
new Rectangle(0, 0, BitmapImage.Width, BitmapImage.Height),
ImageLockMode.ReadWrite, BitmapImage.PixelFormat);
switch (nBpp)
{
case 0:
unsafe
{
byte* psrcBuffer = (byte*)srcBmpData.Scan0.ToPointer();
int nIndex = 0;
for (int i = 0; i < nHeight; i += nCompress )
{
for (int j = 0; j < nWidth; j += nCompress )
{
psrcBuffer[nIndex++] = DataBuf[i * nWidth + j];
psrcBuffer[nIndex++] = DataBuf[i * nWidth + j];
psrcBuffer[nIndex++] = DataBuf[i * nWidth + j];
}
}
}
break;
case 1:
unsafe
{
byte* psrcBuffer = (byte*)srcBmpData.Scan0.ToPointer();
int nCount = nWidth * nHeight;
int nStep = nCount * 2;
int nIndex = 0;
for (int i = 0; i < nHeight; i += nCompress )
{
for (int j = 0; j < nWidth; j += nCompress )
{
psrcBuffer[nIndex++] = DataBuf[i * nWidth + j + nStep];
psrcBuffer[nIndex++] = DataBuf[i * nWidth + j + nCount];
psrcBuffer[nIndex++] = DataBuf[i * nWidth + j];
}
}
}
break;
}
BitmapImage.UnlockBits(srcBmpData);
return BitmapImage;
}
最后
以上就是温婉衬衫为你收集整理的相机采集的图片原始 buffer 转 C# BitmapImage的全部内容,希望文章能够帮你解决相机采集的图片原始 buffer 转 C# BitmapImage所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复