Solved

how to create, draw and display a bitmap in C/C++ and WinAPI?

Posted on 2001-08-06
9
912 Views
Last Modified: 2013-12-03
hi... I'm strugling to display a bitmap - I've read a lot of tutorials and books so don't send me to any... I would just like someone to write me a few lines of code for creating, and displaying a bitmap in windows (win98) using visualC++ (6.0) (no MFC - clean WinAPI please)

//I want to create an array:
//size is 64x64 and 3 colors (RGB)
unsigned char screen[64][64][3];

now I want to create a bitmap from this array and display it on the screen in a single window... how do I do that?
0
Comment
Question by:wrewers
  • 4
9 Comments
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 150 total points
ID: 6357262
//to create a bitmap you will need a device context to draw it in
HDC hDC = ::CreateCompatibleDC(NULL); // create a DC that is compatible with the screen

// then you need a DC to draw on
HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC,64,64);

// then you need to select it into your DC
HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hDC,hBitmap);

// now you can draw on it
for (i=0;i<64;i++)
  for (j=0;j<64;j++)
    ::SetPixel(i,j,RGB(screen[i][j][0],screen[i][j][1],screen[i][j][2]));

// then you select in the old bitmap
::SelectBitmap(hDC,hOldBitmap);

// delete old DC
::DeleteDC(hDC);


now hBitmap contains your image which you can do with what you will - the above will create a bitmap that has colours that match your screen - if you have limited colours you may have to play with palettes


0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6357270
oops typo I missed out the hDC in SetPixel - - but the jist is there
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6357294
to put the bitmap you have create onto a screen you will need a window to place it in and you wil need to know its HDC

then you can do - pobably in response to an WM_PAINT message

//to create a bitmap you will need a device context to draw it in
HDC hDC = ::CreateCompatibleDC(hWindowDC); // create a DC that is compatible with the window

// then you need to select it into your DC
HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hWindowDC,hBitmap);

// display the bitmap
::BitBlt(hWindowDC,0,0,64,64,hDC,0,0,SRC_COPY);

// now we tidy up
// then you select in the old bitmap
::SelectBitmap(hDC,hOldBitmap);

// delete old DC
::DeleteDC(hDC);


The one thing you must remember about windows and drawing is that you must tidy up - windows is very unforgiving and you will suffer resource leaks and lack of resources very quickly if you are not careful
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6357300
oops typo again - I really should have used a proper editor for this


> HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hWindowDC,hBitmap);

should be

HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hDC,hBitmap);



0
 
LVL 5

Assisted Solution

by:FengYuan
FengYuan earned 150 total points
ID: 6358225
const int width  = 64;
const int height = 64;
const int stride = ( width * 3 + 3 ) / 4 * 4;

unsigned char screen[height][stride];

for (int y=0; y<height; y++)
   for (int x=0; x<width; x++)
   {
      screen[y][x*3]   = blue;
      screen[y][x*3+1] = green;
      screen[y][x*3+2] = red;
   }

BITMAPINFO bmi = { { sizeof(BITMAPINFO), width, height,
         1, 24 } };

StretchDIBits(hDC, x, y, width, height, ...);

www.fengyuan.com
0
 

Expert Comment

by:SneWs
ID: 7737006
I Could send you an Complet set of displaying and creating bitmaps in WinAPI Style...

I Use it for my Games...

You can read bitmaps from File and from Resource, and even create your own

Send me an email if you are interested: snewss@hotmail.com

you can give me the poits after you'vd gotten the file

It is wery easy to use...

0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

685 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