Can C++ use to create a bitmap?

Hi all,

I have to create a bitmap image and thought of doing it in c++. The bitmap will be having a 10x10 pixel where i can assign a 1 or 0 to a particular pixel.

My question is can this be done using c++. If can, how do i start doing one as i am absolutely new to c++ and know nothing about it.

Thanks.



I suggest you do not manually create the bitmap. The library easybmp is perfect for this and is one of the easiest libraries to start using I've ever found.
I found what you mean on easybmp.sourceforge.net but could not quite understand it. Do you know of any tutorial which will guide me through creating a bitmap?

Thanks again.
1
2
3
4
5
6
7
8
9
10
11
12
13
BMP AnImage;
// Set size to 640 × 480
AnImage.SetSize(640,480);
// Set its color depth to 32-bits
AnImage.SetBitDepth(32);

// Set one of the pixels
AnImage(14,18)->Red = 255;
AnImage(14,18)->Green = 255;
AnImage(14,18)->Blue = 255;
AnImage(14,18)->Alpha = 0;

AnImage.WriteToFile("Output.bmp");
Woo,that's really nice of you. That helps alot man!Many thanks!
Another question came while reading the Easybmp user guide.
Can this easybmp library be used in microsoft visual studio 2008 since there was no compiling instruction given in the web whereas there is instruction for other compiler like Borland?
Yes.

You have two choices. The very easy latest version (#include the header, make sure you compile the .cpp file along with the rest of your code) or the very very easy previous version, in which you just have to #include the header (there is no cpp file).

Well, very easy for you is not easy for me through...face some problem while trying to build the below source code in Win32 project using microsoft visual studio 2008 and need your help!!


# include "EasyBMP.h"
using namespace std;


int main()
{
BMP AnImage;
AnImage.SetSize (640,480);
AnImage.SetBitDepth (8);
AnImage (14,18) -> Red =255;
AnImage (14,18) -> Green =255;
AnImage (14,18) -> Blue =255;
AnImage (14,18) -> Alpha =0;
AnImage.WriteToFile ("output.bmp");

return 0;
}

What i expect was a bitmap image in a file name "output.bmp" but when i build solution the following error occur;

"error LNK2019: unresolved external symbol "public: __thiscall BMP::~BMP(void)" (??1BMP@@QAE@XZ) referenced in function _main"

Seems like there is some linkage error, but i have include the following header file as per instruction in EasyBMP website in my directory;

1. EasyBMP.h
2. EasyBMP DataStructures.h
3. EasyBMP BMP.h
4. EasyBMP VariousBMPutilities.h
5. EasyBMP.cpp
6. EasyBMPsample.cpp

The way i include these header file is as below;

Tools > Options > Projects and Solutions > VC++ Directories
Select "Include files" on the combo box under "Show directories for:"
And then add the folder which contain all the 6 header files.

Can you identify where the fault is? Seriously need help as i was dump to do this with no help from academic stuff at all and i know nothing about c++.


I extracted easybmp to a single directory, listing as below:


j@j-desktop:~/Downloads/easybmp$ l
-rw-r--r-- 1 j j 1489 2005-11-01 18:12 BSD_(revised)_license.txt
-rw-r--r-- 1 j j 2815 2006-12-01 09:20 EasyBMP_BMP.h
-rw-r--r-- 1 j j 31370 2006-12-01 09:29 EasyBMP_ChangeLog.txt
-rw-r--r-- 1 j j 48149 2006-12-01 09:22 EasyBMP.cpp
-rw-r--r-- 1 j j 2708 2006-12-01 09:02 EasyBMP_DataStructures.h
-rw-r--r-- 1 j j 2418 2006-12-01 09:05 EasyBMP.h
-rw-r--r-- 1 j j 1975 2006-12-01 09:02 EasyBMP_VariousBMPutilities.h


I created a file in that directory named 01.cpp with contents as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "EasyBMP.h"
int main()
{
BMP AnImage;
// Set size to 640 × 480
AnImage.SetSize(640,480);
// Set its color depth to 32-bits
AnImage.SetBitDepth(32);

 for (int i=1;i<100;i++)
	  {
	    for (int j=1; j<100; j++)
	      {
                  // Set one of the pixels
                 AnImage(i,j)->Red = 0;
                 AnImage(i,j)->Green = 0;
                 AnImage(i,j)->Blue = 0;
                 AnImage(i,j)->Alpha = 0;
	      }
}
AnImage.WriteToFile("Output.bmp");
 return 0;
}


I compiled it witht he following command line:

g++ -o program 01.cpp EasyBMP.cpp


Ran it as follows:

./program

and the new directory listing is:

-rw-r--r-- 1 j j 410 2011-07-08 23:01 01.cpp
-rw-r--r-- 1 j j 345 2011-07-08 22:56 01.cpp~
-rw-r--r-- 1 j j 1489 2005-11-01 18:12 BSD_(revised)_license.txt
-rw-r--r-- 1 j j 2815 2006-12-01 09:20 EasyBMP_BMP.h
-rw-r--r-- 1 j j 31370 2006-12-01 09:29 EasyBMP_ChangeLog.txt
-rw-r--r-- 1 j j 48149 2006-12-01 09:22 EasyBMP.cpp
-rw-r--r-- 1 j j 2708 2006-12-01 09:02 EasyBMP_DataStructures.h
-rw-r--r-- 1 j j 2418 2006-12-01 09:05 EasyBMP.h
-rw-r--r-- 1 j j 1975 2006-12-01 09:02 EasyBMP_VariousBMPutilities.h
-rw-r--r-- 1 j j 1228854 2011-07-08 23:01 Output.bmp
-rwxr-xr-x 1 j j 50139 2011-07-08 23:01 program*


Output.bmp is a bitmap file of the expected size with a big black square in it.



It looks to me like you are not compiling the cpp file EasyBMP.cpp in your code. You're using Visual Studio.



Be sure to add EasyBMP.cpp to your project to be compiled. That's what "unresolved external" usually means. You're not compiling EasyBMP.cpp into your project (or missing the library, but easyBMP has no libraries).


Last edited on
OhOh...finally got it!!
Dear Moschops, you are really great and i would like to say thank you for your help!!
this post saved me a lot of trouble, thanks a lot! (was about to give up trying to find a library and write the input and output for bmp's myself which would have been a big waste of time)
Topic archived. No new replies allowed.