Improve company productivity with a Business Account.Sign Up

x
?
Solved

Win32 Programming (Drawing a Bitmap)

Posted on 1999-01-17
6
Medium Priority
?
3,989 Views
Last Modified: 2013-12-03
I want to draw a Bitmap on Screan not using MFC
(I use visual C++ 5.0 )

So hire is my problem: I want to load a BMP and show it on my screan, hire is my code:

void DrawBitmap (HDC hdc, HBITMAP hBitmap, int xStart, int yStart)
     {
  BITMAP bm ;
  HDC hdcMem ;
  DWORD dwSize ;
  POINT ptSize, ptOrg ;

  hdcMem = CreateCompatibleDC (hdc) ;
  SelectObject (hdcMem, hBitmap) ;
  SetMapMode (hdcMem, GetMapMode(hdc)) ;

  GetObject (hBitmap, sizeof (BITMAP), (LPVOID) & bm) ;

  ptSize.x = bm.bmWidth ;
  ptSize.y = bm.bmHeight ;
  DPtoLP (hdc, &ptSize, 1) ;

  ptOrg.x = 0 ;
  ptOrg.y = 0 ;
  DPtoLP (hdcMem, &ptOrg, 1) ;

  BitBlt (hdc, 1, 1, ptSize.x, ptSize.y, hdcMem, ptOrg.x, ptOrg.y,
RCCOPY  ) ;
  DeleteDC (hdcMem) ;

    }

I then try to Draw it in WinProc:
      case WM_CREATE :                        
                        /*hwndEdit = CreateWindow ("edit", NULL, WS_CHILD | WS_VISIBLE | WS_OVERLAPPEDWINDOW  |
                                                                  WS_HSCROLL | WS_VSCROLL | ES_LEFT | ES_MULTILINE |
                                                                  ES_AUTOHSCROLL | ES_AUTOVSCROLL, 10, 10, 15, 15, hwnd, (HMENU) EDITID,
                                                                  ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;-*/
                        for(i=0;i>200;i++)      
                         for(ii=0;ii>200;ii++)      
                          BMP[i][ii]=2;
                        if(hBitmap = CreateBitmap (200,200,1,1,BMPpoint)) ;
                         else MessageBox (hwnd, "NO BMP","NONE", MB_ICONEXCLAMATION | MB_OK) ;    
                        //SetWindowText (hwndEdit, "Text Document") ;            
                        hdc2     = CreateDC ("DISPLAY", NULL, NULL, NULL) ;
                        DrawBitmap (hdc2, hBitmap, 10, 10) ;      
                        DeleteDC(hdc2) ;
                        
                        //BitBlt (hdc, 0, 0, 100, 100, hdc, 10, 20, BLACKNESS ) ;
                        CBFileInitialize(hwnd);                        
                        DrawBitmap (hdc2, hBitmap, 10, 10) ;      
                        
Anyhow it dos't work !!

What im I doing wrong, ?? My hBitmap dos't work welll, and I can't see why !!
0
Comment
Question by:win32
  • 3
  • 2
6 Comments
 

Accepted Solution

by:
casaprog earned 300 total points
ID: 1184415

Instead of using the WM_CREATE to draw the bitmap, use the WM_PAINT, where all the window painting must be done, and don't create the DC that way, use the given DC to do it, also you have to delete the bitmap object, like this:

WinProc....
{
PAINTSTRUCT ps;
static HBITMAP hBitmap;

case WM_CREATE:
  hBitmap = CreateBitmap(...);
break;

case WM_PAINT:
  BeginPaint(hWindow, &ps);
  DrawBitmap(ps.hdc, hBitmap, 0, 0);
  EndPaint(hWindow, &ps);
break;

case WM_DESTROY:
  DeleteObject(hBitmap);
break;
}

Best Regards,

Wenderson Teixeira
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184416
Your DrawBitmap() function als has some serious memory leaks.  When you select an object into a DC, the previous one is selected out.  When you are "given" a DC it has a buncyh of default objects (pens, brushes, birtmaps), you must make sure that when you are donw with the DC that these original objets are ale selected back.  They must be selected back before your destroy the DC.  ALso when you create an object, like a pen or bitmap, you must delete it.  Again, you must make sure that object is selected out of the DC before the object is deleted.
0
 

Author Comment

by:win32
ID: 1184417
Thats a start Yes! But the real problem is that I can't get the CreateBitmap(..) function to work right !. When running the program one pixel is displayed !!??
>>>Hire is the Code<<<<
WinProc()
tatic HBITMAP                   hBitmap;      //Handel to Draw static int                  BMP[200][200];
static char *BMPpoint=BMP;


Create:
for(int i=0;i<200;i++)      
 for(int ii=0;ii<200;ii++)      
  BMP[i][ii]=2;
hBitmap = CreateBitmap (200,200,16,8,&BMPpoint) ;
// I want 8bit per pixel ...



I think that i might not use the CreateBitmap function right !

0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
LVL 22

Expert Comment

by:nietod
ID: 1184418
One problem is that you are setting the color planes to 16 (3rd parameter).  Set it to 1.  I don't think any device supports 16 color planes.  In fact, I don't think any used today support more than 1.  
0
 

Author Comment

by:win32
ID: 1184419
1) Your DrawBitmap() function als....(from nietod)
    Yes I can see that's a problem! How do I solve problem ?,   what api's would I use ?

2) Ok I got my program to work win 32bit,16 colors, using WORD and DWORD to make the bitmapdata !. But what will I do if I want to use 24 bit display ??. A simple datatype like WORD24 dos not exist !. How do I solve that problem ??

3)Is it possible to make modifikations to the Palette like this:
SetPalRGB(int R, int G, int B, PalNr); in a 16,24,and 32 bit display enviroment ?

Thans for all the helpe that I have recived ! :-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184420
1) FYI I have no idea how that "als" got in there.

Take a look at SelectObject().  It returns an object of the type selected in.  If you select in one brush, it returns the brush that was previously in use.  Before you destroy your brush, you must get it out of the DC.  How are you going to get it out?  By selecting back in the brush that was  originaly there.  That also solves the other problem, the DC must have its original objects, before it is destroyed, so this returns its originial object, the brush, to it.  You must do this any time you use SelectObject(), even with bitmaps.  

2) work with arrays of bytes, like char.  You can figure out how many character to allocate based on the dimensions of the bitmap and the number of bits per pixel.  Like

new char[Width*Height*BitsPerPixel/8];

(Actually, the calculation is probably a bit more complex than that, because I beleive there may be restrictions on the row length, like each row of pixels must be even that sort of thing.)

3) Look at SetPaletteEntries().
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

595 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