Link to home
Start Free TrialLog in
Avatar of librazone
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);
Avatar of fl0yd
fl0yd

GetFilePointer is not a win32 API function. I'm sure I can help you with bitmap files, though, if you give a bit more information on what exactly you want to do, ideally with the source code you have so far.

.f
Avatar of librazone

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(SurfDC, Width, Height)) == NULL)   {      MessageBox(hwnd,"Could not get bitmap","ERROR",MB_OK|MB_OKCANCEL); }
 
    OffscrDC = CreateCompatibleDC(SurfDC) ;
    // Select OffscrBmp into OffscrDC:
    OldBmp = (HBITMAP)SelectObject(OffscrDC, OffscrBmp);
    BitBlt(OffscrDC, 0, 0, Width, Height, SurfDC, 0, 0, SRCCOPY);

    ReleaseDC(hwnd,SurfDC);
      SurfDC = NULL;

       cbColorTableSize = sizeof(RGBQUAD)+1000;//*(GetColorTableSize()-1);
       cbSize = sizeof(BITMAPINFO) + cbColorTableSize;
 
  lpbi = (LPBITMAPINFO)malloc(cbSize);

    ZeroMemory(&lpbi->bmiHeader, 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.biSizeImage]) == NULL)  {MessageBox(hwnd,"Could not bit memory","ERROR",MB_OK|MB_OKCANCEL); }
      cbSize=lpbi->bmiHeader.biSizeImage;
    lpvBits=malloc(cbSize);
    // Have GetDIBits convert OffscrBmp to a DIB (device-independent bitmap):
    GetDIBits(OffscrDC,OffscrBmp,0,Height,NULL,lpbi,DIB_RGB_COLORS);

    // Create a file to save the DIB to:
    BmpFile = CreateFile(FileName,GENERIC_WRITE,0, NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,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_OKCANCEL); }
    if (Written < sizeof(bmfh))  {      MessageBox(hwnd,"wrong size","ERROR",MB_OK|MB_OKCANCEL); }

    // 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_OKCANCEL); }
    if (Written < sizeof(BITMAPINFOHEADER))  {      MessageBox(hwnd,"Could not write file","ERROR",MB_OK|MB_OKCANCEL); }

   
    if (lpbi->bmiHeader.biCompression == BI_BITFIELDS) { PalEntries = 3;}
    else
        // bitmap is palettized?
        PalEntries = (lpbi->bmiHeader.biBitCount <= 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.biSizeImage,
        &Written, NULL);
    if (Written < lpbi->bmiHeader.biSizeImage) 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;
}


ASKER CERTIFIED SOLUTION
Avatar of TascoDLX
TascoDLX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.biSizeImage;
    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
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