Question

I'm using C++ Builder XE3 to develop a graph editor. All of the editing and drawing capabilities are made in a DLL that is loaded by the end user applications. To store information about the available graph objects I use a SQLite database. That database contains BMP icons that are loaded into a TImageList at run-time.

Everything works fine with Win-7, Win-8 and Win-vista but with Win-XP a "Floating point division by 0" occurs when loading the bitmap. I use a temporary memory stream to load the blob from the database and then load it into a temporary TBitmap which is used to add the new icon into the final TImageList.

Here is the function used to do so...

void TIcons::AddMaskedBitmap( TImageList *ptImgList, unsigned char *pucIcon, unsigned int uiSize )
{
    TMemoryStream *ptMemStream;

    //  Use a memory stream
    ptMemStream = new TMemoryStream();
    ptMemStream->Write( pucIcon, uiSize );
    ptMemStream->Position = 0;//Seek( ( int )0, ( unsigned short )soBeginning );

    //  Load using the cached bmp object
    m_ptBmp->Transparent = true;
#warning "floatting point division by 0 error with WinXP"
    m_ptBmp->LoadFromStream( ptMemStream ); //  floatting point division by 0 error with WinXP
//  m_ptBmp->LoadFromFile( ".\\d.bmp" );    //  works

    //  Create a mask
    m_ptBmpMask->Assign( m_ptBmp );
    m_ptBmpMask->Canvas->Brush->Color = m_ptBmp->TransparentColor;
    m_ptBmpMask->Monochrome = true;

    //  Add it to the list
    ptImgList->Add( m_ptBmp, m_ptBmpMask );

    //  Free mem
    m_ptBmp->FreeImage();
    m_ptBmpMask->FreeImage();
    delete ptMemStream;
}

I've traced the TBitmap::LoadFromStream function and the exception occurs in the CreateDIBSection function.

To make sure the loaded bitmap files are saved using the right encoding I've tried to load them using the TBitmap::LoadFromFile function and it works fine, so I think there's something wrong with the TBitmap::LoadFromStream function but I can't figure out what !

If anyone has an idea... Thanks.

Was it helpful?

Solution

LoadFromFile is implemented by creating a file stream, and passing that to LoadFromStream. This, if the contents of your memory stream are the same as the contents of the file, then the call to LoadFromStream will succeed.

Thus the only sane conclusion is that the contents of the memory stream are invalid in some way.

OTHER TIPS

The bitmap stored into the database is encoded using the BITMAPV4HEADER structure which is supposed to be supported since Win95/NT4 but there's something wrong.

It works fine if I encode the bitmap using the BITMAPINFOHEADER structure which is an older version of bitmap encoding which does not contain color space information.

Just found out a solution that works for me.

My problem was that software that was developed on Win7, when running on XP was throwing the division by 0 error when loading one of my BMPs.

It turns out that the problematic BMP was saved using Win7 Paint (other BMPs that were ok were saved from Gimp).

All I needed to do to fix it was to open this BMP on XP Paint and save it from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top