Click here to Skip to main content
15,922,007 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: HOW TO DETACT MULTIPLE VGA CARDS IN VC++ Pin
Anonymous29-Apr-05 20:22
Anonymous29-Apr-05 20:22 
Answerbut you are you shouting ???? Pin
Anonymous29-Apr-05 20:23
Anonymous29-Apr-05 20:23 
GeneralRe: but you are you shouting ???? Pin
ThatsAlok29-Apr-05 21:22
ThatsAlok29-Apr-05 21:22 
AnswerRe: HOW TO DETACT MULTIPLE VGA CARDS IN VC++ Pin
David Crow30-Apr-05 6:33
David Crow30-Apr-05 6:33 
GeneralProblem with flipping DDraw surfaces Pin
Kieroshark29-Apr-05 17:21
Kieroshark29-Apr-05 17:21 
Generaldesign membership function Pin
Anonymous29-Apr-05 17:21
Anonymous29-Apr-05 17:21 
GeneralBinary Waveform Pin
gremouster29-Apr-05 16:41
gremouster29-Apr-05 16:41 
QuestionHow to display bitmap bit data from memory? Pin
mikec++29-Apr-05 13:06
mikec++29-Apr-05 13:06 
Please be aware that I am a hack a total newbie to C++. I'm using Visual C++ 6.0

I have written a program to process a rectangular 2D data set and then output the 2D result
as a graphic. I have created a bitmap type structure with the resulting data. I can
currently write the created bitmap out to a BMP file, then re-load the same file and
finally display the graphic. I want to be able to just display the graphic, instead of
first going to/from disk. I've got something that displays a black rectangle instead of
the graphic. For several weeks I've been searching for a good example and have yet to find
something that works for me. I think that I'm just a line or two of code away from getting
this to work, I was hoping someone out here could help. Here's what I have:

------------- The following code works fine ----------------

// MaxArrayDim = a const value defined earlier

BYTE aBitmapBits[MaxArrayDim * MaxArrayDim * 3];

//... a bunch of code is omitted where:
// - Raw 2D data is processed and mapped to RGB data and stored in aBitmapBits
// - Physical values (NumOfBytes, NumCols, NumRows) are determined
//

BITMAPFILEHEADER BitmapFileHeader;
BITMAPINFOHEADER BitmapInfoHeader;
RGBQUAD aColors[4];

BitmapFileHeader.bfType = 0x4d42; // = 'BM'
BitmapFileHeader.bfSize = NumOfBytes + 54;
BitmapFileHeader.bfReserved1 = 0;
BitmapFileHeader.bfReserved2 = 0;
BitmapFileHeader.bfOffBits = 54;

BitmapInfoHeader.biSize = 40;
BitmapInfoHeader.biWidth = NumCols;
BitmapInfoHeader.biHeight = NumRows;
BitmapInfoHeader.biPlanes = 1;
BitmapInfoHeader.biBitCount = 24;
BitmapInfoHeader.biCompression = 0;
BitmapInfoHeader.biSizeImage = NumCols * NumRows * 3;
BitmapInfoHeader.biXPelsPerMeter = 3780;
BitmapInfoHeader.biYPelsPerMeter = 3780;
BitmapInfoHeader.biClrUsed = 0;
BitmapInfoHeader.biClrImportant = 0;

// Not needed. No color map necessary for 24-bit.
aColors->rgbBlue = 0;
aColors->rgbGreen = 0;
aColors->rgbRed = 0;
aColors->rgbReserved = 0;

//Output as a BMP file
char FileName[] = "junk.bmp";
FILE *output_file; // output file pointer

// Open the data file for output
if ((output_file = fopen(FileName, "wb")) != NULL)
{
// Output the File Header, Info Header and data bytes.
fwrite(&BitmapFileHeader.bfType, sizeof(short int), 1, output_file);
fwrite(&BitmapFileHeader.bfSize, sizeof(long int), 1, output_file);
fwrite(&BitmapFileHeader.bfReserved1, sizeof(short int), 1, output_file);
fwrite(&BitmapFileHeader.bfReserved2, sizeof(short int), 1, output_file);
fwrite(&BitmapFileHeader.bfOffBits, sizeof(long int), 1, output_file);

fwrite(&BitmapInfoHeader.biSize, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biWidth, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biHeight, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biPlanes, sizeof(short int), 1, output_file);
fwrite(&BitmapInfoHeader.biBitCount, sizeof(short int), 1, output_file);
fwrite(&BitmapInfoHeader.biCompression, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biSizeImage, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biXPelsPerMeter, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biYPelsPerMeter, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biClrUsed, sizeof(long int), 1, output_file);
fwrite(&BitmapInfoHeader.biClrImportant, sizeof(long int), 1, output_file);

// write out the data bits
fwrite(&aBitmapBits[0], sizeof(char), NumOfBytes, output_file);

fclose(output_file);
}

