[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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);
  • 3
  • 2
1 Solution
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.

librazoneAuthor Commented:
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:

    // 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);

      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); }
    // Have GetDIBits convert OffscrBmp to a DIB (device-independent bitmap):

    // Create a file to save the DIB to:

    // 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;}
        // bitmap is palettized?
        PalEntries = (lpbi->bmiHeader.biBitCount <= 8) ?
                     (int)(1 << lpbi-bmiHeader.biBitCount):
    // 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;

To get the current file pointer position, do this:

   SetFilePointer(BmpFile, 0, NULL, FILE_CURRENT);

Return value is the current file pointer position.

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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.

librazoneAuthor Commented:
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.


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now