1

私はファイルからピクセルデータを読み取るために他の多くの手法を使用しましたが、GDIを使用してみるのは良い考えのように思えました。非画面DCのドキュメントは少し曖昧なので、私はストローを把握しています。

これが私が今持っているものです、そしてそれはすべてのピクセルが範囲外であると言います(「x」を印刷します)。

#include <windows.h>
#include <iostream>

using namespace std;

#define filename "test.bmp"


int main()
{
    HBITMAP hBmp;
    hBmp = (HBITMAP)LoadImage(NULL,(LPCTSTR)filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_SHARED);
    if( hBmp==NULL )
    {
        cout<< "could not load\n";
        system("pause");
        return 0;
    }

    BITMAP bmp;
    HDC hdc = CreateCompatibleDC(NULL);
    GetObject(hBmp,sizeof(bmp),&bmp);
    BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,hdc,0,0,SRCCOPY);

    for(int y=0;y<bmp.bmHeight;y++)
    {
        for(int x=0;x<bmp.bmWidth;x++)
        {
            if(x==0) 
                cout<< endl;

            COLORREF clr;
            clr = GetPixel(hdc,x,y);

            if( clr != CLR_INVALID )
                cout<< 0+(int)(clr==0);
            else 
                cout<< 'x';
        }
    }
    system("pause");

    DeleteDC(hdc);
    DeleteObject(hBmp);

    return 0;
}
4

1 に答える 1

2

DCへのビットマップを選択する必要があります。

HBITMAP hOldBmp = SelectObject(hdc, hBmp);

// I haven't understood what you're trying to achieve with this line of code
BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,hdc,0,0,SRCCOPY);

   ....

SelectObject(hDc, hOldBmp);
DeleteDC(hdc);
   ....

メモリDCを作成すると、デフォルトで1x1ビットマップが選択されます。

于 2011-05-20T11:28:46.697 に答える