Bitmap in Graustufen-BMP umwandeln



  • Hi,
    weiß einer von euch wie ich ne Bitmap (in der Form HBITMAP, also im 32 Bit Farbmodus) in eine Graustufen bzw. Schwarzweiß Bitmap umwandeln kann ?

    N' bissl Code wär ganz hilfreich ...

    Danke schonmal!! 😉

    MfG CodeFinder



  • Hm, ...habs schon...und für die Allgemeinheit 😃 :

    HBITMAP GetGrayedBitmap2(HDC hdcRef, HBITMAP hBmp)
    {
      BITMAPINFO    bmi;
      RGBTRIPLE*    pRgbTrip;
      HBITMAP       hGrayBmp;
      BITMAP        bm;
      BYTE*         pBits;
      BYTE          bLuma;
      HDC           hdc;
      int           x, y, nBytesPerLine;
    
        GetObject((HGDIOBJ)hBmp, sizeof(bm), &bm);
        ZeroMemory(&bmi, sizeof(bmi));
    
        bmi.bmiHeader.biSize        = sizeof(bmi.bmiHeader);
        bmi.bmiHeader.biWidth       = bm.bmWidth;
        bmi.bmiHeader.biHeight      = bm.bmHeight;
        bmi.bmiHeader.biPlanes      = 1;
        bmi.bmiHeader.biBitCount    = 24;
        bmi.bmiHeader.biCompression = BI_RGB;
    
        nBytesPerLine = (((3 * bm.bmWidth) + 3) & ~3);
        pBits = (BYTE*)HeapAlloc(GetProcessHeap(), 0, (nBytesPerLine * bm.bmHeight));
        hdc = CreateCompatibleDC(hdcRef);
        hGrayBmp = (HBITMAP)CopyImage(hBmp, IMAGE_BITMAP, 
                                      bm.bmWidth, bm.bmHeight, 0);
        GetDIBits(hdc, hBmp, 0, bm.bmHeight, pBits, &bmi, DIB_RGB_COLORS);
    
        for(y = 0; y < bm.bmHeight; ++y)
        {
            for(x = 0, pRgbTrip = (RGBTRIPLE*)(pBits + y * nBytesPerLine); 
                                   x < bm.bmWidth; ++x, ++pRgbTrip)
            {
                bLuma = LOBYTE(((pRgbTrip->rgbtRed * 30) + (pRgbTrip->rgbtGreen * 59) + 
                                (pRgbTrip->rgbtBlue * 11)) / 100);
                pRgbTrip->rgbtRed = pRgbTrip->rgbtGreen = pRgbTrip->rgbtBlue = bLuma;
            }
        }
    
        SetDIBits(hdc, hGrayBmp, 0, bm.bmHeight, pBits, &bmi, DIB_RGB_COLORS);
        DeleteDC(hdc);
        HeapFree(GetProcessHeap(), 0, pBits);
    
      return(hGrayBmp);
    }
    

    MfG CodeFinder





  • hmm hab eigtnl. gesucht...aber meine Methode ist schneller...aber trotzdem danke°!!° weißt du vll wie ich das innen s/w Bild umbaue, eigntl. doch alle Pixel != RGB(255,255,255) müssen schwarz oder ? (scheiß Satzbau 😃 ...egal 😃 )

    MfG CodeFinder


Anmelden zum Antworten