打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
DDB转为DIB
 
把DDB转换成DIB 
  设备相关位图(DDB)显示方式是尽可能与显示设备驱动程序相匹配,这样,DDB不可能与其他显示设备兼容;而设备无关位图(DIB)能与所有显示设备兼容,但是,其缺点在于显示速度比较慢。 
   
  我们需要把DDB转换为DIB的一种情况是,需要将位图保存到一个文件中。下面是其实现的代码。 
   
//  DDBToDIB  -  Creates  a  DIB  from  a  DDB 
//  bitmap  -  Device  dependent  bitmap(DDB) 
//  pPal   -  Logical  palette
/*
HANDLE  DDBToDIB(CBitmap&  bitmap, DWORD  dwCompression, CPalette*  pPal)   

 BITMAP bm; 
 BITMAPINFOHEADER bi; 
 LPBITMAPINFOHEADER lpbi; 
 DWORD dwLen;  //DIB长度
 HANDLE hDIB; 
 HANDLE handle; 
 HDC hDC; 
 HPALETTE hPal; 
   
 ASSERT(bitmap.GetSafeHandle()); 
   
 //  The  function  has  no  arg  for  bitfields
 if(  dwCompression  ==  BI_BITFIELDS  ) 
    {
        return  NULL; 
    }
   
 //  If  a  palette  has  not  been  supplied  use  defaul  palette 
 hPal  =  (HPALETTE)  pPal->GetSafeHandle(); 
 if  (hPal==NULL)
 {
  hPal  =  (HPALETTE)  GetStockObject(DEFAULT_PALETTE); 
 }
   
 //  Get  bitmap  information 
 bitmap.GetObject(sizeof(bm),(LPSTR)&bm); 
   
 //  Initialize  the  bitmapinfoheader 
 bi.biSize  =  sizeof(BITMAPINFOHEADER); 
 bi.biWidth  =  bm.bmWidth; 
 bi.biHeight    =  bm.bmHeight; 
 bi.biPlanes    =  1; 
 bi.biBitCount  =  bm.bmPlanes  *  bm.bmBitsPixel; 
 bi.biCompression =  dwCompression; 
 bi.biSizeImage  =  0; 
 bi.biXPelsPerMeter =  0; 
 bi.biYPelsPerMeter =  0; 
 bi.biClrUsed  =  0; 
 bi.biClrImportant =  0; 
   
 //  Compute  the  size  of  the    infoheader  and  the  color  table 
 int  nColors  =  (1  <<  bi.biBitCount); 
 if(  nColors>  256  )
 {   
        nColors  =  0; 
 }
 dwLen    =  bi.biSize  +  nColors  *  sizeof(RGBQUAD); 
   
 //  We  need  a  device  context  to  get  the  DIB  from 
 hDC  =  GetDC(NULL); 
 hPal  =  SelectPalette(hDC,hPal,FALSE); 
 RealizePalette(hDC); 
   
 //  Allocate  enough  memory  to  hold  bitmapinfoheader  and  color  table 
 hDIB  =  GlobalAlloc(GMEM_FIXED,dwLen); 
   
 if (!hDIB)//如果内存申请失败
 { 
  SelectPalette(hDC,hPal,FALSE); 
  ReleaseDC(NULL,hDC); 
  return  NULL; 
 } 
   
 lpbi  =  (LPBITMAPINFOHEADER)hDIB;  //使用hDIB申请的内存
   
 *lpbi  =  bi; 
   
 //  Call  GetDIBits  with  a  NULL  lpBits  param,  so  the  device  driver   
 //  will  calculate  the  biSizeImage  field   
 GetDIBits(hDC,  (HBITMAP)bitmap.GetSafeHandle(),  0L,  (DWORD)bi.biHeight, 
     (LPBYTE)NULL,  (LPBITMAPINFO)lpbi,  (DWORD)DIB_RGB_COLORS); 
 //GetDIBits函数用来计算所需的内存空间的大小和得到DIB参数
  
 bi  =  *lpbi; 
   
 //  If  the  driver  did  not  fill  in  the  biSizeImage  field,  then  compute  it 
 //  Each  scan  line  of  the  image  is  aligned  on  a  DWORD  (32bit)  boundary 
 if (bi.biSizeImage  ==  0)
 { 
  bi.biSizeImage  =  ((((bi.biWidth  *  bi.biBitCount)  +  31)  &  ~31)  /  8)  *  bi.biHeight;
 }
   
    //  If  a  compression  scheme  is  used  the  result  may  infact  be  larger 
    //  Increase  the  size  to  account  for  this. 
    if (dwCompression  !=  BI_RGB)
 {
  bi.biSizeImage  =  (bi.biSizeImage  *  3)  /  2; 
 } 
   
 //  Realloc  the  buffer  so  that  it  can  hold  all  the  bits 
 dwLen  +=  bi.biSizeImage; 
 if  (handle  =  GlobalReAlloc(hDIB,  dwLen,  GMEM_MOVEABLE)) 
 {
  hDIB  =  handle; 
 }
 else
 { 
  GlobalFree(hDIB); 
   
  //  Reselect  the  original  palette 
  SelectPalette(hDC,hPal,FALSE); 
  ReleaseDC(NULL,hDC); 
  return  NULL; 
 } 
   
 //  Get  the  bitmap  bits  获取内存
 lpbi  =  (LPBITMAPINFOHEADER)hDIB; 
   
 //  FINALLY  get  the  DIB 
 BOOL  bGotBits  =  GetDIBits(  hDC,  (HBITMAP)bitmap.GetSafeHandle(), 
                                   0L,    //  Start  scan  line 
                                   (DWORD)bi.biHeight,  //  #  of  scan  lines 
                                   (LPBYTE)lpbi     //  address  for  bitmap  bits 
                                    +  (bi.biSize  +  nColors  *  sizeof(RGBQUAD)), 
                                   (LPBITMAPINFO)lpbi,  //  address  of  bitmapinfo 
                                   (DWORD)
                                   DIB_RGB_COLORS  );  //  Use  RGB  for  color  table 
   
 if(  !bGotBits  ) 
 {   
  GlobalFree(hDIB); 
     
  SelectPalette(hDC,hPal,FALSE); 
  ReleaseDC(NULL,hDC); 
  return  NULL; 
 } 
   
 SelectPalette(hDC,hPal,FALSE); 
 ReleaseDC(NULL,hDC); 
 return  hDIB; 
}
2006-8-21
今天又写了一个DDB转DIB的函数,它是一个象素一个象素点的读取的,相对较慢!而且只支持24位的!
HANDLE CProcess::DDBtoDIB(CBitmap &bitmap, CDC *pDC)
{
 LPBITMAPINFO lpbmi;
 DWORD dwHeader;
 DWORD dwLen;
 HANDLE hDIB;
 LPSTR pDIB;
 CDC memDC;
 dwHeader = m_image->GetHeaderSize();
 dwLen = dwHeader + m_image->GetImageSize();//获取文件大小
 
 memDC.CreateCompatibleDC(pDC);
 memDC.SelectObject(bitmap);
 pDIB = (LPSTR)new char[dwLen];//该内存在SaveLayer()函数中释放
 if (pDIB == NULL)//如果内存申请失败
 { 
  return  NULL; 
 } 
   
 lpbmi  =  (LPBITMAPINFO)pDIB;  //使用pDIB申请的内存
 int width = m_image->GetWidth();
 int height = m_image->GetHeight();
 if (m_image->m_lpbmi->bmiHeader.biBitCount < 24)
 {
  m_image->ComputePaletteSize(24);
  if (m_image->m_lpbmi != NULL)
  {
   m_image->m_lpbmi = NULL;
   delete m_image->m_lpbmi;
  }
  m_image->m_lpbmi = (LPBITMAPINFO)new char[sizeof(BITMAPINFOHEADER)];
  if (NULL == m_image->m_lpbmi)
  {
   AfxMessageBox("m_lpbmi内存申请失败");
  }
  m_image->m_lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  m_image->m_lpbmi->bmiHeader.biWidth = width;
  m_image->m_lpbmi->bmiHeader.biHeight = height;
  m_image->m_lpbmi->bmiHeader.biPlanes = 1;
  m_image->m_lpbmi->bmiHeader.biBitCount = 24;
  m_image->m_lpbmi->bmiHeader.biCompression = BI_RGB;
  m_image->m_lpbmi->bmiHeader.biSizeImage = 0;
  m_image->m_lpbmi->bmiHeader.biXPelsPerMeter = 0;
  m_image->m_lpbmi->bmiHeader.biXPelsPerMeter = 0;
  m_image->m_lpbmi->bmiHeader.biClrUsed = 0;
  m_image->m_lpbmi->bmiHeader.biClrImportant = 0;
  m_image->ComputeImage();//重新计算图像大小
  m_image->m_lpbmi->bmiHeader.biSizeImage = m_image->GetImageSize();
 }
 memcpy(lpbmi, m_image->m_lpbmi, dwHeader);//获取信息头
 
 int x = 0 ,y = 0;
 DWORD i = 0;
 COLORREF color;
 for (y = height - 1; y >= 0; y--)
 {
    for (x = 0; x < width; x++)
    {
    i = (height - 1 - y)*width + x;
    color = memDC.GetPixel(x, y);
    pDIB[dwHeader + i*3] = (BYTE)((color&0x00ff0000)>>16);
    pDIB[dwHeader + i*3 +1] = (BYTE)((color&0x0000ff00)>>8);
    pDIB[dwHeader + i*3 +2] = (BYTE)(color&0x000000ff);
   }
 }
 hDIB = (HANDLE)pDIB;
 return hDIB;
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gophychan/archive/2008/10/16/3086450.aspx
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
根据指定窗口句柄进行截屏
DibImage.cpp
保存MFC的CBitmap对象中的图象到一个BMP文件中
vc 保存Bitmap到文件
windows中使用CreateDIBSection创建bitmap
窗口截图(二)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服