Thread: asking for bitmap info

  1. #1
    Registered User
    Join Date
    Aug 2006

    asking for bitmap info

    hi guys .. I am a beginner programmer using c and Im looking for info on how to create a bitmap file .. draw to it .. close it ..and save it.

    any help appreciated .. Im in an info gathering phase right now so anything considered..

    thanks al.

  2. #2
    Registered User
    Join Date
    Aug 2003
    A good start is to read up on the bmp file format: BMP file format - Wikipedia, the free encyclopedia

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    If you're writing a game it might pay off to use a library like the SDL (Simple DirectMedia Layer), which provides support for loading BMPs, displaying them, drawing pixels and handing keyboard and mouse input. If you're just wanting to use the Windows API (not what I'd suggest but you can if you wish) I believe LoadImage() is a good place to start.

    See also:
    SDL displaying .bmp
    Lazy Foo' Productions
    C++: graphics, memory device context, loadimage function

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2006
    thanks for the help so far ..

    I want to draw huge mandelbrot set pictures ..

    currently I can draw pictures up too 12,000 x 8,000 pixels using sdl .. I want to increase that to at least 60,000 x 40,000 which I cannot do at the moment if it is even possible so I am looking at creating a bitmap file and dawing to the file.

    any hints suggestions or downright critisims appreciated .. al.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    There are several significant problems with images that size.

    1. An image that size, even if only 8 bits per pixel, will take 2.4 gigabytes of memory.
    2. 2.4 gigabytes is more than a userspace process can allocate on some operating systems.
    3. 2.4 billion is exceeds the range of a signed 32-bit integer.
    4. 60000 and 40000 both exceed the range of a signed 16-bit integer.
    5. A lot of image file formats represent the width and height of images as 16-bit integers.
    6. A lot of image file manipulation programs have bugs in them (such as using signed values instead of unsigned for certain things... ahem.)

    You might create an image that large, but have fun finding a viewer that can open it. I speak, sadly, from experience.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  6. #6
    Registered User
    Join Date
    Aug 2006
    thanks for the heads up .. appreciate it .. could have spent a lot of time and effort only to be frustrated ..

  7. #7
    Registered User
    Join Date
    Aug 2006
    I am trying to write some code to draw the 2x2 bitmap on BMP file format - Wikipedia, the free encyclopedia.

    I don't know how to fill in the array of colors .. so far as my thinking is going I think I need a structure with red green and brown elements and an array to hold copies of the structure.

    how to access this or write out the results is still outside my skills .. I have been looking for a while on this issue .. so any help again very much appreciated ..

    heres my code so far .. its a work in progress but you can see what I have done so far .. thanks al.
    //objective write bimap from
    struct bmpfile{
      unsigned char    magic[2];
    struct fileheader{
      uint32_t         filesize;
      uint16_t         creator1;
      uint16_t         creator2;
      uint32_t         bmpoffset;
    struct bmpinfoheader{
      uint32_t        headersize;
      int32_t         width;
      int32_t         height;
      uint16_t        nplanes;
      uint16_t        bitsperpixel;
      uint32_t        compressiontype;
      uint32_t        bitmapbytesize;
      int32_t         hres;
      int32_t         vres;
      uint32_t        ncolors;
      uint32_t        nimpcolors;
    struct rgb{
      unsigned char  rgbBlue;         
      unsigned char  rgbGreen;        
      unsigned char  rgbRed;          
      unsigned char  rgbReserved;     
    void create( struct bmpfile* bfp, struct fileheader* fhp, 
    	     struct bmpinfoheader* bhp );
    void *write( struct bmpfile* bfp, struct fileheader* fhp, 
    	     struct bmpinfoheader* bhp, FILE *fp, struct rgb* p );
    FILE *open( FILE *fp );
    int close( FILE *fp );
    void create( struct bmpfile* bfp, struct fileheader* fhp, 
    	     struct bmpinfoheader* bhp ){
      int bpl;
      bfp->magic[0] = 'B';
      bfp->magic[1] = 'M';
      fhp->filesize = 70;
      fhp->bmpoffset = 54;
      bhp->width = 2;
      bhp->height = 2;
      bhp->nplanes = 1;
      bhp->bitsperpixel = 24;
      bhp->compressiontype = 0;
      bhp->bitmapbytesize = 16;
      bhp->hres = 2835;
      bhp->vres = 2835;
      bhp->ncolors = 0;
      bhp->nimpcolors = 0;
      bhp->headersize = bhp->bitsperpixel + bhp->bitmapbytesize;
    the total number of bytes necessary to store one row of pixels can be calculated
    as (( width * bitsperpixel ) / 32 ) * 4;
    in this case it is (( 2 * 24 ) / 8 ) = 6 rounded up to 8
      bpl = p->width * 3;
      if( bpl & 0x0003 ){
      bpl != 0x0003;
    the total number of bytes necessary to store an array of pixels can be 
    calculated by accounting for the effect of rounding up the size of each row to
    a multiple of 4 bytes ..
    pixelarraysize = rowsize * | height |
    8 * 2 = 16
    the total bitmap image filesize can be approximated as filesize = 54 + 
    pixelarraysize = 70
    offset is always 54
    fhp->filesize = fhp->bmpoffset + bmp->hieght * bpl
    fhp->bmpoffset = 54
    headersize is for bytes after bm ie the 24 + 16 = 40
    void *write( struct bmpfile* bfp, struct fileheader* fhp, 
    	     struct bmpinfoheader* bhp, FILE *fp, struct rgb* p ){
        write out color array here
    struct rgb *colors( struct rgb* p ){
      int c;
      for( c = 0; c < 4; c++ ){
      // 2nd trouble spot
      return( p );
    FILE *open( FILE *fp ){
      errno = 0;
      //function may choke on null filename ..
      fp = fopen("foo.bmp","wb");
      if( fp == NULL ){
        fprintf( stderr,"open fail %s\n",strerror(errno));
      return( fp );
    int close( FILE *fp ){
      int s;
      if( fp == NULL ){
        return 0;
      errno = 0;
      fclose( fp );
      if( s == EOF ){
        perror("close fail");
      return s;
    int main(){
      struct bmpfile bf;
      struct bmpfile *bfp;
      bfp = & bf;
      struct fileheader fh;
      struct fileheader *fhp;
      fhp = &fh;
      struct bmpinfoheader bh;
      struct bmpinfoheader *bhp;
      bhp = &bh;
      struct rgb color[ 4 ];
      struct rgb *p;
      p = &color;
      FILE *fp;
      colors( p );
      create( bfp,fhp,bhp );//bitmap
      open( fp );
      write( bfp, fhp, bhp, fp, p );
      close( fp );
      return( 0 );
    Last edited by mad_muppet; 05-18-2011 at 09:13 PM. Reason: clean up code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get CPU info
    By elvio.vicosa in forum C Programming
    Replies: 13
    Last Post: 03-27-2008, 01:11 PM
  2. I need info!!!
    By xixpsychoxix in forum Networking/Device Communication
    Replies: 1
    Last Post: 11-22-2007, 10:04 PM
  3. SAT info
    By Lurker in forum A Brief History of
    Replies: 11
    Last Post: 01-21-2004, 07:29 AM
  4. help info for gcc
    By Jaguar in forum Linux Programming
    Replies: 2
    Last Post: 12-28-2002, 06:34 PM
  5. need some info..
    By xlordt in forum C Programming
    Replies: 4
    Last Post: 03-22-2002, 05:48 AM