views:

743

answers:

2

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?

A: 

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.

Tal Pressman
+1  A: 

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.

Scott Smith