Click here to Skip to main content
15,921,660 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
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 
Thanks to shog9's response, I have the answer to my question. I have combined his solution
into my original code example so others my not have to dig quite a long and deep as I have
been.

Please be aware that I am a hack a total newbie to C++. I'm using Visual C++ 6.0.
Any mistakes are mine.

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 was able
to figure out how to write the created bitmap out to a BMP file, then re-load the same file
and finally display the graphic. I wanted to be able to just display the graphic directly,
instead of first going to/from disk. With the info I got from shog9, I was able to
accomplish this goal. So, I am creating a bitmap graphic on-the-fly and then displaying it.

-------------------- Example Code ----------------

// 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 are determined:
// NumOfBytes, NumCols, NumRows of the bitmap
// xLeft, yTop = coordinates to draw bitmap at
// xMaxWidth, yMaxHeight = size of display area
//

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 and write file "junk.bmp"
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);
}

HBITMAP hBmp; // This is the target bitmap
HDC dcMap = CreateCompatibleDC(NULL);
ASSERT(dcMap);

// Set ReadGraphicFromFile manually & recompile to test different methods
// true = use LoadImage to get bitmap from file
// false = use CreateDIBSection and move data from memory

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

if (ReadGraphicFromFile)
{
// Load the bitmap data from file "junk.bmp", which was saved above.
CString szFilename("junk.bmp");

hBmp = (HBITMAP)::LoadImage(NULL,szFilename,IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
}
else
{
// Here's the code I got from shog9 (modified to work here), including
// his explanations:

// DIBSections are pretty easy. You specify what you want (using a BITMAPINFO
// structure, like you already are), and Windows gives you memory that you can
// write to. You just pass in a pointer to the pointer you want to use to
// reference this:

unsigned char* phBmpBits = NULL;
hBmp= CreateDIBSection( dcMap,&infoMap,DIB_RGB_COLORS,
(void**)&phBmpBits,NULL,0 );

// Then you just need to copy your bitmap data into the memory Windows allocated
// for you:

memcpy(phBmpBits, &aBitmapBits, (NumCols * NumRows * 3));

// And you're ready to do something fun with your new bitmap. Don't forget to
// delete the bitmap when you're done with it though (using DeleteObject())...
// and that'll free the memory used as well, so don't be using that pointer
// afterwards. Enjoy!

}

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);

DeleteObject(hBmp);

------------- End of Example Code ----------------

I'm probably missing some more memory housekeeping here, so be careful!
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 
GeneralRe: WMI Pin
David Crow29-Apr-05 9:35
David Crow29-Apr-05 9:35 
GeneralRe: WMI Pin
Alex_Y2-May-05 2:11
Alex_Y2-May-05 2:11 
GeneralEasy Way to Hide Taskbar Button Pin
Anonymous29-Apr-05 8:49
Anonymous29-Apr-05 8:49 

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.