Board index » cppbuilder » Read bitmap file

Read bitmap file

when I click a button, and Image1 will be shows a bitmap.
But Image1 alway show White color.
What wrong this code is below? and correct code?
(Image1, Button1, Form1 & a bitmap_file)

void __fastcall TForm1::Button1Click(TObject *Sender)
{
char cdir[MAX_PATH];
char *bfile = "//BIT-8-256.bmp";
::GetCurrentDirectory( MAX_PATH, cdir);
strcat( cdir, bfile);

BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;

HANDLE HFile = ::CreateFile(cdir, GENERIC_READ,
                            0, NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL, NULL);
//
if (HFile != INVALID_HANDLE_VALUE)
   {
   DWORD dwRead;
   ::ReadFile(HFile, &bmfh, sizeof(BITMAPFILEHEADER), &dwRead, NULL);
   ::ReadFile(HFile, &bmih, sizeof(BITMAPINFOHEADER), &dwRead, NULL);
   // here is crux!!! but can you help me?
   HDC hdc = ::GetDC(HFile);
   ::BitBlt(hdc, 0, 0,
            bmih.biWidth, bmih.biHeight,
            Image1->Canvas->Handle, 0, 0,
            SRCCOPY);

   Caption = "Succeed for read bitmap file!";
   }
else
   Caption = "fail for read bitmap file!";

::CloseHandle(HFile);

Quote
}

 

Re:Read bitmap file


Hi Mimi,

I susspect you use invalid HDC for BitBlt function:
[snip]

Quote
>    ::BitBlt(hdc, 0, 0,   bmih.biWidth, bmih.biHeight,
>             Image1->Canvas->Handle, 0, 0,
>             SRCCOPY);

Source HDC should be created from bitmap you have loaded and you have
used
file handle for creating HDC.
You have read the file containg image, but I don't see you have get
HBITMAP type handle:

HDC hdc = CreateCompatibleDC(NULL);
SelectObject(hdc, your_bitmap_handle);

Hope this helps,
Sinisa Petric
http://www.sigmapi-design.com

Re:Read bitmap file


Quote
"Mimi" <cm...@pchome.com.tw> wrote in message news:3b4ec539_1@dnews...

<snip>

Quote
>    ::ReadFile(HFile, &bmfh, sizeof(BITMAPFILEHEADER), &dwRead, NULL);
>    ::ReadFile(HFile, &bmih, sizeof(BITMAPINFOHEADER), &dwRead, NULL);

You have only extracted the header information.  You still need to extract
the pixel and (possibly) palette data from the file and then create an
HBITMAP.

If you need an example, then download the BMGLib project from my web site.
The "Projects\BMGLib\BMGLib_DLL\bmprw.c" file shows you how to extract data
from a BMP file.  There are other functions that show you how to convert the
data to a HBITMAP and display the bitmap with the VCL.

--
Regards,
Scott
http://members.home.net/scottheiman/

Other Threads