bool ReadGraphicFromFile = true; // set manually to test different methods

if (ReadGraphicFromFile)
{
// The code below works fine, but it displays the bitmap by reading
// it from the Junk.bmp file that was written.
CString szFilename("junk.bmp");

hBmp = (HBITMAP)::LoadImage(NULL,szFilename,IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
}
else
{
// ***** Here's where the problem lies *****
// hBmp = (something else based on bitmap data from memory)
}

CBitmap bmp;
bmp.Attach(hBmp);

CDC bmDC;
bmDC.CreateCompatibleDC(&dialogDC);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

BITMAP bi;
bmp.GetBitmap(&bi);

dialogDC.StretchBlt(xLeft,yTop,xMaxWidth,yMaxHeight,&bmDC,0,0,
bi.bmWidth,bi.bmHeight,SRCCOPY);

bmDC.SelectObject(pOldbmp);

------------- End of code example ----------------


The creation of the bimap structure and writing out the file, I understand just fine.

All of the stuff after it, starting with LoadImage, I got from:
http://www.codeguru.com/Cpp/G-M/bitmap/displayingandsizing/article.php/c4905/
(Thanks to Ramakrishna Talla for that post)

Although there may be other ways to accomplish the same end, I figured if I could
create a bitmap in memory in the same form as the LoadImage gives me, with the "hBmp"
handle, then I would be all set.

I think that the answer may lie in CreateDIBSection, but the (void**)ppvbits thing
has got my brain all confused and I can't figure out how to get my data over to the
right place. I'm just flailing away with different things and all I'm getting is
a big black rectangle where I want to get a color graphic. I'd include some of that
code, but all I have is various snippets of code I've found on-line and it's a mess.

Can someone recommend some code to accomplish what I'm trying to do, or perhaps
point me to a reference for this?????

I've found tons of examples for using a bitmap resource, but only a couple that
explain loading from a file, and none (yet) that explain how to do what I'm trying
to do. I've found many that use CreateDIBSection and code similar I have above for
doing the outputing, but the step where the real data bits get copied or some
pointer gets re-specified seem to be omitted. Maybe this is assumed trivial and
because of my newbie ignorance I just can't make the leap to get there. This seems
like something that would be done a lot. Just last night I started looking into gaming
type books in the hopes that I can find something, but I haven't found it yet. Blush | :O (

Any help on this would be greatly appreciated.



AnswerRe: How to display bitmap bit data from memory? Pin
Shog929-Apr-05 14:25
sitebuilderShog929-Apr-05 14:25 
GeneralRe: How to display bitmap bit data from memory? Pin
mikec++29-Apr-05 14:56
mikec++29-Apr-05 14:56 
GeneralRe: How to display bitmap bit data from memory? Pin
mikec++29-Apr-05 16:00
mikec++29-Apr-05 16:00 
Generalnon-member function threading Pin
outoolcoe29-Apr-05 12:56
outoolcoe29-Apr-05 12:56 
Generalpipes of CreateProcess() Pin
includeh1029-Apr-05 11:20
includeh1029-Apr-05 11:20 
GeneralParsing XML file Pin
Reveur129-Apr-05 10:28
Reveur129-Apr-05 10:28 
GeneralDisplay a disabled 24-bit image Pin
eddya29-Apr-05 9:44
eddya29-Apr-05 9:44 
GeneralRe: Display a disabled 24-bit image Pin
bmzhao29-Apr-05 14:19
bmzhao29-Apr-05 14:19 
QuestionWhy make a function 'static'? Pin
Chris Meech29-Apr-05 9:35
Chris Meech29-Apr-05 9:35 
AnswerRe: Why make a function 'static'? Pin
David Crow29-Apr-05 9:43
David Crow29-Apr-05 9:43 
GeneralRe: Why make a function 'static'? Pin
Chris Meech2-May-05 2:11
Chris Meech2-May-05 2:11 
AnswerRe: Why make a function 'static'? Pin
BambooMoon29-Apr-05 9:50
BambooMoon29-Apr-05 9:50 
AnswerRe: Why make a function 'static'? Pin
Blake Miller29-Apr-05 10:44
Blake Miller29-Apr-05 10:44 
GeneralRe: Why make a function 'static'? Pin
Chris Meech2-May-05 2:16
Chris Meech2-May-05 2:16 
GeneralWMI Pin
Alex_Y29-Apr-05 9:01
Alex_Y29-Apr-05 9:01 
GeneralRe: WMI Pin
Alexander M.,29-Apr-05 9:04
Alexander M.,29-Apr-05 9:04 
GeneralRe: WMI Pin
Alex_Y2-May-05 2:12
Alex_Y2-May-05 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.