You are here:

C++/BMP Image processing in C/C++


i am a university student and i want to Write a C/C++ program that extracts the width and height of images with BMP file
format.I've searched alot in web & i found some codes .but they were too complicated .plese help me to write this program.i have no idea what to do
thanks alot

Hi dear

presenting you a simple BMP processor program in C long  days ago

#include <alloc.h>
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>

struct BMP
 char Type[2];          //File type. Set to "BM".
 unsigned long Size;     //Size in BYTES of the file.
 unsigned long Reserved;      //Reserved. Set to zero.
 unsigned long OffSet;   //Offset to the data.
 unsigned long headsize; //Size of rest of header. Set to 40.
 unsigned long Width;     //Width of bitmap in pixels.
 unsigned long Height;     //  Height of bitmap in pixels.
 unsigned int  Planes;    //Number of Planes. Set to 1.
 unsigned int  BitsPerPixel;       //Number of Bits per pixels.
 unsigned long Compression;   //Compression. Usually set to 0.
 unsigned long SizeImage;  //Size in bytes of the bitmap.
 unsigned long XPixelsPreMeter;     //Horizontal pixels per meter.
 unsigned long YPixelsPreMeter;     //Vertical pixels per meter.
 unsigned long ColorsUsed;   //Number of colors used.
 unsigned long ColorsImportant;  //Number of "important" colors.

int ShowBMP(int x, int y, char* FileName)
   int b,a;
   BMP Obj;

   unsigned char* Datas;
   int in=0;
   unsigned char c=0;
   FILE * fp;

   fp = fopen(FileName,"rb");
   printf("Error : Unable to open file ..");

   fread(&Obj, sizeof(Obj), 1, fp);
   if(Obj.BitsPerPixel!=4)  // This isn't a 16 color bmp we can read;
    printf("Error : File format not supported ..");
   Datas=(unsigned char*) calloc(Obj.Width/2+1, sizeof(unsigned char));
    fread(Datas, sizeof(unsigned char), Obj.Width/2, fp);
        c = (Datas[in] | 0x00) >>4;
        c = (Datas[in] | 0xF0) & 0x0F;
   free (Datas);
   return 1;
void main()
int color;
int gd , gm ;
gd = VGA ; gm = VGAHI;


 The program presented above is strictly compatible with Turbo C/C++ of any versions that support BGI.
 Here im unable to present you the bitmap file "sample.bmp"  , so i recommend you to create your own file to test this program !
 While creating your own file   make sure that the new file should be compatible to this program ! other wise u will get undesired results !

If you take a look at the BMP Struct you can find two structure members

unsigned long Width;     //Width of bitmap in pixels.
unsigned long Height;     //  Height of bitmap in pixels.

once after execution of this statement >>fread(&Obj, sizeof(Obj), 1, fp);
BMP struct will be filled with almost all the properties of the given BMP as mentioned in the BMP struct you can easily access the height and width of the given Image as well
just by
Width: Obj.Width;
Height: Obj.Height;

Hope this will help you !

Thanks and Regards!
Prince M. Premnath


All Answers

Answers by Expert:

Ask Experts


Prince M. Premnath


Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)


More than 5 years


©2017 All rights reserved.