Solved

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

Posted on 2001-08-06
9
879 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now