Bildanalyse unter fullscreen



  • Hallo, ich habe folgendes Problem, ich muss für ein eigenbau project die durchschnittsfarbe des aktuellenbildes bestimmen.
    Das funktionierd unter Desktop modus auch ganz gut.
    Sobald ein Spiel in den Vollbildmodus geht ist allerdings ende im gelände.
    Dann liefert die funktion immer schwarz zurück,

    RGB ScreenAverageColor()
    {
        int     Width  = GetSystemMetrics(SM_CXSCREEN);
        int     Height = GetSystemMetrics(SM_CYSCREEN);
    
        HDC hdcScreen;
        HBITMAP hbmScreen;
    
        //---------------Bitmap Informationen
        BITMAPINFO infobmp;
        infobmp.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        infobmp.bmiHeader.biWidth = Width;
        infobmp.bmiHeader.biHeight = Height;
        infobmp.bmiHeader.biPlanes = 1;
        infobmp.bmiHeader.biBitCount = 24;
        infobmp.bmiHeader.biCompression = 0;
        infobmp.bmiHeader.biSizeImage = 0;
        infobmp.bmiHeader.biXPelsPerMeter = 0;
        infobmp.bmiHeader.biYPelsPerMeter = 0;
        infobmp.bmiHeader.biClrUsed = 0;
        infobmp.bmiHeader.biClrImportant = 0;
    
        BYTE* bitmap = new BYTE[Width*Height*3];
    
        BITMAPFILEHEADER bfheader;
    
        bfheader.bfType = 19778;
        bfheader.bfSize = sizeof(BITMAPFILEHEADER) + Width*Height*3 + sizeof(BITMAPINFOHEADER);
        bfheader.bfReserved1 = 0;
        bfheader.bfReserved2 = 0;
        bfheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
        //Bitmap -----------------------      Informationen
    
        hdcScreen = GetWindowDC(NULL);
        hbmScreen = CreateCompatibleBitmap(hdcScreen, Width, Height);
    
        // temporärer DC
        HDC hdcTemp = CreateCompatibleDC(hdcScreen);
    
        // Bitmap reinselektieren
        HBITMAP hbmOld = (HBITMAP)SelectObject(hdcTemp, hbmScreen);
    
        // Inhalt von Desktop übertragen
        BitBlt(hdcTemp, 0, 0, Width, Height, hdcScreen, 0, 0, SRCCOPY);
    
        GetDIBits(hdcTemp, hbmScreen, 0, Height, bitmap, &infobmp, DIB_RGB_COLORS);
    
        // aufräumen
        SelectObject(hdcTemp, hbmOld);
        DeleteObject(hbmScreen);
        DeleteDC(hdcTemp);
    
        //HANDLE hfile = CreateFile("C:/neu.bmp", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0);
    
        //Datei Schreiben
        //DWORD word;
        //WriteFile(hfile, &bfheader, 14, &word, NULL);
        //WriteFile(hfile, &infobmp, 40,& word, NULL);
        //WriteFile(hfile, bitmap, Width*Height*3, &word,NULL);
    
        ReleaseDC(NULL, hdcScreen);
        //CloseHandle(hfile);
        //BYTE* bitmapBits=(BYTE*)infobmp+sizeof(infobmp->bmiHeader);
        if (infobmp.bmiHeader.biCompression==BI_BITFIELDS)bitmap+=12;
    
        const int w=infobmp.bmiHeader.biWidth;
        const int h=infobmp.bmiHeader.biHeight;
        const bool topdownBitmap=h<0;
        const int bytesPerPixel=infobmp.bmiHeader.biBitCount/8;
        const int scanLineLength=(topdownBitmap? -1 : 1)*((w*bytesPerPixel+3)/4*4);
        const int firstLineOffset=topdownBitmap? 0 : ((h-1)*scanLineLength);
        unsigned long long global_r=0;
        unsigned long long global_g=0;
        unsigned long long global_b=0;
        long long dividor=0;
        for(int x=0; x<Width; x++)
        {
            for(int y=0; y<Height; y++)
            {
                const int pixelOffset=firstLineOffset-y*scanLineLength+x*bytesPerPixel;
    
                BYTE r=bitmap[pixelOffset+2];
                BYTE g=bitmap[pixelOffset+1];
                BYTE b=bitmap[pixelOffset];
                global_r+=r;
                global_g+=g;
                global_b+=b;
                dividor+=1;
                //cout<<r<<"/"<<g<<"/"<<b<<endl;
            }
        }
        cout<<"G RED:"<<global_r<<endl;
        cout<<"G GREEN:"<<global_g<<endl;
        cout<<"G BLUE:"<<global_b<<endl;
        cout<<"dividor:"<<dividor<<endl;
        RGB returncolor;
        returncolor.Blue=global_b/dividor;
        returncolor.Red=global_r/dividor;
        returncolor.Green=global_g/dividor;
        cout<<"DIV RGB:"<<global_r/dividor<<"/"<<global_g/dividor<<"/"<<global_b/dividor<<endl;
        delete[] bitmap;
        return returncolor;
    }
    

    Ich hab gelese das es über den Framebuffer möglich sein soll an die benötigten Daten ranzukommen, allerdings hab ich nicht die geringste idee wie.

    MFG Thalhammer


Anmelden zum Antworten