I in trying to translate MASM32 code from
http://www.asmcommun...p?topic=20194.0
into C code
Here is what I had done. It successfull to create a BMP file with 1366x768(my screen resolution) in directory C:\file1.bmp
But, when I open it, it's only blank black screen and it's size only 54 Bytes.
What problem? I think I already make it correctly?
BITMAPFILEHEADER hdr;
BITMAPINFOHEADER pbih;
BITMAPINFO bmpinfo;
BITMAPFILEHEADER bmpFileHeader;
HANDLE hfile;
HBITMAP hbitmap;
HDC hdc,cdc;
DWORD dwWidth, dwHeight, dwBPP, dwNumColors,ColorSize,dwByte;
LPVOID pBits;
RGBQUAD ColorTable[256];
hdc=CreateDC("DISPLAY", NULL, NULL, NULL);
if(hdc==NULL)
{
MessageBox(NULL, "Couldn''t create DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}
dwWidth=GetDeviceCaps(hdc, HORZRES);
dwHeight=GetDeviceCaps(hdc, VERTRES);
dwBPP = GetDeviceCaps(hdc, BITSPIXEL);
dwBPP=24; //24 Bit for LWF | JPEG | JPEG2000 output, else doesnt work
if(dwBPP<=8)
{
dwNumColors = GetDeviceCaps(hdc, NUMCOLORS);
dwNumColors = 256;
}
else
{
dwNumColors = 0;
}
cdc=CreateCompatibleDC(hdc);
if(cdc==NULL)
{
DeleteDC(hdc);
MessageBox(NULL, "Couldn''t create compatible DC", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
return ;
}
// Create bitmap
bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.bmiHeader.biWidth = dwWidth;
bmpinfo.bmiHeader.biHeight = dwHeight;
bmpinfo.bmiHeader.biPlanes = 1; //LOWORD(0x00000001);
bmpinfo.bmiHeader.biBitCount = (WORD) dwBPP; //LOWORD(dwBPP);
bmpinfo.bmiHeader.biCompression = BI_RGB;
bmpinfo.bmiHeader.biSizeImage = 0;
bmpinfo.bmiHeader.biXPelsPerMeter = 0;
bmpinfo.bmiHeader.biYPelsPerMeter = 0;
bmpinfo.bmiHeader.biClrUsed = dwNumColors;
bmpinfo.bmiHeader.biClrImportant = dwNumColors;
hbitmap = CreateDIBSection(hdc, &bmpinfo,DIB_PAL_COLORS, &pBits, NULL, 0);
if(hbitmap==NULL)
{
DeleteDC(hdc);
DeleteDC(cdc);
MessageBox(NULL, "Couldn''t create compatible bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}
if(!SelectObject(cdc,hbitmap))
{
DeleteDC(hdc);
DeleteDC(cdc);
MessageBox(NULL, "Couldn''t select bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}
if(!BitBlt(cdc,0,0,dwWidth,dwHeight,hdc,0,0,SRCCOPY))
{
DeleteDC(hdc);
DeleteDC(cdc);
MessageBox(NULL, "Couldn''t copy bitmap.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}
if(dwNumColors!=0)
{
dwNumColors=GetDIBColorTable(cdc,0,dwNumColors,ColorTable);
}
hdr.bfType = 0x4d42; // File type designator "BM" 0x42 = "B" 0x4d = "M"
ColorSize=dwNumColors*sizeof(RGBQUAD);
hdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ColorSize+((dwWidth*dwHeight*dwBPP)/8);
hdr.bfReserved1=0;
hdr.bfReserved2=0;
hdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ColorSize;
pbih.biSize=sizeof(BITMAPINFOHEADER);
pbih.biWidth=dwWidth;
pbih.biHeight=dwHeight;
pbih.biPlanes=1; //LOWORD(0x00000001)
pbih.biBitCount=(WORD) dwBPP; //LOWORD(dwBPP)
pbih.biCompression=BI_RGB;
pbih.biSizeImage=0;
pbih.biXPelsPerMeter=0;
pbih.biYPelsPerMeter=0;
pbih.biClrUsed=dwNumColors;
pbih.biClrImportant=0;
strcpy(strText,"C:\\file1.bmp");
hfile = CreateFile(
strText,
GENERIC_WRITE,
(DWORD) 0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hfile == INVALID_HANDLE_VALUE)
{
DeleteObject(hbitmap);
DeleteDC(cdc);
DeleteDC(hdc);
MessageBox(NULL, "Couldn''t write file to disk.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
}
WriteFile(hfile, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwByte, NULL);
WriteFile(hfile, (LPVOID) &pbih, sizeof(BITMAPINFOHEADER), (LPDWORD) &dwByte, NULL);
if(dwNumColors!=0)
{
WriteFile(hfile, ColorTable,ColorSize, (LPDWORD) &dwByte, NULL);
}
ColorSize=(dwWidth*dwHeight*dwBPP)/8; // No of pixels
WriteFile(hfile,&pBits,ColorSize,&dwByte,NULL);
CloseHandle(hfile);
MessageBox(NULL, "Bitmap created.", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
DeleteObject(hbitmap);
DeleteDC(cdc);
DeleteDC(hdc);

New Topic/Question
Reply



MultiQuote






|