海康工业相机像素格式转换
public Bitmap getBitmapFromByteStream(byte[] imgByte, int imgH, int imgW, int channel)
{
Bitmap bitmap = null;
if (channel == 3)
{
bitmap = new Bitmap(imgW, imgH, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}
else if (channel == 1)
{
bitmap = new Bitmap(imgW, imgH, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
}
else if (channel == 4)
{
bitmap = new Bitmap(imgW, imgH, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
else
{
return bitmap;
}
BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, imgW, imgH), ImageLockMode.WriteOnly, bitmap.PixelFormat);
int stride = bitmapData.Stride;
int offset = stride - imgW * channel;
IntPtr iptr = bitmapData.Scan0;
int scanbytes = stride * imgH;
int posScan = 0, posReal = 0;
byte[] pixelvalues = new byte[scanbytes];
if (0 != offset)
{
for (int x = 0; x < imgH; x++)
{
for (int y = 0; y < imgW * channel; y++)
{
pixelvalues[posScan++] = imgByte[posReal++];
}
posScan += offset;
}
Marshal.Copy(pixelvalues, 0, iptr, scanbytes);
}
else
{
Marshal.Copy(imgByte, 0, iptr, scanbytes);
}
bitmap.UnlockBits(bitmapData);
if (channel == 1)
{
var pal = bitmap.Palette;
for (int j = 0; j < 256; j++)
pal.Entries[j] = System.Drawing.Color.FromArgb(j, j, j);
bitmap.Palette = pal;
}
return bitmap;
}
public void ReceiveThreadProcess()
{
MyCamera.MV_FRAME_OUT stFrameOut = new MyCamera.MV_FRAME_OUT();
IntPtr pBufForConvert = IntPtr.Zero;
while (m_bGrabbing)
{
int nRet = hik.MV_CC_GetImageBuffer_NET(ref stFrameOut, 1000);
if (nRet == MyCamera.MV_OK)
{
MyCamera.MvGvspPixelType enType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined;
uint nChannelNum = 0;
if (IsColorPixelFormat(stFrameOut.stFrameInfo.enPixelType))
{
enType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
nChannelNum = 3;
}
else if (IsMonoPixelFormat(stFrameOut.stFrameInfo.enPixelType))
{
enType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
nChannelNum = 1;
}
else
{
Console.WriteLine("Don't need to convert!");
}
if (enType != MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined)
{
if (pBufForConvert == IntPtr.Zero)
{
pBufForConvert = Marshal.AllocHGlobal((int)(stFrameOut.stFrameInfo.nWidth * stFrameOut.stFrameInfo.nHeight * nChannelNum));
}
MyCamera.MV_PIXEL_CONVERT_PARAM stConvertPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
stConvertPixelParam.nWidth = stFrameOut.stFrameInfo.nWidth;
stConvertPixelParam.nHeight = stFrameOut.stFrameInfo.nHeight;
stConvertPixelParam.pSrcData = stFrameOut.pBufAddr;
stConvertPixelParam.nSrcDataLen = stFrameOut.stFrameInfo.nFrameLen;
stConvertPixelParam.enSrcPixelType = stFrameOut.stFrameInfo.enPixelType;
stConvertPixelParam.enDstPixelType = enType;
stConvertPixelParam.pDstBuffer = pBufForConvert;
stConvertPixelParam.nDstBufferSize = (uint)(stFrameOut.stFrameInfo.nWidth * stFrameOut.stFrameInfo.nHeight * nChannelNum);
nRet = hik.MV_CC_ConvertPixelType_NET(ref stConvertPixelParam);
if (MyCamera.MV_OK != nRet)
{
Console.WriteLine("Convert pixel type Failed:{0:x8}", nRet);
break;
}
byte[] data = new byte[stConvertPixelParam.nDstLen];
Marshal.Copy(stConvertPixelParam.pDstBuffer, data, 0, (int)stConvertPixelParam.nDstLen);
FileStream pFile = null;
Bitmap image2 = getBitmapFromByteStream(data, stConvertPixelParam.nHeight, stConvertPixelParam.nWidth, 3);
DateTimeOffset currentTime = DateTimeOffset.Now;
string time_now_ymd = currentTime.Year.ToString() + "_" + currentTime.Month.ToString() + "_" + currentTime.Day.ToString();
string time_now_hms = currentTime.Hour.ToString() + "_" + currentTime.Minute.ToString() + "_" + currentTime.Second.ToString();
string time_now = time_now_ymd + "_" + time_now_hms;
image2.Save(Application.StartupPath + "saveimg\\" + time_now + ".bmp");
}
}
}
}