none
Getting a Bitmap into Metafile

    Question

  • Hello coders,

     

    Could you help me find the best way to get a bitmap image into an enhanced metafile? I am not very familiar with Windows graphics and have read dos and examples until my brain is jelly. Below I describe what I have figured out so far and the questions I can't resolve. If anyone wants to answer, I would very much appreciate it.

     

    The image is a 2-d barcode, consisting of square b/w "pixels". I have an array of bits representing these pixels. I want to end up with an enhanced metafile that 1) shows the barcode pixels as square blocks (consisting of the necessary number of screen pixels), 2) is portable to any other screeen or printer, displaying at the same absolute size, and 3) is as compact as possible.

     

    To make the emf compact, I thought drawing a bitmap into it was the best approach. If I use drawing commands there will typically be about 9000 MoveTo and LineTo pairs vs. 9000 bits.

     

    Q1) Does this approach make sense? Can the metafile store and replay an image as a small bitmap?

     

    Here's the pseudo code that "works":

     

    malloc - make the data area for a bitmap

    set the contents to white

    CreateBitmap - make b/w bitmap from empty data, size of barcode 

     

    Q2) Do I have to malloc memory just to create an empty HBITMAP, if CreateBitmap merely copies the data? Any way to create an empty bitmap of a specified size?

     

    CreateCompatibleDC

    SelectObject - get the bitmap into abouve HDC

    Set the black barcode "pixels" with SetPixelV

     

    CreateEnhMetaFile - size RECT is absolute size of barcode, DC is NULL.

     

    Q3) If emf is completely "device independent", why does it need a "reference DC"?  Docs say, "to record the resolution and units of the device on which a picture originally appeared". But I specifically *do not* want it to remember anything about the original device: I understood that device independence was the piurpose of the emf! So how does this "reference DC" affect playback on a different system?

     

    StretchBlt - copy from the barcode DC to the metafile DC. Barcode RECT is in pixels; emf RECT is in absolute units (LOENGLISH)

     

    CloseEnhMetaFile

     

    PlayEngMetaFile - to screen. It works.

     

    Q4) Does this sequence store the b/w (1 bit) bitmap into the emf? That is the result I want. Or does it store a much larger bitmap that is scaled to my "reference DC"?

     

    Q5) Can I use SelectObject to place the barcode BitMap directly into the emf DC? This seems to fail (no image when it is predrawn, nor when pixels are set in the emf DC). IOW, is there a way to skip the step of copying with StretchBlt and instead place or draw the bitmap directly into the emf DC?

     

    Q6) When playing the emf, I truly want "device independence"; in particular, for it to display with the original size and proportions. Is the player expected to first query the emf to get the rclFrame, and then draw it into the equivalent rectangle on the target device? Or does this happen automatically?

     

    Thanks for any light you can shed on these questions!

     

    Doug

     

     

     

     

     

     

    Friday, May 23, 2008 4:57 PM