librazone
asked on
File Pointers
The GetFilePointer function does not work.Is there an include file for this or another way of doing it?
bmfh.bfOffBits =GetFilePointer(BmpFile);
bmfh.bfOffBits =GetFilePointer(BmpFile);
ASKER
Just taking a screenshot of the app.Heres the code.
BOOL Screenshot(LPCTSTR FileName)
{
int Width;
int Height;
int PalEntries;
int cbColorTableSize;
int cbSize;
DWORD Written;
if (!FileName ) return FALSE;
Width = rc.right;
Height =rc.bottom;
// Create a GDI-compatible device context for the surface:
SurfDC=GetDC(hwnd);
// We need an HBITMAP to convert it to a DIB:
if ((OffscrBmp = CreateCompatibleBitmap(Sur fDC, Width, Height)) == NULL) { MessageBox(hwnd,"Could not get bitmap","ERROR",MB_OK|MB_O KCANCEL); }
OffscrDC = CreateCompatibleDC(SurfDC) ;
// Select OffscrBmp into OffscrDC:
OldBmp = (HBITMAP)SelectObject(Offs crDC, OffscrBmp);
BitBlt(OffscrDC, 0, 0, Width, Height, SurfDC, 0, 0, SRCCOPY);
ReleaseDC(hwnd,SurfDC);
SurfDC = NULL;
cbColorTableSize = sizeof(RGBQUAD)+1000;//*(G etColorTab leSize()-1 );
cbSize = sizeof(BITMAPINFO) + cbColorTableSize;
lpbi = (LPBITMAPINFO)malloc(cbSiz e);
ZeroMemory(&lpbi->bmiHeade r, sizeof(BITMAPINFOHEADER));
lpbi->bmiHeader.biSize =sizeof(BITMAPINFOHEADER);
SelectObject(OffscrDC, OldBmp);
GetDIBits(OffscrDC, OffscrBmp, 0, Height, NULL, lpbi, DIB_RGB_COLORS);
// if ((lpvBits =char[lpbi->bmiHeader.biSi zeImage]) == NULL) {MessageBox(hwnd,"Could not bit memory","ERROR",MB_OK|MB_O KCANCEL); }
cbSize=lpbi->bmiHeader.biS izeImage;
lpvBits=malloc(cbSize);
// Have GetDIBits convert OffscrBmp to a DIB (device-independent bitmap):
GetDIBits(OffscrDC,OffscrB mp,0,Heigh t,NULL,lpb i,DIB_RGB_ COLORS);
// Create a file to save the DIB to:
BmpFile = CreateFile(FileName,GENERI C_WRITE,0, NULL,CREATE_ALWAYS,FILE_AT TRIBUTE_NO RMAL,NULL) ;
// Write a file header to the file:
bmfh.bfType = 19778;
// bmfh.bfSize = ???
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
// bmfh.bfOffBits = ???
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
{ MessageBox(hwnd,"Could not write file","ERROR",MB_OK|MB_OKC ANCEL); }
if (Written < sizeof(bmfh)) { MessageBox(hwnd,"wrong size","ERROR",MB_OK|MB_OKC ANCEL); }
// Write BITMAPINFOHEADER to the file:
if (!WriteFile(BmpFile, &lpbi->bmiHeader, sizeof(BITMAPINFOHEADER),
&Written, NULL)) { MessageBox(hwnd,"Could not get screen rect","ERROR",MB_OK|MB_OKC ANCEL); }
if (Written < sizeof(BITMAPINFOHEADER)) { MessageBox(hwnd,"Could not write file","ERROR",MB_OK|MB_OKC ANCEL); }
if (lpbi->bmiHeader.biCompres sion == BI_BITFIELDS) { PalEntries = 3;}
else
// bitmap is palettized?
PalEntries = (lpbi->bmiHeader.biBitCoun t <= 8) ?
(int)(1 << lpbi-bmiHeader.biBitCount) :
0;
// If biClrUsed use only biClrUsed palette entries:
if (lpbi->bmiHeader.biClrUsed ) PalEntries = lpbi->bmiHeader.biClrUsed;
// Write palette to the file:
if (PalEntries)
{
if (!WriteFile(BmpFile, &lpbi->bmiColors, PalEntries * sizeof(RGBQUAD),
&Written, NULL)) return FALSE;
if (Written < PalEntries * sizeof(RGBQUAD)) return FALSE;
}
// The current position in the file
// will be saved to the BITMAPFILEHEADER:
bmfh.bfOffBits =GetFilePointer(BmpFile);
// Write bitmap bits to the file:
WriteFile(BmpFile, lpvBits, lpbi->bmiHeader.biSizeImag e,
&Written, NULL);
if (Written < lpbi->bmiHeader.biSizeImag e) return FFALSE;
bmfh.bfSize =GetFilePointer(BmpFile);
// dwPointer = SetFilePointer (hFile, lDistance,
// NULL, FILE_END);
//SetFilePointer(BmpFile, 0, 0, FILE_BEGIN);
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
return FALSE;
if (Written < sizeof(bmfh)) return FALSE;
if (SurfDC) ReleaseDC(hwnd,SurfDC);
if (OffscrDC) DeleteDC(OffscrDC);
if (OffscrBmp) DeleteObject(OffscrBmp);
if (BmpFile != INVALID_HANDLE_VALUE) CloseHandle(BmpFile);
return TRUE;
}
BOOL Screenshot(LPCTSTR FileName)
{
int Width;
int Height;
int PalEntries;
int cbColorTableSize;
int cbSize;
DWORD Written;
if (!FileName ) return FALSE;
Width = rc.right;
Height =rc.bottom;
// Create a GDI-compatible device context for the surface:
SurfDC=GetDC(hwnd);
// We need an HBITMAP to convert it to a DIB:
if ((OffscrBmp = CreateCompatibleBitmap(Sur
OffscrDC = CreateCompatibleDC(SurfDC)
// Select OffscrBmp into OffscrDC:
OldBmp = (HBITMAP)SelectObject(Offs
BitBlt(OffscrDC, 0, 0, Width, Height, SurfDC, 0, 0, SRCCOPY);
ReleaseDC(hwnd,SurfDC);
SurfDC = NULL;
cbColorTableSize = sizeof(RGBQUAD)+1000;//*(G
cbSize = sizeof(BITMAPINFO) + cbColorTableSize;
lpbi = (LPBITMAPINFO)malloc(cbSiz
ZeroMemory(&lpbi->bmiHeade
lpbi->bmiHeader.biSize =sizeof(BITMAPINFOHEADER);
SelectObject(OffscrDC, OldBmp);
GetDIBits(OffscrDC, OffscrBmp, 0, Height, NULL, lpbi, DIB_RGB_COLORS);
// if ((lpvBits =char[lpbi->bmiHeader.biSi
cbSize=lpbi->bmiHeader.biS
lpvBits=malloc(cbSize);
// Have GetDIBits convert OffscrBmp to a DIB (device-independent bitmap):
GetDIBits(OffscrDC,OffscrB
// Create a file to save the DIB to:
BmpFile = CreateFile(FileName,GENERI
// Write a file header to the file:
bmfh.bfType = 19778;
// bmfh.bfSize = ???
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
// bmfh.bfOffBits = ???
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
{ MessageBox(hwnd,"Could not write file","ERROR",MB_OK|MB_OKC
if (Written < sizeof(bmfh)) { MessageBox(hwnd,"wrong size","ERROR",MB_OK|MB_OKC
// Write BITMAPINFOHEADER to the file:
if (!WriteFile(BmpFile, &lpbi->bmiHeader, sizeof(BITMAPINFOHEADER),
&Written, NULL)) { MessageBox(hwnd,"Could not get screen rect","ERROR",MB_OK|MB_OKC
if (Written < sizeof(BITMAPINFOHEADER)) { MessageBox(hwnd,"Could not write file","ERROR",MB_OK|MB_OKC
if (lpbi->bmiHeader.biCompres
else
// bitmap is palettized?
PalEntries = (lpbi->bmiHeader.biBitCoun
(int)(1 << lpbi-bmiHeader.biBitCount)
0;
// If biClrUsed use only biClrUsed palette entries:
if (lpbi->bmiHeader.biClrUsed
// Write palette to the file:
if (PalEntries)
{
if (!WriteFile(BmpFile, &lpbi->bmiColors, PalEntries * sizeof(RGBQUAD),
&Written, NULL)) return FALSE;
if (Written < PalEntries * sizeof(RGBQUAD)) return FALSE;
}
// The current position in the file
// will be saved to the BITMAPFILEHEADER:
bmfh.bfOffBits =GetFilePointer(BmpFile);
// Write bitmap bits to the file:
WriteFile(BmpFile, lpvBits, lpbi->bmiHeader.biSizeImag
&Written, NULL);
if (Written < lpbi->bmiHeader.biSizeImag
bmfh.bfSize =GetFilePointer(BmpFile);
// dwPointer = SetFilePointer (hFile, lDistance,
// NULL, FILE_END);
//SetFilePointer(BmpFile, 0, 0, FILE_BEGIN);
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
return FALSE;
if (Written < sizeof(bmfh)) return FALSE;
if (SurfDC) ReleaseDC(hwnd,SurfDC);
if (OffscrDC) DeleteDC(OffscrDC);
if (OffscrBmp) DeleteObject(OffscrBmp);
if (BmpFile != INVALID_HANDLE_VALUE) CloseHandle(BmpFile);
return TRUE;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I see, this is a lot easier than you might have guessed. Here is the code with a few modifications:
bmfh.bfType = (WORD)( 'B' | 'M' << 8 );
bmfh.bfSize = sizeof( bmfh ) + sizeof( BITMAPINFO ) + cbColorTableSize + lpbi->bmiHeader.biSizeImag e;
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof( BITMAPINFO ) + cbColorTableSize;
I modified the bfType to make it easier to read -- no additional code will be produced; it produces exactly the same code that you had before. The size consists of the both headers plus the size of the raw bits. The bits follow directly after the colortable, thus the offset is the size of the BITMAPINFO structure plus the color table size. Hope this solved it for you. If not, feel free to ask for additional help.
.f
bmfh.bfType = (WORD)( 'B' | 'M' << 8 );
bmfh.bfSize = sizeof( bmfh ) + sizeof( BITMAPINFO ) + cbColorTableSize + lpbi->bmiHeader.biSizeImag
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof( BITMAPINFO ) + cbColorTableSize;
I modified the bfType to make it easier to read -- no additional code will be produced; it produces exactly the same code that you had before. The size consists of the both headers plus the size of the raw bits. The bits follow directly after the colortable, thus the offset is the size of the BITMAPINFO structure plus the color table size. Hope this solved it for you. If not, feel free to ask for additional help.
.f
ASKER
Since both answers work and Tasco was the first I am giving points to Tasco.Will be posting more questions on bitmaps shortly.
I doubt that it works, but you will have to find out for yourself. At least you cannot write your bitmap in one pass, you will always have to go back and alter information already stored in the file, leaving the file inconsistent at one point in time -- something you should never allow to happen, imho.
.f
.f
.f