我要将一张图片的一部分颜色处理一下
试了很多次老是不成功
希望大家能给我贴一段代码上来让我参考一下
不要用GetPixel和SetPixel太慢了
谢谢。

解决方案 »

  1.   

    哪一部分?
    如果是矩形,FillRect();如果要做锐化之类的,不要用GetPixel和SetPixel,直接用数据块的指针。
      

  2.   

    锐化之类的吧我用GetDIBits获得了位图信息
    执行 for (int i = 0; i<255; i++)
    {  
    bmpInfo.bmiColors[i].rgbReserved=0;
    bmpInfo.bmiColors[i].rgbBlue=i;
    bmpInfo.bmiColors[i].rgbGreen=i;
    bmpInfo.bmiColors[i].rgbRed=i; }
    后原来的BITMAPINFO数据都不对了
    是不是要分配内存之类的????
    这个bmiColors默认大小怎么只有一??
      

  3.   

    不对,
    int GetDIBits(
      HDC hdc,           // handle to DC
      HBITMAP hbmp,      // handle to bitmap
      UINT uStartScan,   // first scan line to set
      UINT cScanLines,   // number of scan lines to copy
      LPVOID lpvBits,    // array for bitmap bits
      LPBITMAPINFO lpbi, // bitmap data buffer
      UINT uUsage        // RGB or palette index
    );
    要的是lpvBits,这才图像真正的数据,根据大小for()循环处理比如 LPBYTE lp = lpvBits;for(int i= 0; i< lpbi.bmiHeader.biWidth *lpbi.bmiHeader.biHeight,i++) // biSizeImage也可以,但有些情况为0
    {
         if( *(lp+ i) > 127)
         {
             *(lp+ i) = 255;
         }
         else
         {
             *(lp+ i) = 0;
          }
    }如果是彩色32的,i+ = 4,分量做分别处理;
    如果是彩色32的,i+ = 3,分量做分别处理;
      

  4.   

    看看以下demo,dc为32位//得到数据:
    void AlphaGetBlock(CDC* pDC,UINT x,UINT y,UINT cx,UINT cy,COLORREF *color)
    {
    //cx*bits/8必须被4整除 CDC hmemdc;
     CBitmap hbitmap;
     UINT bits;
     bits=pDC->GetDeviceCaps(BITSPIXEL);   if(cx*bits/8%4)AfxMessageBox(L"cx*bits/8%4 有余数"); hbitmap.CreateBitmap(cx,cy,1,bits,NULL);
     hmemdc.CreateCompatibleDC(pDC);
     hmemdc.SelectObject(&hbitmap);
     hmemdc.BitBlt(0,0,cx,cy,pDC,x,y,SRCCOPY);
     hbitmap.GetBitmapBits(cx*cy*bits/8,color);
     hmemdc.DeleteDC();
     hbitmap.DeleteObject();
    }
    //处理完了,再绘回DC
    void AlphaCopyBlock(CDC* pDC,UINT x,UINT y,UINT cx,UINT cy,COLORREF *color)
    {
     CDC hmemdc;
     CBitmap hCurrentBitmap,*hOldBitmap;
     hmemdc.CreateCompatibleDC(pDC);
     hCurrentBitmap.CreateBitmap(cx,cy,1,32,color);
     hmemdc.SelectObject(&hCurrentBitmap);
     pDC->BitBlt(x,y,cx,cy,&hmemdc,0,0,SRCCOPY);
     hmemdc.SelectObject(&hOldBitmap);
     hmemdc.DeleteDC();
     hCurrentBitmap.DeleteObject();
    }