1

I'm creating a control that allows the user to create a visual element that can be printed. The challenge is the user can change the size of visual but needs to know the actual size of the visual they are creating. In other words I have the size of the element in pixels but I need to know what the actual size of the element will be in inches.

For example if I create a button that is 96 pixels wide, I would expect it to be 1 inch long, yet the size of the button is different on different monitors with different resolutions.

Is this possible?

user1604008
  • 995
  • 9
  • 17
  • When you say 'can be printed', do you mean on paper? The rest of your question seems to indicate not. – Troels Larsen Jul 08 '14 at 20:18
  • eventually 'to paper' but more importantly for now the user needs to know the size of the element. For example they may have the requirement to create something that has text, that can fit in a 2 by 3 inch square. They may want to change the font size to be as large as possible but still fit in a 2X3 inch square. Ideally I would like to show the element on screen at the actual size so if it is larger then 2x3 they can adjust the font size appropriately. – user1604008 Jul 08 '14 at 20:26

2 Answers2

2

It is not possible in cases where you don't know the actual PPI of your monitor (such as with a projector), but here is an attempt at calculating it from a Rectangle of 96x96 device independent pixels:

var ppi = 220; //The PPI on my monitor.
var devicePixels = GetElementPixelSize(MyRectangle); //Convert WPF pixels to device pixels
var widthInInches = devicePixels.Width / ppi; //Convert pixels to inches based on the PPI.

Using this code (from How do I convert a WPF size to physical pixels?)

public Size GetElementPixelSize(UIElement element)
{
    Matrix transformToDevice;
    var source = PresentationSource.FromVisual(element);
    if (source != null)
        transformToDevice = source.CompositionTarget.TransformToDevice;
    else
        using (var source2 = new HwndSource(new HwndSourceParameters()))
            transformToDevice = source2.CompositionTarget.TransformToDevice;

    if (element.DesiredSize == new Size())
        element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

    return (Size)transformToDevice.Transform((Vector)element.DesiredSize);
}

In my case, 96 translates to 144px. 144/220 is 0.65656565 inches.

To get the ppi, take a look at this question: acquire monitor physical dimension

It is not an easy task, and there is no guarantee that simply because it is rendered a certain size, it will also look that way on print. A printer also has a DPI and a paper size, after all.

Community
  • 1
  • 1
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54
-2

You really should google on WPF, DPI and Scaling because the scaling system of WPF has some important characteristics. From the search you will find this site for a first read about the topic.

About your question is this possible? Yes it surely is! ;)

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • If I understand I can create a per monitor DPI aware application, but would I not still need to know the size of the monitor? – user1604008 Jul 08 '14 at 20:46