Writing Bitmap Files

This is a discussion on Writing Bitmap Files within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem writing the code for writing a .bmp file. What I'm trying to do right now is ...

  1. #1
    HappyDude
    Guest

    Writing Bitmap Files

    I'm having a problem writing the code for writing a .bmp file. What I'm trying to do right now is load a bitmap then use that same information and write it into another file. The problem is, the first 10 lines or so of pixels in the image I write ends up completely black... I need help =/. Here's the code I'm using.

    Code:
    BITMAPINFOHEADER bmpiheader;//Info header
    BITMAPFILEHEADER bmpfheader;//File header
    unsigned int bbuffer[2048]; //holds the image (64x32x32bit)
    FILE *fptr; //file pointer
    fptr=fopen("first.bmp","rb"); //opens original bitmap in binary
    
    //Reads the header information into the headers
    fread(&bmpfheader,sizeof(BITMAPFILEHEADER),1,fptr);
    fread(&bmpiheader,sizeof(BITMAPINFOHEADER),1,fptr);
    
    //Reads the image into bbuffer
    fread(bbuffer,sizeof(UINT),sizeof(bbuffer),fptr);
    fclose(fptr); //closes file pointer
    
    fptr=fopen("new.bmp,"wb");//makes new .bmp file for bin writing
    
    //writes in the headers I got from the other file
    fwrite(&bmpfheader,sizeof(BITMAPFILEHEADER),1,fptr);
    fwrite(&bmpiheader,sizeof(BITMAPINFOHEADER),1,fptr);
    
    //writes in the image information from the other image
    fwrite(bbuffer,sizeof(UINT),sizeof(bbuffer),fptr);
    fclose(fptr); //closes file
    I can't understand why this makes the first 10 lines completely black...

  2. #2
    HappyDude
    Guest

    Nevermind

    I figured out the problem - the bmp file was a 24-bit image, but I was treating it as a 32bit (unsigned int). I just changed my unsigned int bbuffer[2048] to unsigned char bbuffer[2048][3] (chars are 8 bit, 3 of them make 24 bit) and now it works. YAY.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 06:33 AM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 07:00 PM
  3. Writing files to a CD
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 04-16-2003, 05:43 PM
  4. writing files to a specified folder
    By jverkoey in forum Windows Programming
    Replies: 5
    Last Post: 03-29-2003, 11:09 PM
  5. Writing to files on exact spots
    By R@m0n in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 06:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21