如下图:这幅图片里透明色设置为RGB(255,0,255),黑色区域便是我想要得到的rgn;目前我也实现了,方法如下:CRgn rgn;
rgn.CreateRectRgn(0,0,0,0);for (int y = 0, int iBeginX = 0; y < bmpHeight; y++)
{
  for (int x = 0; x < bmWidth; x++)
   {
     CRgn rgnTemp;
      
     if (pDC->GetPixel(x, y) == clr)
          iBeginX = x;     else
         rgnTemp.CreateRectRgn(iBeginX, y, x, y+1);     rgn.CombineRgn(&rgn, &rgnTemp, RGN_OR);
     
     rgnTemp.DeleteObject();
   }
}循环结束后,rgn便是除去透明区域以外的了,但是这个做法有个问题,图片稍微大一点就不实用了。各位达人有其它好的方法吗?“取得一幅透明图片除去透明(透明色区域)以外的rgn”

解决方案 »

  1.   

    “不实用”是怎么个不实用法?
    不要用GetPixel,速度很慢,比直接存取内存像素位起码慢上几十倍。
      

  2.   

    使用位图数据扫描吧,不用GetPixel方法。使用GetDIBits,得到位图数据,然后对比。
      

  3.   

    HRGN BitmapToRegion(HBITMAP hBmp, COLORREF cTransparentColor, COLORREF cTolerance)
    {
    HRGN hRgn = NULL;
    if (hBmp)
    {
    HDC hMemDC = CreateCompatibleDC(NULL);
    if (hMemDC)
    {
    BITMAP bm;
    GetObject(hBmp, sizeof(bm), &bm);
    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);
    HDC hDC = CreateCompatibleDC(hMemDC);
    if (hDC)
    {
    BITMAP bm32;
    GetObject(hbm32, sizeof(bm32), &bm32);
    while (bm32.bmWidthBytes % 4)
    bm32.bmWidthBytes++;
    HBITMAP holdBmp = (HBITMAP)SelectObject(hDC, hBmp);
    BitBlt(hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);
    #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);
    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));
    BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
    for (int y = 0; y < bm.bmHeight; y++)
    {
    for (int x = 0; x < bm.bmWidth; x++)
    {
    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)
    break;
    }
    }
    p++;
    x++;
    } if (x > x0)
    {
    if (pData->rdh.nCount >= maxRects)
    {
    GlobalUnlock(hData);
    maxRects += ALLOC_UNIT;
    hData = GlobalReAlloc(hData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
    pData = (RGNDATA *)GlobalLock(hData);
    }
    RECT *pr = (RECT *)&pData->Buffer;
    SetRect(&pr[pData->rdh.nCount], x0, y, x, y+1);
    if (x0 < pData->rdh.rcBound.left)
    pData->rdh.rcBound.left = x0;
    if (y < pData->rdh.rcBound.top)
    pData->rdh.rcBound.top = y;
    if (x > pData->rdh.rcBound.right)
    pData->rdh.rcBound.right = x;
    if (y+1 > pData->rdh.rcBound.bottom)
    pData->rdh.rcBound.bottom = y+1;
    pData->rdh.nCount++;
    if (pData->rdh.nCount == 2000)
    {
    HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
    if (hRgn)
    {
    CombineRgn(hRgn, hRgn, h, RGN_OR);
    DeleteObject(h);
    }
    else
    hRgn = h;
    pData->rdh.nCount = 0;
    SetRect(&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
    }
    }
    }
    p32 -= bm32.bmWidthBytes;
    }
    HRGN h = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
    if (hRgn)
    {
    CombineRgn(hRgn, hRgn, h, RGN_OR);
    DeleteObject(h);
    }
    else
    hRgn = h;
    GlobalFree(hData);
    SelectObject(hDC, holdBmp);
    DeleteDC(hDC);
    }
    DeleteObject(SelectObject(hMemDC, holdBmp));
    }
    DeleteDC(hMemDC);
    }
    }
    return hRgn;
    }