将透明 png 图像转换为位图时,它不保留透明度吗?

发布于 2024-11-01 08:25:23 字数 1272 浏览 1 评论 0原文

我希望有人可以帮助我解决这个问题。我目前正在使用 ImageMagick.NET 包装器将透明 png 图像转换为位图(我们这样做是因为我们需要 c++ imageMagick 附带的附加图像处理功能)。现在一切都很好,除了当我使用其中有透明度的图像时。当我进行转换时,透明区域往往会变成黑色。现在这是我的问题,正在进行转换的代码是用 c++ 编写的,并且发现很难弄清楚它在做什么。请参阅下面的代码:

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;
    }

任何人都可以帮助破译 C++ 代码或为我指出正确的方向吗?任何信息都会有很大的帮助。

非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2024-11-08 08:25:23

位图不支持透明度。一般来说,你可以反过来做。您可以让 BMP 选择一种表示透明的颜色,并且在转换为支持透明度的格式时,该颜色将变为透明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文