Solved

Load an image into a bitmap

Posted on 1998-06-08
8
333 Views
Last Modified: 2013-12-03
I have some raw 8-bit grayscale data that I want to load into a bitmap.  Can anyone give me directions on how to load this data and display it in a CFormView.  I'm using VC5.0 and MFC. Thanx

zeekial@yahoo.com
0
Comment
Question by:zeekial
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:zeekial
ID: 1165382
Edited text of question
0
 
LVL 7

Expert Comment

by:galkin
ID: 1165383
Allocate memory by any function or C++ new operator. Read file into this buffer. Call CreateDIBitmap to create DIB. Then you can either use StretchDIBits, SetDIBitsToDevice to draw DIB or to convert it to DDB and then use BitBlt or StretchBlt
0
 

Author Comment

by:zeekial
ID: 1165384
Here's some code to show you where I stand:

int fh;
char buffer[60000];
unsigned int nbytes = 60000;

fh = open("imagefile",O_RDONLY)
read(fh, buffer, nbytes);

now all my data is in character buffer "buffer".  How do I put this data into a bitmap and BitBlt it?  
0
 
LVL 6

Expert Comment

by:alamo
ID: 1165385
Is "imagefile" an actual image of a particular type, or is it simply a collection of bits?

How do you know the format (number of rows and columns) in imagefile?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:zeekial
ID: 1165386
"imagefile" is raw data of a 300X200 grayscale image I saved from another program.
0
 
LVL 7

Expert Comment

by:galkin
ID: 1165387
IF THIS IS A GRAYSCALE IMAGE IT IS ASSUMED EACH BYTE in image data is between 0 and 255. So you can create grayscale palette and create DIB from you image data so that eacch byte would be corresponding index in the palette.
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1165388
HDC DCHnd = CreateDC("DISPLAY",NULL,NULLNUL);
BITMAPINFORHEADER BMIH = {sizeof(BITMAPINFOHEADER),
300,
200,
1,
8,
BI_RGB,
0,
96,
96,
256,
256};

HBITMAP = CreateDIBitMap(DCHnd,&BMIH,CBM_INIT,Buffer,&BMI,DIB_RGB_COLORS);

This is approximately what you need. (I don't know all the details of what you have.)   If you have questions, ask.  Don't reject the answer without asking questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165389
I haven't done this in so long I forgot something.  The BITMAPINFOHEADER  and BITMAPINFO and color table are ussually created as one structure.  Like
BITMAPINFO *BMIPtr = (BITMAPINFO *) new char[sizeof BITMAPINFOHEADER) + sizeof(RGBQUAD)*256];

Then you wild fill in the BITMAPINFOHEADER part as I showed above and fill in the color table appropriately (depending on your color format etc.)

We can discuss some of these things, but you might want to look into the docs on
BITMAPINFO BITMAPINFOHEADER and CreateDIBitMap() first.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now