Solved

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

Posted on 2001-08-06
9
875 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

15 Experts available now in Live!

Get 1:1 Help Now