bitmap display

bitmap display

Post by mark » Fri, 17 Dec 1999 04:00:00



I am trying to display bitmap images from a file to the screen and from
memory to the screen in Borland C++ for Windows version 3.1.  I have
been doing this in DOS with .PCX files and an SVGA.bgi driver.  Firstly
I set the screen resolution with the appropriate .BGI driver.  I then
unpack the .PCX file directly to the screen using putpixel() and set the
palette using the palette in the PCX file.  I am now trying to do a
similar thing in windows and find that windows doesnt use .BGI drivers
and doesnt have the putpixel() function.
        I have a manual that lists windows API functions for dealing with
bitmaps but it doesnt make any sense to me.  Could someone out there
either;
        (a) Tell me where I can get a library function that will load bitmaps
from a file into a memory buffer, and display this on the screen etc, or
        (b) Explain to me in simple terms the steps required to display an
image on the screen from a file, and how to store a bitmap from a file
into a memory buffer, when working with BorlandC for Windows.

        The images I wish to display are 8 bit monochrome and colour images.

Any help would be most appreciated.
Josh.

 
 
 

bitmap display

Post by Damon Chandle » Fri, 17 Dec 1999 04:00:00


Hi Josh,

Quote:>         I have a manual that lists windows API functions for dealing with
> bitmaps but it doesnt make any sense to me.
> (b) Explain to me in simple terms the steps required to display an
> image on the screen from a file, and how to store a bitmap from a file
> into a memory buffer, when working with BorlandC for Windows.

There are several API functions available to ease the process.  I understand
though, the API confused to all to well when I first delved into Windows
programming.  Fortunately, with some time and patience I was up to speed, as I'm
sure you will be.  To load a bitmap, the easiest thing to do is to use the
LoadImage() API function.  Here's a simple wrapper function that demonstrates
how this can be used...

HBITMAP LoadBitmapFile(char *filename)
{
    // use the LoadBitmap() API function to load the
    // bitmap file (returns a handle to the BITMAP)
    return LoadImage(HInstance, filename, IMAGE_BITMAP, 0, 0,
                     LR_LOADFROMFILE);

Quote:}

Here, HInstance is the handle to the instance of your application.  The zeros as
the fourth and fifth parameters tell the function to use the actual bitmap's
width and height.  The LR_LOADFROMFILE flag tells the function that the second
parameter is a filename.  The function returns a handle to a BITMAP (HBITMAP).

Ok, with the above function you can get the handle, now how to display it?  For
this, you can use the BitBlt() API function.  Unfortunately, this function takes
HDC parameters, not HBITMAP's.  HDC is a handle to a device context, which is
simply a Windows drawing surface.  You can grab a handle to a window's device
context by using the GetDC() API function, but for a bitmap, you'll need to use
a memory device context.  So, what needs to be done is to create a memory device
context (via CreateCompatibleDC), select the bitmap into the device context (via
SelectObject), then BitBlt() from the memory device context to the device
context of the window you want to display the bitmap on.  This takes care of the
HDC parameters of BitBlt(); the nXDest and nYDest define the location you want
to draw the bitmap at, while the nWidth and nHeight parameters specify the width
and height of the bitmap.  Well, you have an HBITMAP, but you don't have its
width and height.  To get these values, use the GetObject() API function which
will fill a BITMAP structure from its handle.  You can read the width and height
directly from the BITMAP structure.  Here's an example...

void DisplayBitmap(HWND HWnd, int X, int Y, HBITMAP HBitmap)
{
    // use the GetObject() API function to
    // retrieve the width and height of the bitmap
    BITMAP Bitmap;
    GetObject(HBitmap, sizeof(BITMAP), &Bitmap);
    int bitmap_width = Bitmap.bmWidth;
    int bitmap_height = Bitmap.bmHeight;

    // create a memory device context compatible
    // with the screen
    HDC HMemoryDC = CreateCompatibleDC(0);

    // select the bitmap into the memory device context
    HBITMAP HOldBitmap = SelectObject(HMemoryDC, HBitmap);

    // grab the handle to the device context of
    // the window whose handle is passed in as HWnd
    HDC Hdc = GetDC(HWnd);

    // block transfer the bits from the
    // memory DC to the screen    
    BitBlt(Hdc, X, Y,
           bitmap_width, bitmap_height,
           HMemoryDC, 0, 0,
           SRCCOPY);

    // clean up
    ReleaseDC(HWnd, Hdc);
    SelectObject(HMemoryDC, HOldBitmap);
    DeleteDC(HMemoryDC);

Quote:}

So when you actually use these functions, you'll do a process similar to...

HBITMAP HBitmap = LoadBitmapFile("C:\\Windows\\Images\\Bitmap1.bmp\0");
DisplayBitmap(Handle, 5, 5, HBitmap);
DeleteObject(HBitmap);

Hope this helps!

--------------------------------------
Damon Chandler

http://bcbcaq.freeservers.com
Answers to <Commonly Asked Questions>
52064929

 
 
 

1. bitmap display in power point

I am sure this is an easy question to most of this group, but I can't
figure it out, so I am hoping for some help.

What I want to do is take a circular logo in a bitmap image and insert
it into a power point presentation without the outline of the bitmap
outside of the logo itself showing.  Making the background transparent
doesn't work.  Believe I need to convert somehow to a .wmf, but I
can't figure out how to do this whtihout converting the entire
original rectangular bitmap.  Any advice would be appreciated.
Thanks.......joss

2. JF has moved to a new home!

3. ?: DOS raw b&w bitmap display?

4. Help. How I can open a GIF Animation File

5. Windows Bitmap Display Utility

6. Freelance opportunities

7. BITMAP DISPLAY PROGRAM

8. character demo tape

9. Bitmap Display Shutter

10. Fastest bitmap display method ???

11. Efficient bitmap display

12. Fast bitmap displaying...

13. Array->BITMAP(Display/Save/Print)