如何转换 gdi+ 将类似位图的结构放入 HDC 中?

发布于 2024-07-29 01:58:10 字数 1375 浏览 5 评论 0原文

如何将类似位图的结构转换为 HDC?

我现在正在用c++、gdi编写图像处理程序。

如果我有HDC。 我可以通过以下代码在gdi中的HDC上绘制任何我喜欢的东西。

// HDC is handy.
HDC dc;
dc.DrawXXX       // I can draw using gdi method.
Graphics gr(dc); // now I can also draw on the dc using gdi+ method.

我的应用程序基于 FreeImage。 我用 fipImage 制作。 (使用像Bitmap这样的数据结构)

但是如果我想在fipWinImage上绘图,现在我必须将fipWinImage复制到Bitmap,然后在Bitmap上绘图,最后再次将位图转换为fipImage,这是消耗时间和内存的。

将 fipImage 转换为位图 -> 在位图上绘制 -> 将位图转换为 fipWinImage

fipWinImage imagefip;
Bitmap* tempImg = new Bitmap(imagefip->GetWidth(), imagefip.GetHeigt(), PixelFormat24bppRGB); // memory comsuming is image is large
Graphics *pGr = Graphics::FromImage(tempImg);

HDC dc = pGr->GetHDC();

RECT rec;
rec.left = 0;
rec.top = 0;
rec.right = imagefip.GetWidth();
rec.bottom = imagefip.GetHeight();
fipImage.draw(dc, rec); // using stretchdibits()
pGr->ReleaseHDC(dc);

Graphics gr(tempImg);
HDC dc = gr.GetHDC();         // Get an Hdc, draw using gdi method
gr.ReleaseHDC(tempDC);     //
gr.drawXXX                            // Draw using gdi+ method.


fipWinImage fipImg;             // final result fipWinImage.
HBITMAP temp;
Color color;
tempImg->GetHBITMAP(color, &temp);
fipImg->copyFromBitmap(temp);

我想直接从 fipImage 构造 HDC。 并直接在 fipWinImage 上绘图 我怎样才能做到这一点?

How to Convert a Bitmap-like struct into an HDC?

I am now writting image processing program in c++, gdi.

If I got a HDC.
I can draw whatever I like on the HDC in gdi by the following code.

// HDC is handy.
HDC dc;
dc.DrawXXX       // I can draw using gdi method.
Graphics gr(dc); // now I can also draw on the dc using gdi+ method.

My Application is based on FreeImage.
I make of fipImage. ( use data struct like Bitmap )

However if I want to draw on fipWinImage, now I have to copy fipWinImageto Bitmap, then draw on the Bitmap, and finally convert the bitmap into fipImage again, which is time comsuming and memory comsuming.

Convert fipImage to Bitmap -> Draw on the bitmap -> convert bitmap to fipWinImage

fipWinImage imagefip;
Bitmap* tempImg = new Bitmap(imagefip->GetWidth(), imagefip.GetHeigt(), PixelFormat24bppRGB); // memory comsuming is image is large
Graphics *pGr = Graphics::FromImage(tempImg);

HDC dc = pGr->GetHDC();

RECT rec;
rec.left = 0;
rec.top = 0;
rec.right = imagefip.GetWidth();
rec.bottom = imagefip.GetHeight();
fipImage.draw(dc, rec); // using stretchdibits()
pGr->ReleaseHDC(dc);

Graphics gr(tempImg);
HDC dc = gr.GetHDC();         // Get an Hdc, draw using gdi method
gr.ReleaseHDC(tempDC);     //
gr.drawXXX                            // Draw using gdi+ method.


fipWinImage fipImg;             // final result fipWinImage.
HBITMAP temp;
Color color;
tempImg->GetHBITMAP(color, &temp);
fipImg->copyFromBitmap(temp);

I want to construct a HDC directly from fipImage. and draw directly on fipWinImage
How can I do this?

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

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

发布评论

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

评论(2

大海や 2024-08-05 01:58:10

首先需要澄清一些:

设备上下文基本上是一种结构,可以记住前景色和背景色、画笔、字体信息和物理绘图表面(位图)等内容。

这是一件很方便的事情,这样当您进行一个又一个的图形操作时,就不必继续指定所有这些事情。 您还可以通过这种方式更轻松地传递所有这些设置。 这就是 DC 的全部内容 - 只是绘图参数的集合,包括要绘制的表面。

“HDC”只是这些结构之一的句柄(引用)。 作为“句柄”,窗口可以在内存中移动结构体来管理可用空间,而不会弄乱指向它的指针。

如果您有权访问所使用的库的源代码,请检查 fipWinImage::draw(...) 方法。 如果他们使用 StretchDIBits,那么他们必须在某个时候将原始位图数据转换为兼容的格式。 fipWinImage 类也可能包装底层 BITMAP 或 DIB 等。

获得自己的 HDC 的最后一步...

位图被“选择”到设备上下文中,并且只能一次选择到单个 DC 中时间。 如果您可以从 fipWinImage 获取内部 HBITMAP,则可以将其选择到另一个 DC(假设它尚未选择到另一个 HC)。

当您创建 DC 时,Windows 会自动为其创建一个 1x1 位图(因为 DC 必须始终具有选定的位图)。 当您选择新位图时,您将获得返回给您的先前选择的位图的句柄。 请坚持下去,因为完成后您需要将其放回去。

希望有帮助。

First a few clarifications:

A Device Context is basically a structure that remembers things like foreground and background colors, brushes, font info, and the physical drawing surface (bitmap).

This is a handy thing, so that you don't have to keep specifying all of these things when you're doing one graphics operation after another. You can also pass all of these settings around more easily this way. That's all that a DC really is - just a collection of drawing parameters, including the surface to draw upon.

An "HDC" is just a handle (reference) to one of these structs. Being a "Handle" lets window move the struct around in memory to manage free space without your pointers to it getting messed up.

If you have access to the source code for the library you're using, examine the fipWinImage::draw(...) method. If they're using StretchDIBits, then they must get their raw bitmap data into a compatible format at some point. It's also possible that the fipWinImage class is wrapping an underlying BITMAP or DIB, etc.

The final step to getting your own HDC...

A bitmap is "SELECTED" into a device context, and can only be selected into a single DC at one time. If you can get the internal HBITMAP from fipWinImage, you can select it into another DC (assuming that it isn't still selected into another HC).

When you create a DC, windows automatically creates a 1x1 bitmap for it (since a DC must have a selected bitmap at all times). When you select in a new bitmap, you get the handle to the previously selected bitmap returned to you. Hang on to that, because you're going to need to put it back when you're done.

Hope that helps.

岁吢 2024-08-05 01:58:10

我不知道 FreeImage,但如果您可以从中获取指向实际像素数据(DIB 部分)的指针,您可以创建一个共享它的 HBITMAP,而不必每次都复制数据。

I don't know FreeImage, but if you can get a pointer to the actual pixel data (DIB section) out of it, you could just create a HBITMAP that shares it without having to copy the data every time.

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