473,485 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Try to create Bitmap but cannot open it

3 New Member
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 ?
Expand|Select|Wrap|Line Numbers
  1. //
  2. #pragma once
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. using namespace std;
  12.  
  13. void wait ()
  14. {
  15.     std::cin.clear();
  16.     std::cin.ignore(std::cin.rdbuf()->in_avail());
  17.     std::cin.get();
  18.  
  19. int main(){
  20.  
  21.     int i,j,r;
  22.  
  23.     ofstream bmpdata("BITMAP.bmp", ios::out|ios::binary); //begin stream
  24.  
  25.     if (!bmpdata) return 1;
  26.  
  27.     BITMAPINFOHEADER bmpinfo; //Infoheader Data
  28.     bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
  29.     bmpinfo.biWidth = 256;
  30.     bmpinfo.biHeight = 256;
  31.     bmpinfo.biPlanes = 1;
  32.     bmpinfo.biBitCount = 24;                                                            
  33.     bmpinfo.biCompression = BI_RGB;
  34.     bmpinfo.biSizeImage = 0;
  35.     bmpinfo.biXPelsPerMeter = 0;
  36.     bmpinfo.biYPelsPerMeter = 0;
  37.     bmpinfo.biClrUsed = 0;
  38.     bmpinfo.biClrImportant = 0;
  39.  
  40.     BITMAPFILEHEADER bmpfile;    //Fileheader Data
  41.     bmpfile.bfType = 'Mb';
  42.     bmpfile.bfSize = (bmpinfo.biWidth * bmpinfo.biHeight * (bmpinfo.biBitCount / 8)) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //aggregate - bmpfile
  43.     bmpfile.bfReserved1 = 0;
  44.     bmpfile.bfReserved2 = 0;
  45.     bmpfile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  46.  
  47.     bmpdata.write((char*)&bmpinfo, sizeof(bmpinfo));
  48.     bmpdata.write((char*)&bmpfile, sizeof(bmpfile));
  49.  
  50.     RGBQUAD bmp;;
  51.     for(r=0;r < 256; r++)   {
  52.         bmp.rgbBlue = r;
  53.         bmp.rgbGreen = r;
  54.         bmp.rgbRed = r;
  55.         bmp.rgbReserved=0;
  56.  
  57.       //writing directly to the file:
  58.         bmpdata.write((char*)&(bmp.rgbBlue), sizeof(bmp.rgbBlue));
  59.         bmpdata.write((char*)&(bmp.rgbGreen), sizeof(bmp.rgbGreen));
  60.         bmpdata.write((char*)&(bmp.rgbRed), sizeof(bmp.rgbRed));
  61.         bmpdata.write((char*)&(bmp.rgbReserved), sizeof(bmp.rgbReserved));
  62.     }
  63.  
  64.     if(bmpinfo.biBitCount == 24) //fill image
  65.     {
  66.         for (i=0; i < bmpinfo.biHeight ; i++){
  67.             for (j=0; j < bmpinfo.biWidth; j++){
  68.                 bmpdata.write((char*)&bmp, sizeof(bmp));           
  69.             }
  70.         }
  71.     }
  72.  
  73.     bmpdata.close();
  74.  
  75.     cout << "Size: " << bmpfile.bfSize << "byte" << 
  76.             " Width: " << bmpinfo.biWidth <<
  77.             " Height: " << bmpinfo.biHeight <<
  78.             " Blue: " << bmp.rgbBlue <<
  79.             "\nBitmap created successfully" << endl;
  80.     wait();
  81.  
  82.     return 1;
thanks in advance
Aug 7 '08 #1
3 2294
boxfish
469 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. bmpdata.write((char*)&bmpfile, sizeof(bmpfile));
  2. 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:
Expand|Select|Wrap|Line Numbers
  1. for (i=0; i < bmpinfo.biHeight; i++)
Don't count all the way up to bmpinfo.biHeight, because you already wrote a row to the file. Make it bmpinfo.biHeight - 1
After all of that is fixed, your program should make real .bmp files.
Hope this helps.
Aug 7 '08 #2
0saya0
3 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. [...]
  2.  
  3.  bmpfile.bfType = 'MB';
  4.           bmpfile.bfSize = (bmpinfo.biWidth * bmpinfo.biHeight * (bmpinfo.biBitCount / 8)) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //aggregate - bmpfile
  5.           bmpfile.bfReserved1 = 0;
  6.           bmpfile.bfReserved2 = 0;
  7.           bmpfile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  8.  
  9.           bmpdata.write((char*)&bmpfile, sizeof(bmpfile));
  10.           bmpdata.write((char*)&bmpinfo, sizeof(bmpinfo));
  11.  
  12.  
  13.           RGBQUAD bmp;;
  14.           for(r=0;r < 255; r++)   {
  15.               bmp.rgbBlue = r;
  16.               bmp.rgbGreen = 0;
  17.               bmp.rgbRed = 0;
  18.               bmp.rgbReserved=0;
  19.  
  20.             //writing directly to the file:
  21.               bmpdata.write((char*)&(bmp.rgbBlue), sizeof(bmp.rgbBlue));
  22.               bmpdata.write((char*)&(bmp.rgbGreen), sizeof(bmp.rgbGreen));
  23.               bmpdata.write((char*)&(bmp.rgbRed), sizeof(bmp.rgbRed));
  24.           }
  25.  
  26.           if(bmpinfo.biBitCount == 24) //fill image
  27.           {
  28.               for (i=0; i < bmpinfo.biHeight-1 ; i++){
  29.                   for (j=0; j < bmpinfo.biWidth; j++){
  30.                       bmpdata.write((char*)&bmp, sizeof(bmp));           
  31.                   }
  32.               }
  33.           }
  34.  
  35.           bmpdata.close();
  36.  
  37.           cout << "\nBitmap created successfully" << endl;
  38.           wait();
  39.  
  40.           return 1;
  41.       }
  42.  
Best Regards

Saya
Aug 8 '08 #3
0saya0
3 New Member
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

Expand|Select|Wrap|Line Numbers
  1. bmpdata.write((char*)&bmp, 3);
  2.  
Aug 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4809
by: Steve Amey | last post by:
Hi all I have an ARGB value for a Colour (Eg. -65536. The value was retrieved by using the Color.ToArgb method), is there any way that I can create a System.Drawing.Image or a...
11
7441
by: Prashant | last post by:
Hi, I have a huge problem. I have a data file which looks something like this -: ..1 .5 .9 -1 .2 .5 ...... ..2 .9 .1 .4 .3 -1 ...... ..2 .4 .5 .7 .6 .2 ...... ........
5
7345
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color...
3
8607
by: CSH | last post by:
Hi all, I've run into a small problem opening and saving bitmaps. Concider the following code: Dim oBM as Bitmap Dim cFileName as String ... some code to get the filename etc...
7
2281
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
0
2588
by: news.microsoft.com | last post by:
Hi guys, This text looks long and complicate but it is not, and I really really need some help here. For the last 24hs I'm trying to resolve this issue (last night I dreamed
2
2655
by: Cameron Walsh | last post by:
Hi all, I'm trying to extract the data from a bitmap or .pnm file using the following code: import Image img = Image.open("test.bmp","r") data=img.getdata() Unfortunately I get the...
4
12411
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
1268
by: Victor Reboucas | last post by:
Hi, I'm a VB.Net developer and cannot find a way to create a bipmap from a pointer... My application does video rendering using Windows media encoder (that's out of the content of this message)....
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7116
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4857
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.