?
Solved

File Pointers

Posted on 2003-03-22
6
Medium Priority
?
270 Views
Last Modified: 2013-12-03
The GetFilePointer function does not work.Is there an include file for this or another way of doing it?

 bmfh.bfOffBits =GetFilePointer(BmpFile);
0
Comment
Question by:librazone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:fl0yd
ID: 8188158
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
0
 
LVL 1

Author Comment

by:librazone
ID: 8188504
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;
}


0
 
LVL 3

Accepted Solution

by:
TascoDLX earned 100 total points
ID: 8189261
To get the current file pointer position, do this:

   SetFilePointer(BmpFile, 0, NULL, FILE_CURRENT);

Return value is the current file pointer position.

Enjoy!
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 8

Expert Comment

by:fl0yd
ID: 8189438
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
0
 
LVL 1

Author Comment

by:librazone
ID: 8190591
Since both answers work and Tasco was the first I am giving points to Tasco.Will be posting more questions on bitmaps shortly.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 8193566
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
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question