15
votes

Is there any way to trick a WPF application into thinking that it is running at a certain DPI?

I'd like to test my program at various DPI levels (96, 120, 144, 192) without changing the system setting (which requires a log-out/in under Windows 7).

Can I manually set the size of 1 DIU? (At 96 DPI, 1 DIU = 1 pixel. I'd like to set 1 DIU to 1.25 pixels to imitate 120 DPI.)

3
I have some bitmap graphics in my application, rendered at 4 different sizes for 96, 120, 144 and 192 DPI. I need to test that my image switching code works (I haven't found anything in WPF that does this automatically). I've got a spare box to test with, but it would be nice to do it while debugging in VS somehow.Quppa
@madd0: Why wouldn't it be necessary? It's called testing for a reason.BoltClock♦
"Are you sure that this testing is really necessary?" Neat, never heard that one beforeGlenn Maynard

3 Answers

4
votes

You might be able to do what you want if you ScaleTransform the outtermost container. You'd just need to calculate the different between the current dpi and the target dpi and set the scale accordingly.

The other option is to use something like http://research.microsoft.com/en-us/projects/detours/ to override the windows API methods that gives the device dpi. I doubt you would want to go there though.

3
votes

As Steven above has stated, you can apply a top level ScaleTransform to achieve the same effect, i.e. define it on all your windows. I am doing something similar in my own application. This works best if your app doesn't have many different Window derived classes, since you have to modify every one. E.g. in the root layout item of your Window define something like the following.

<Grid x:Name="LayoutRoot">
  <Grid.LayoutTransform>
    <TransformGroup>
      <ScaleTransform ScaleX="1.25" ScaleY="1.25"/>
    </TransformGroup>
  </Grid.LayoutTransform>

  <!-- Rest of your app here... -->    

</Grid>
1
votes

You can use RenderTargetBitmap to render any Visual with any DPI to any size which may be helpful in your situation.