[英]how to use GDI to read pixels from bitmap?
我已經使用了許多其他技術來從文件中讀取像素數據,但是嘗試使用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;
}
您必須選擇到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位圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.