Note that there are some explanatory texts on larger screens.

plurals
  1. POAbout GDI/GDI+ coordinate compatibility?
    primarykey
    data
    text
    <p>I have a problem while drawing with both GDI and GDI+ interchangeably. The page transformation&mdash;in particular scaling&mdash;seems to be a little bit off between the two. Which properties of the GDI context affects the scaling of the output other than <code>SetViewportExt</code> and <code>SetWindowExt</code>?</p> <p>The code uses almost exclusively GDI for its drawing, but uses GDI+ in a few cases where its features (semitransparency) are needed. It uses <code>SetViewportExt</code>, <code>SetWindowExt</code> and <code>SetViewportOrg</code> to enable zooming and scrolling.</p> <p>When GDI+ is needed I construct a <code>Gdiplus::Graphics</code> object around the HDC and do the drawing. I assume this makes the graphics context wrap the device context and relay its rendering to the device context. If I extract the transformation matrix of the GDI+ graphics context, I see that it is the identity matrix, so the scaling is done elsewhere (in the device context I guess).</p> <p>I devised a simple test where I draw the same array of rectangles with GDI and GDI+ to be sure that all the transformations are the same in both cases. The code snippet follows:</p> <pre><code>CRect rect = ...; // Draw the rectangle using GDI CPen cpen(PS_DASH, 0, RGB(0,0,255)); pDC-&amp;gt;SelectObject(&amp;amp;cpen); pDC-&amp;gt;Rectangle(rect); { // Draw the rectangle using GDI+ Gdiplus::Graphics graphics(pDC-&amp;gt;m_hDC); Gdiplus::Pen pen(Gdiplus::Color(180,180,180)); graphics.DrawRectangle( &amp;amp;pen, Gdiplus::Rect(rect.left, rect.top, rect.Width(), rect.Height())); } </code></pre> <p>And the result is here: (the blue dashed is drawn by GDI and the gray is drawn by GDI+) </p> <p><a href="https://i.stack.imgur.com/2DZ6w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2DZ6w.png" alt="Result drawn by code"></a> </p> <p>I can clearly see that the two coordinate systems are different. I expected some round-off errors, but not a scaling error as seen here. Also, when I change the zoom factor, the GDI+ jumps around ±4 pixel in both directions depending on the zoom. This is also highlighted in the screenshot as the GDI+ rectangle has a positive offset on the X axis and a negative offset on the Y axis as compared to the GDI rectangle.</p> <ul> <li><p>Does anybody know what's going on here?</p></li> <li><p>How would I go about investigating/debugging this? This happens in the bowels of windows so I'm unfortunately unable to debug it.</p></li> </ul> <p>For reference, this is what my viewport/window org/ext looks like:</p> <pre><code>Window Ext: (134000, 80500) Window Org: (0, 0) Viewport Ext: (1452 872) Viewport Org: (35 35) </code></pre> <h3>Update:</h3> <p>I have fixed the problem, but it's not pretty. The basic approach is:</p> <ol> <li><p>Take two coordinates (origin and a second appropriate point) in screen space and transform them to logical coordinates using GDI (<code>DPtoLP</code> function).</p></li> <li><p>Reset the GDI transformation to <code>MM_TEXT</code>.</p></li> <li><p>Use the transformed points to construct a transformation matrix for GDI+ which represent the same transformation</p></li> <li><p>And finally use this matrix to construct a GDI+ context with the correct transformation.</p></li> </ol> <p>This is a bit of a hack, but it works. I still don't know why there is a difference between the two, though. At least it goes to show that it is <em>possible</em> to have a GDI+ context mimic the GDI transformation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload