Try to create Bitmap but cannot open it

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 0saya0
    New Member
    • Aug 2008
    • 3

    Try to create Bitmap but cannot open it

    Hello,

    I am writing a simple program that is creating a bmp file. Unfortunately it seems that my Bitmap does not contain any color data.
    Where is my mistake ?
    Code:
    //
    #pragma once
       
    #include "stdafx.h"
    #include <windows.h>
    #include <fstream>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    void wait ()
    {
        std::cin.clear();
        std::cin.ignore(std::cin.rdbuf()->in_avail());
        std::cin.get();
    } 
    
    int main(){
    
    	int i,j,r;
    
    	ofstream bmpdata("BITMAP.bmp", ios::out|ios::binary); //begin stream
    	
    	if (!bmpdata) return 1;
    
    	BITMAPINFOHEADER bmpinfo; //Infoheader Data
    	bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
    	bmpinfo.biWidth = 256;
    	bmpinfo.biHeight = 256;
    	bmpinfo.biPlanes = 1;
    	bmpinfo.biBitCount = 24;															
    	bmpinfo.biCompression = BI_RGB;
    	bmpinfo.biSizeImage = 0;
    	bmpinfo.biXPelsPerMeter = 0;
    	bmpinfo.biYPelsPerMeter = 0;
    	bmpinfo.biClrUsed = 0;
    	bmpinfo.biClrImportant = 0;
    
    	BITMAPFILEHEADER bmpfile;	//Fileheader Data
    	bmpfile.bfType = 'Mb';
    	bmpfile.bfSize = (bmpinfo.biWidth * bmpinfo.biHeight * (bmpinfo.biBitCount / 8)) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //aggregate - bmpfile
    	bmpfile.bfReserved1 = 0;
    	bmpfile.bfReserved2 = 0;
    	bmpfile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    
    	bmpdata.write((char*)&bmpinfo, sizeof(bmpinfo));
    	bmpdata.write((char*)&bmpfile, sizeof(bmpfile));
    
    	RGBQUAD bmp;;
    	for(r=0;r < 256; r++)   {
    		bmp.rgbBlue = r;
    		bmp.rgbGreen = r;
    		bmp.rgbRed = r;
    		bmp.rgbReserved=0;
    
          //writing directly to the file:
    		bmpdata.write((char*)&(bmp.rgbBlue), sizeof(bmp.rgbBlue));
    		bmpdata.write((char*)&(bmp.rgbGreen), sizeof(bmp.rgbGreen));
    		bmpdata.write((char*)&(bmp.rgbRed), sizeof(bmp.rgbRed));
    		bmpdata.write((char*)&(bmp.rgbReserved), sizeof(bmp.rgbReserved));
    	}
    
    	if(bmpinfo.biBitCount == 24) //fill image
        {
            for (i=0; i < bmpinfo.biHeight ; i++){
                for (j=0; j < bmpinfo.biWidth; j++){
    				bmpdata.write((char*)&bmp, sizeof(bmp));           
                }
            }
    	}
    
    	bmpdata.close();
    			
    	cout << "Size: " << bmpfile.bfSize << "byte" << 
    			" Width: " << bmpinfo.biWidth <<
    			" Height: " << bmpinfo.biHeight <<
    			" Blue: " << bmp.rgbBlue <<
    			"\nBitmap created successfully" << endl;
    	wait();
    
    	return 1;
    }
    thanks in advance
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    There are a few things that aren't right with the bitmap files your program is creating.
    Firstly:
    bmpfile.bfType = 'Mb'; should be bmpfile.bfType = 'MB';
    Secondly:
    Code:
    bmpdata.write((char*)&bmpfile, sizeof(bmpfile));
    bmpdata.write((char*)&bmpinfo, sizeof(bmpinfo));
    You're writing these in the wrong order. Swich these two lines of code around.
    Thirdly:
    Don't write bmp.rgbReserved to the file. It shouldn't be there. Get rid of it in both of the places you're writing it.
    Lastly:
    Code:
    for (i=0; i < bmpinfo.biHeight; i++)
    Don't count all the way up to bmpinfo.biHeigh t, because you already wrote a row to the file. Make it bmpinfo.biHeigh t - 1
    After all of that is fixed, your program should make real .bmp files.
    Hope this helps.

    Comment

    • 0saya0
      New Member
      • Aug 2008
      • 3

      #3
      Thank your very much !

      It really works now. A small milestone for me :-)

      But now there is another problem. :( Take a look at the picture.



      I cannot change the colors. Always this grayscale type of thing :(

      Modified Code:

      Code:
      [...]
      
       bmpfile.bfType = 'MB';
                bmpfile.bfSize = (bmpinfo.biWidth * bmpinfo.biHeight * (bmpinfo.biBitCount / 8)) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //aggregate - bmpfile
                bmpfile.bfReserved1 = 0;
                bmpfile.bfReserved2 = 0;
                bmpfile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
      
      		  bmpdata.write((char*)&bmpfile, sizeof(bmpfile));
                bmpdata.write((char*)&bmpinfo, sizeof(bmpinfo));
                
             
                RGBQUAD bmp;;
                for(r=0;r < 255; r++)   {
                    bmp.rgbBlue = r;
                    bmp.rgbGreen = 0;
                    bmp.rgbRed = 0;
                    bmp.rgbReserved=0;
             
                  //writing directly to the file:
                    bmpdata.write((char*)&(bmp.rgbBlue), sizeof(bmp.rgbBlue));
                    bmpdata.write((char*)&(bmp.rgbGreen), sizeof(bmp.rgbGreen));
                    bmpdata.write((char*)&(bmp.rgbRed), sizeof(bmp.rgbRed));
                }
             
                if(bmpinfo.biBitCount == 24) //fill image
                {
                    for (i=0; i < bmpinfo.biHeight-1 ; i++){
                        for (j=0; j < bmpinfo.biWidth; j++){
                            bmpdata.write((char*)&bmp, sizeof(bmp));           
                        }
                    }
                }
             
                bmpdata.close();
                       
                cout << "\nBitmap created successfully" << endl;
                wait();
             
                return 1;
            }
      Best Regards

      Saya

      Comment

      • 0saya0
        New Member
        • Aug 2008
        • 3

        #4
        Edit: bmp.Reserved also needs a value!

        But this makes it impossible to create a blank red bitmap. :(

        Anybody an idea ?

        Edit:

        solved that problem. Have to Change sizeof(bmp) to 3

        Code:
        bmpdata.write((char*)&bmp, 3);

        Comment

        Working...