Dib to Bitmap trouble

Dib to Bitmap trouble

Post by Chri » Fri, 13 Jun 2003 08:07:12



I am trying to convert a DIB to a bitmap...
TBitmap won't load CF_DIB format, so I found a function that converts it to
a bitmap file, so I converted it to write to a stream instead of a file,
then use LoadFromStream(), since all TBitmap::LoadFromFile() does anyway is
load a bitmap into a TFileStream, then use LoadFromStream()....

Problem is, the function I converted to begin with won't work, it only
creates a bitmap of the bottom half of the image. So, when using
LoadFromStream, it gives me a Stream Read error....

I got the code off of a Microsoft VC++ MSDN application called IconPro....
This code is un-edited.

It creates a bitmap, c:\dib.bmp, of the clipboard's CF_DIB format. (Of
course, lots of programs like PSP can be used to copy to the clipboard in
CF_DIB format)
The last function is a TButton press that uses the second to last function
that converts the DIB.
Here is 100% of my relevant code:

#define WIDTHBYTES(bits)      ((((bits) + 31)>>5)<<2)

DWORD BytesPerLine( LPBITMAPINFOHEADER lpBMIH )
{
    return WIDTHBYTES(lpBMIH->biWidth * lpBMIH->biPlanes *
lpBMIH->biBitCount);

Quote:}

/* End BytesPerLine()
********************************************************/

WORD DIBNumColors( LPSTR lpbi )
{
    WORD wBitCount;
    DWORD dwClrUsed;

    dwClrUsed = ((LPBITMAPINFOHEADER) lpbi)->biClrUsed;

    if (dwClrUsed)
        return (WORD) dwClrUsed;

    wBitCount = ((LPBITMAPINFOHEADER) lpbi)->biBitCount;

    switch (wBitCount)
    {
        case 1: return 2;
        case 4: return 16;
        case 8: return 256;
        default:return 0;
    }
    return 0;

Quote:}

WORD PaletteSize( LPSTR lpbi )
{
    return ( DIBNumColors( lpbi ) * sizeof( RGBQUAD ) );
Quote:}

/* End PaletteSize()
********************************************************/

/***************************************************************************
*
*
*     FUNCTION: WriteBMPFile
*
*     PURPOSE:  Writes a BMP file from CF_DIB format
*
*     PARAMS:   LPCTSTR szFileName - the name of the file to read
*               LPBYTE - pointer to the CF_DIB, NULL for failure
*
*     RETURNS:  BOOL - TRUE for success, FALSE for Failure
*
* History:
*                July '95 - Created
*
\***************************************************************************
*/
BOOL WriteBMPFile( LPCTSTR szFileName, LPBYTE lpDIB )
{
    HANDLE             hFile;
    BITMAPFILEHEADER    bfh;
    DWORD             dwBytes, dwBytesToWrite;
    LPBITMAPINFOHEADER lpbmih;

    // Open the file
    if( (hFile=CreateFile( szFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE )
    {
        MessageBox( NULL, "Error opening file", szFileName, MB_OK );
        return FALSE;
    }
    bfh.bfType = 0x4d42;
    bfh.bfReserved1 = 0;
    bfh.bfReserved2 = 0;
    bfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER )
+ PaletteSize( lpDIB );
    bfh.bfSize = (bfh.bfOffBits + ((LPBITMAPINFOHEADER)lpDIB)->biHeight *
BytesPerLine((LPBITMAPINFOHEADER)lpDIB))/4;
    // Write the header
    if( ( ! WriteFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytes,
NULL ) ) || ( dwBytes != sizeof( BITMAPFILEHEADER ) ) )
    {
        CloseHandle( hFile );
        MessageBox( NULL, "Error Writing file", szFileName, MB_OK );
        return FALSE;
    }
    lpbmih = (LPBITMAPINFOHEADER)lpDIB;
    lpbmih->biHeight /= 2;
    dwBytesToWrite = bfh.bfOffBits + (lpbmih->biHeight *
BytesPerLine(lpbmih));
    if( ( ! WriteFile( hFile, lpDIB, dwBytesToWrite, &dwBytes, NULL ) ) ||
( dwBytes != dwBytesToWrite ) )
    {
        CloseHandle( hFile );
        MessageBox( NULL, "Error Writing file", szFileName, MB_OK );
        return FALSE;
    }
    lpbmih->biHeight *= 2;
    CloseHandle( hFile );
    return TRUE;

Quote:}

/* End WriteBMPFile()
*******************************************************/

//Write the bmp file from the clipboard
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        OpenClipboard(Form1->Handle);

        HGLOBAL h = GetClipboardData(CF_DIB);
        if(h)
        {
                unsigned char* p = (unsigned char*)GlobalLock(h);
                if(p)
                {
                char filename[] = "C:\\dib.bmp";
                WriteBMPFile(filename,p);
                GlobalUnlock(h);
                }

        }

        CloseClipboard();

Quote:}

 
 
 

1. Trouble Rescaling a DIB

I want to scale a DIB on the fly without using API calls to do it (such
as StretchDIBits or StretchBLT) because I am moving components over the
image, and when the image is scaled by the API it causes horrible
redraw. I have tried a couple of different algorithms, and have had
weird problems, I was hoping someone could help me with. I am putting
the image on the screen with StretchDIBits during my testing so I can
see the entire image on the screen
When I want to make the image bigger I don't have any problem at all,
the problems come in when I try to make it smaller. The only scaling
percentage that fully works when I make it smaller  is 10%. No matter
what image or algorithm I was using 10% worked fine. (BTW I'm scaling
both the width and the height the same percentage) When I scale it 50%,
nothing comes on the screen, but if I then take that scaled image and
scale it to 200% The original image comes up. So I know that it is
scaling it to 50%, but for some reason it is not putting it on the
screen.
If anybody has any thoughts on this, or if anyone has any fast proven
scaling algorithms,  I'd love to hear them.
Thanks
Sim

2. pdd extention converter

3. dib bitmap

4. GREAT JOB$$

5. Need to divide a DIB to a DDB (bitmap ) files

6. Loading a Bitmap file into a DIB and device dependencies

7. convert a bitmap to DIB and print

8. displaying 24 bits DIB bitmap

9. DIB to bitmap

10. DIB to Bitmap

11. DIB and Bitmap question