cngreatwall

赫赫盛唐艺轩

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

HRESULT CVideoSource::SaveToBmpFile(char* szFile)
{
 HRESULT hr =NULL;
 
 TRY
 {
  BYTE *buffer = NULL;
  hr = m_pWindowsLessControl->GetCurrentImage(&buffer);
  if (!SUCCEEDED(hr))
  {    
   return hr;
  }
 
  BITMAPFILEHEADER hdr;    
  BITMAPINFOHEADER *lpbi = (BITMAPINFOHEADER *)buffer;
  //颜色空间转换,如果为32位位图,转换为24
  bool isSize24 = false;
  DWORD dwSize24 = 0;
  DWORD dwSize32 = lpbi->biSizeImage;
  dwSize24 = lpbi->biBitCount==32?(dwSize32*3)/4:dwSize32;   //RGB32与RGB24的像素点空间只差了一个字节
  BYTE* pImg24 = new BYTE[dwSize24]; //存放RGB24存储空间
  BYTE* pImg24Temp = pImg24;
  BYTE* pImg32 = buffer + sizeof(BITMAPINFOHEADER);
  if (lpbi->biBitCount==32)
  {
   isSize24 = true;
   for (DWORD index=0; index<dwSize32/4; index++)
   {
    unsigned char r = *(pImg32++);
    unsigned char g = *(pImg32++);
    unsigned char b = *(pImg32++);
    pImg32++;    //跳过alpha分量,实现转换
    *(pImg24++) = r;
    *(pImg24++) = g;
    *(pImg24++) = b;   //赋值
   }

   lpbi->biBitCount = 24;
  }
  int nColors = 1 << lpbi->biBitCount;
  if (nColors > 256)        
   nColors = 0;     
  hdr.bfType = ((WORD)('M' << 8) | 'B'); // always is "BM"    
  hdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nColors * sizeof(RGBQUAD) + lpbi->biSizeImage;   
  hdr.bfReserved1 = 0;    
  hdr.bfReserved2 = 0;    
  hdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + lpbi->biSize + nColors * sizeof(RGBQUAD));

  //将图像调整为640*480,非这种格式,强制调整
  
  

  FILE *bmpFile = NULL;    
  bmpFile = fopen((LPCTSTR)szFile, "wb");//inFile为文件名    
  if (bmpFile != NULL)    
  {        
   fwrite(&hdr, 1, sizeof(BITMAPFILEHEADER), bmpFile);//位图文件头        
   fwrite(buffer, 1, sizeof(BITMAPINFOHEADER), bmpFile);//位图信息头
   if (!isSize24)
   {
    fwrite(buffer+sizeof(BITMAPINFOHEADER), 1, nColors * sizeof(RGBQUAD), bmpFile);//颜色表        
    fwrite(buffer+sizeof(BITMAPINFOHEADER)+nColors * sizeof(RGBQUAD), 1, lpbi->biSizeImage, bmpFile);//位图数据    
   }
   else
   {
    fwrite(pImg24Temp,1,dwSize24,bmpFile);
    //fwrite(pImgNormal,1,700*500*3,bmpFile);
   }
  }    
  fclose(bmpFile);  
  
  
  
  CoTaskMemFree(buffer);//释放资源
  delete pImg24Temp;
  return hr;
 }
 CATCH (CMemoryException, e)
 {
  
 }
 END_CATCH
 }

posted on 2011-09-02 11:42  cngreatwall  阅读(405)  评论(0编辑  收藏  举报