About GDI/GDI+ coordinate compatibility?

前端 未结 4 1735
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 03:49

I have a problem while drawing with both GDI and GDI+ interchangeably. The page transformation—in particular scaling—seems to be a little bit off between the two. Wh

4条回答
  •  花落未央
    2021-02-04 04:26

    I found a workaround for the issues when printing. Note that the graphics object in the example doesn't set any world space transformations, so drawing is done directly in page space.

    Setting the page units to inches, then converting coordinates to inches seems to fix the drawing issues without much additional work. Tested with display and printer DCs at different DPIs (ranging from 72 to 4000.)

    Gdiplus::Graphics graphics(..);
    Gdiplus::RectF    rect(0.0f, 0.0f, 1.0f, 1.0f);
    Gdiplus::REAL     dpiX = graphics.getDpiX();
    Gdiplus::REAL     dpiY = graphics.getDpiY();
    
    /* Logical coordinates to inches. In this example, the window extents are
       equal to the DC's DPI. You will have to convert to inches based on your
       specific configuration. */
    rect.X      /= dpiX;
    rect.Y      /= dpiY;
    rect.Width  /= dpiX;
    rect.Height /= dpiY;
    
    graphics.SetPageUnit(Gdiplus::UnitInch);
    graphics.FillRectangle(.., rect);
    

提交回复
热议问题