Solved

Win32 Programming (Drawing a Bitmap)

Posted on 1999-01-17
6
3,640 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 100 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
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 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

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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

947 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

22 Experts available now in Live!

Get 1:1 Help Now