当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

 ·adsl上网问题100分在线等    »显示摘要«
    摘要: 执行adsl -setup没有反应啊怎么回事? ......
 ·找不到光驱怎么办    »显示摘要«
    摘要: 装easy cd 看不到光驱了 一个刻露机 一个光驱 原来好使 起机检测不到 ......


问个有难度的题。如何找出一幅图中的一个特定区域

比如:找个红色的区域,函数返回HRGN

NO.1   作者: SatanLi1982

对图片的象素点进行比对(顺序是RGB),24位为(255,0,0)时返回HRGN  
 

NO.2   作者: JennyVenus

这个并不太难,如果你想要的是一个特定的颜色,很容易,比较难的是如何读取类似的颜色,下面的代码是获取一个图片前景rgn的例子。  
   
  针对各种颜色的HBITMAP转化为region。  
   
  HRGN   BitmapToRegion   (HBITMAP   hBmp,   COLORREF   cTransparentColor   =   0,   COLORREF   cTolerance   =   0x101010)  
  {  
          HRGN   hRgn   =   NULL;  
   
          if   (hBmp)  
          {  
                  //   Create   a   memory   DC   inside   which   we   will   scan   the   bitmap   content  
                  HDC   hMemDC   =   CreateCompatibleDC(NULL);  
                  if   (hMemDC)  
                  {  
                          //   Get   bitmap   size  
                          BITMAP   bm;  
                          GetObject(hBmp,   sizeof(bm),   &bm);  
   
                          //   Create   a   32   bits   depth   bitmap   and   select   it   into   the   memory   DC    
                          BITMAPINFOHEADER   RGB32BITSBITMAPINFO   =   {          
                                          sizeof(BITMAPINFOHEADER),         //   biSize    
                                          bm.bmWidth,                                         //   biWidth;    
                                          bm.bmHeight,                                 //   biHeight;    
                                          1,                                                         //   biPlanes;    
                                          32,                                                         //   biBitCount    
                                          BI_RGB,                                                 //   biCompression;    
                                          0,                                                         //   biSizeImage;    
                                          0,                                                         //   biXPelsPerMeter;    
                                          0,                                                         //   biYPelsPerMeter;    
                                          0,                                                         //   biClrUsed;    
                                          0                                                         //   biClrImportant;    
                          };  
                          VOID   *   pbits32;    
                          HBITMAP   hbm32   =   CreateDIBSection(hMemDC,   (BITMAPINFO   *)&RGB32BITSBITMAPINFO,   DIB_RGB_COLORS,   &pbits32,   NULL,   0);  
                          if   (hbm32)  
                          {  
                                  HBITMAP   holdBmp   =   (HBITMAP)SelectObject(hMemDC,   hbm32);  
   
                                  //   Create   a   DC   just   to   copy   the   bitmap   into   the   memory   DC  
                                  HDC   hDC   =   CreateCompatibleDC(hMemDC);  
                                  if   (hDC)  
                                  {  
                                          //   Get   how   many   bytes   per   row   we   have   for   the   bitmap   bits   (rounded   up   to   32   bits)  
                                          BITMAP   bm32;  
                                          GetObject(hbm32,   sizeof(bm32),   &bm32);  
                                          while   (bm32.bmWidthBytes   %   4)  
                                                  bm32.bmWidthBytes++;  
   
                                          //   Copy   the   bitmap   into   the   memory   DC  
                                          HBITMAP   holdBmp   =   (HBITMAP)SelectObject(hDC,   hBmp);  
                                          BitBlt(hMemDC,   0,   0,   bm.bmWidth,   bm.bmHeight,   hDC,   0,   0,   SRCCOPY);  
   
                                          //   For   better   performances,   we   will   use   the   ExtCreateRegion()   function   to   create   the  
                                          //   region.   This   function   take   a   RGNDATA   structure   on   entry.   We   will   add   rectangles   by  
                                          //   amount   of   ALLOC_UNIT   number   in   this   structure.  
                                          #define   ALLOC_UNIT         100  
                                          DWORD   maxRects   =   ALLOC_UNIT;  
                                          HANDLE   hData   =   GlobalAlloc(GMEM_MOVEABLE,   sizeof(RGNDATAHEADER)   +   (sizeof(RECT)   *   maxRects));  
                                          RGNDATA   *pData   =   (RGNDATA   *)GlobalLock(hData);  
                                          pData->rdh.dwSize   =   sizeof(RGNDATAHEADER);  
                                          pData->rdh.iType   =   RDH_RECTANGLES;  
                                          pData->rdh.nCount   =   pData->rdh.nRgnSize   =   0;  
                                          SetRect(&pData->rdh.rcBound,   MAXLONG,   MAXLONG,   0,   0);  
   
                                          //   Keep   on   hand   highest   and   lowest   values   for   the   "transparent"   pixels  
                                          BYTE   lr   =   GetRValue(cTransparentColor);  
                                          BYTE   lg   =   GetGValue(cTransparentColor);  
                                          BYTE   lb   =   GetBValue(cTransparentColor);  
                                          BYTE   hr   =   min(0xff,   lr   +   GetRValue(cTolerance));  
                                          BYTE   hg   =   min(0xff,   lg   +   GetGValue(cTolerance));  
                                          BYTE   hb   =   min(0xff,   lb   +   GetBValue(cTolerance));  
   
                                          //   Scan   each   bitmap   row   from   bottom   to   top   (the   bitmap   is   inverted   vertically)  
                                          BYTE   *p32   =   (BYTE   *)bm32.bmBits   +   (bm32.bmHeight   -   1)   *   bm32.bmWidthBytes;  
                                          for   (int   y   =   0;   y   <   bm.bmHeight;   y++)  
                                          {  
                                                  //   Scan   each   bitmap   pixel   from   left   to   right  
                                                  for   (int   x   =   0;   x   <   bm.bmWidth;   x++)  
                                                  {  
                                                          //   Search   for   a   continuous   range   of   "non   transparent   pixels"  
                                                          int   x0   =   x;  
                                                          LONG   *p   =   (LONG   *)p32   +   x;  
                                                          while   (x   <   bm.bmWidth)  
                                                          {  
                                                                  BYTE   b   =   GetRValue(*p);  
                                                                  if   (b   >=   lr   &&   b   <=   hr)  
                                                                  {  
                                                                          b   =   GetGValue(*p);  
                                                                          if   (b   >=   lg   &&   b   <=   hg)  
                                                                          {  
                                                                                  b   =   GetBValue(*p);  
                                                                                  if   (b   >=   lb   &&   b   <=   hb)  
                                                                                          //   This   pixel   is   "transparent"  
                                                                                          break;  
                                                                          }  
                                                                  }  
                                                                  p++;  
                                                                  x++;  
                                                          }  
 


 ·初任项目经理的同仁来集合。    »显示摘要«
    摘要: 本人软件行业从业四年,去年下半年开始任项目经理,感觉很累。 正在苦思管理方法中,本坛中若有志同道合者,欢迎交流,共同进步 邮件交流。谢谢 join_gu@hotmail.com 顾君彦 ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE