?
Solved

Win32 Programming (Drawing a Bitmap)

Posted on 1999-01-17
6
Medium Priority
?
3,812 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

801 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