简体   繁体   中英

RenderTargetBitmap DPI for drawing application

I have been using RenderTargetBitmap to draw lines for my application as shown here .

It's working, but I don't quite understand the DPI. If I need the lines to draw under the mouse, I use a DPI of 96. All I need to know is whether this is the same for all devices and screens and, if not, how to find the correct one.

This is the code I use to get system DPI:

// Get system DPI
Matrix m = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
if (m.M11 > 0 && m.M22 > 0)
{
    dpiXFactor = m.M11;
    dpiYFactor = m.M22;
}
else
{
    // Sometimes this can return a matrix with 0s. Fall back to assuming normal DPI in this case.
    dpiXFactor = 1;
    dpiYFactor = 1;
}

This will be the factor of normal DPI (96) the system has. The system will have a horizontal DPI of dpiXFactor * 96 and a vertical DPI of dpiYFactor * 96. You can test this on Windows 7 by going start menu -> "dpi" and selecting "Make text and other items larger or smaller". 100% means a factor of 1 and a DPI of 96. 125% means a factor of 1.25 and a DPI of 120. 150% means a factor of 1.5 and a DPI of 144.

The fallback logic is due to a customer crash report which I think could have only been caused by the transform matrix having all zeroes. There might be a more reliable way of getting system DPI (pinvoke, maybe?) but I don't know of it.

DPI is used when rendering non vector images. You dont have to worry about changing DPI's for different monitors or screens tahts determined by the OS and monitor hardware. What you do have to consider is the level of pixelation you want in your image. As a general rule of thumb small images under 100x100 pixel images should be ok with 96 DPI, any thing between 100x100 and 300x300 you should go 150-175 DPI. for anything larger than 300 that you can go 300-400 DPI. After a certain point its just overkill. Most commercial printing is done under 350 DPI unless your doing stochastic printing. If your drawing lines with a stroke of 1-3 you should be ok with 96 DPI, any larger I would suggest 150 DPI.

The DPI you use depends on what will be done with the output. If your drawing will be viewed only on the screen without zooming, you will be ok with 96dpi and you don't really need to worry about it anymore. If you need to zoom in on your drawing, then higher resolution can provide a better user experience. If printing your drawing is an important part of your user experience, then you'd probably be better served going with 300dpi.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM