Solved

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

Posted on 2001-08-06
9
955 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
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 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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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