[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2001-08-06
9
Medium Priority
?
1,162 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
6 Comments
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 600 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 600 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

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

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…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

834 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