如何将 GDI 图像* 转换为位图*

发布于 2024-07-27 04:57:11 字数 736 浏览 2 评论 0 原文

我正在用 c++、gdi+ 编写代码。

我利用 Image 的 GetThumbnail() 方法来获取缩略图。 但是,我需要将其转换为 HBITMAP。 我知道下面的代码可以获取GetHBITMAP:

Bitmap* img;
HBITMAP temp;
Color color;
img->GetHBITMAP(color, &temp); // if img is Bitmap*  this works well。

但是如何快速将Image*转换为Bitmap*呢? 非常感谢!

实际上,现在我必须使用以下方法:

int width = sourceImg->GetWidth(); // sourceImg is Image*
int height = sourceImg->GetHeight();
Bitmap* result = new Bitmap(width, height,PixelFormat32bppRGB);
Graphics gr(result);
//gr.SetInterpolationMode(InterpolationModeHighQuality);
gr.DrawImage(sourceImg, 0, 0, width, height);

我真的不知道为什么他们不提供 Image* - > 位图*方法。 但让 GetThumbnail() API 返回一个 Image 对象......

I am writting code in c++, gdi+.

I make use of Image's GetThumbnail() method to get thumbnail.
However, I need to convert it into HBITMAP.
I know the following code can get GetHBITMAP:

Bitmap* img;
HBITMAP temp;
Color color;
img->GetHBITMAP(color, &temp); // if img is Bitmap*  this works well。

But how can I convert Image* into Bitmap* fast?
Many thanks!

Actually, now I have to use the following method:

int width = sourceImg->GetWidth(); // sourceImg is Image*
int height = sourceImg->GetHeight();
Bitmap* result = new Bitmap(width, height,PixelFormat32bppRGB);
Graphics gr(result);
//gr.SetInterpolationMode(InterpolationModeHighQuality);
gr.DrawImage(sourceImg, 0, 0, width, height);

I really don't know why they do not provide Image* - > Bitmap* method. but let GetThumbnail() API return a Image object....

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

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

发布评论

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

评论(3

揽月 2024-08-03 04:57:12
Image* img = ???;
Bitmap* bitmap = new Bitmap(img);

编辑:
我正在查看 GDI+ 的 .NET 参考,但以下是 .NET 实现该构造函数的方式。

using (Graphics graphics = null)
{
    graphics = Graphics.FromImage(bitmap);
    graphics.Clear(Color.Transparent);
    graphics.DrawImage(img, 0, 0, width, height);
}

所有这些功能都可以在 GDI+ 的 C++ 版本中使用

Image* img = ???;
Bitmap* bitmap = new Bitmap(img);

Edit:
I was looking at the.NET reference of GDI+, but here is how .NET implements that constructor.

using (Graphics graphics = null)
{
    graphics = Graphics.FromImage(bitmap);
    graphics.Clear(Color.Transparent);
    graphics.DrawImage(img, 0, 0, width, height);
}

All those function are avaliable in the C++ version of GDI+

甜尕妞 2024-08-03 04:57:12

首先,您可以尝试 dynamic_cast,因为在许多情况下(如果不是大多数 - 至少在我的用例中),Image 是由 Bitmap 实现的。 因此

Image* img = getThumbnail( /* ... */ );
Bitmap* bitmap = dynamic_cast<Bitmap*>(img);
if(!bitmap)
    // getThumbnail returned an Image which is not a Bitmap. Convert.
else
    // getThumbnail returned a Bitmap so just deal with it.

,如果不知何故它不是(bitmap 将是 nullptr),那么您可以尝试更通用的解决方案。

例如,使用 Save 方法将 Image 保存到 COM IStream,然后使用 Bitmap::FromStream 从该流创建Bitmap

可以使用 COM IStream “nofollow noreferrer”>CreateStreamOnHGlobal WinAPI 函数。 然而,这效率不高,尤其是对于较大的流,但要测试它的想法。

还有其他类似的解决方案可以通过阅读 Image位图 文档。

遗憾的是,我还没有亲自尝试过(使用 Image ,它不是 Bitmap),所以我不完全确定。

First you may try dynamic_cast as in many cases (if not most - at least in my use cases) Image is being implemented by Bitmap . So

Image* img = getThumbnail( /* ... */ );
Bitmap* bitmap = dynamic_cast<Bitmap*>(img);
if(!bitmap)
    // getThumbnail returned an Image which is not a Bitmap. Convert.
else
    // getThumbnail returned a Bitmap so just deal with it.

However if somehow it is not (bitmap will be nullptr) then you could try a more general solution.

For example save the Image to a COM IStream using Save method and then use Bitmap::FromStream to create Bitmap from that stream.

A simple COM IStream could be created using the CreateStreamOnHGlobal WinAPI function. However this is not efficient, especially for larger streams, but to test the idea it will do.

There are also other similar solutions which can be deduced from reading Image and Bitmap documentation.

Sadly I haven't tried it on my own (with Image which is not a Bitmap) so I am not entirely sure.

秋凉 2024-08-03 04:57:12

据我所知,您只需创建一个位图并将图像绘制到其中:

Bitmap* GdiplusImageToBitmap(Image* img, Color bkgd = Color::Transparent)
{
    Bitmap* bmp = nullptr;
    try {
        int wd = img->GetWidth();
        int hgt = img->GetHeight();
        auto format = img->GetPixelFormat();
        bmp = new Bitmap(wd, hgt, format);
        auto g = std::unique_ptr<Graphics>(Graphics::FromImage(bmp));
        g->Clear(bkgd);
        g->DrawImage(img, 0, 0, wd, hgt);
    } catch(...) {
        // this might happen if img->GetPixelFormat() is something exotic
        // ... not sure
    }
    return bmp;
}

As far as I can tell you have to just create a bitmap and paint the image into it:

Bitmap* GdiplusImageToBitmap(Image* img, Color bkgd = Color::Transparent)
{
    Bitmap* bmp = nullptr;
    try {
        int wd = img->GetWidth();
        int hgt = img->GetHeight();
        auto format = img->GetPixelFormat();
        bmp = new Bitmap(wd, hgt, format);
        auto g = std::unique_ptr<Graphics>(Graphics::FromImage(bmp));
        g->Clear(bkgd);
        g->DrawImage(img, 0, 0, wd, hgt);
    } catch(...) {
        // this might happen if img->GetPixelFormat() is something exotic
        // ... not sure
    }
    return bmp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文