Solved

Win32 Programming (Drawing a Bitmap)

Posted on 1999-01-17
6
3,692 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 128
Template syntax for variable length arrays 9 75
How are Button Fonts Created? 2 31
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 39
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 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