CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    280

    Read windows bitmap graphic

    Hi,
    I want to read a graphic(Width:266,Height:341).
    After reading this graphic, I will do something on it and store it.
    I write the following codes:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
       BITMAPFILEHEADER bmheader;
       BITMAPINFOHEADER bm;
    
       FILE *fp;
       unsigned char *data;
    
       fp = fopen("origin.bmp","rb");
       if ( fp == NULL ) {
          printf("Cannot open file!\n");
          return -1;
       }
    
       fread(&bmheader,1,sizeof(BITMAPFILEHEADER),fp);
       fread(&bm,1,sizeof(BITMAPINFOHEADER),fp);
       data = (unsigned char *)malloc(bm.biHeight*bm.biWidth*3*sizeof(unsigned char)); 
       fread(data,1,bm.biHeight * bm.biWidth * 3,fp);
       fclose(fp);
    
       // do something...
    
       fp = fopen("result.bmp","wb");
       fwrite(&bmheader,1,sizeof(BITMAPFILEHEADER),fp);
       fwrite(&bm,1,sizeof(BITMAPINFOHEADER),fp);
       fwrite(data,1,bm.biHeight * bm.biWidth * 3, fp);
    
       fclose(fp);
       free(data);
       return 0;
    }
    I just write the read and store parts.
    But I find that the stored graphic can't display correctly.
    I think the size of graphic file is 272172 bytes.
    ( sizeof(BITMAPFILEHEADE) + sizeof(BITMAPINFOHEADER) + raster data( 3 * Width * Height) )
    But I find the size of graphic file is 272854. The difference is 682 ( 272854-272172).
    What' s the purpose of the 682 bytes?
    And what shall I do to store correctly?
    Thank you!

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Read windows bitmap graphic

    The size of each scanline of a windows bitmap should be a multiple of a DWORD.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Sep 2003
    Posts
    280

    Re: Read windows bitmap graphic

    Quote Originally Posted by Marc G
    The size of each scanline of a windows bitmap should be a multiple of a DWORD.
    Thank you!
    I already solve it!
    Last edited by Cooker; February 8th, 2005 at 11:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured