Bobby
Bobby

Reputation: 1604

When convering a transparent png image to bitmap its not retaining the transparency?

I was hoping some one could help me with this problem. I am currently using ImageMagick.NET wrapper to convert transparent png images to bitmap (we are doing this as we need the added image processing functionality that comes with c++ imageMagick). Now everything works great except when I use an image where there is transparency in it. The transparent area tends to turn into black when I do the conversion. Now here is my problem the code that is doing the conversion is in c++ and finding it hard to work out what it is ecavtly doing. Please see the code below:

System::Drawing::Bitmap^ Image::ToBitmap()
    {
        System::Drawing::Bitmap^ bitmap = 
            gcnew System::Drawing::Bitmap(image->columns(), image->rows(), 
            System::Drawing::Imaging::PixelFormat::Format24bppRgb);

        System::Drawing::Imaging::BitmapData^ bitmapData = bitmap->LockBits( 
            System::Drawing::Rectangle(0,0,image->columns(), image->rows()), 
            System::Drawing::Imaging::ImageLockMode::ReadWrite,
            System::Drawing::Imaging::PixelFormat::Format24bppRgb);

        unsigned char *ptr = (unsigned char *) bitmapData->Scan0.ToPointer();
        std::string map = "BGR";
        for( int i=0; i<image->rows(); i++ )
        {
            image->write(
                0, i, image->columns(), 1, map,
                MagickCore::CharPixel, (void *)ptr);
            ptr += bitmapData->Stride;
        }

        bitmap->UnlockBits(bitmapData);
        return bitmap;
    }

Can any one help be decipher the c++ code or point me to the right direction. Any info will be a great help.

Many Thanks.

Upvotes: 0

Views: 1079

Answers (1)

RedX
RedX

Reputation: 15165

Bitmaps do not support transparency. Generally you do it the other way around. You have a BMP select a color that means transparent and when converting to a format that supports transparency that color is made transparent.

Upvotes: 4

Related Questions