none
Obsolete FormattedText(): 'Use the PixelsPerDip override' RRS feed

  • Question

  • Hi,

      I have a simple function to compute the text width. Please see below:

    ******************************

      public static double CalculateTextWidth(string valueText, int lineCount, double maxTextWidth, Typeface typeface, double fontSize, Brush foreground, CultureInfo cultureInfo, FlowDirection flowDirection)

      {

          if (String.IsNullOrEmpty(valueText)  == true)

               return 0.00;

          FormattedText text = new FormattedText(valueText, cultureInfo, flowDirection, typeface, fontSize, foreground);

           if (lineCount > 0)

                text.MaxLineCount = lineCount;

           if (Double.IsNaN(maxTextWidth) == false)

                 text.MaxTextWidth = maxTextWidth;

           return text.Width;

    }

    ******************************

        In my example, I do not really need the PixelPerDips, as I am just trying to compute the text width. In this case, may I know if I can just give a default value, like 1.0? Is it advisable? Or will it be a problem in the computation?

        I saw some examples of computing the PixelPerDips in the Constructor. But in my case, it is not possible. Hence, I need to know other alternatives.

         Thank you.


    Friday, August 4, 2017 12:39 AM

All replies

  • Hi NetworkNovice,

    >>  In my example, I do not really need the PixelPerDips, as I am just trying to compute the text width. In this case, may I know if I can just give a default value, like 1.0? Is it advisable? Or will it be a problem in the computation?

    Please check the following thread:

    You need to calculate the DPI of your monitor, see: How can I get the DPI in WPF?

    If you just have 1 monitor and therefore don't need any DPI changed event handling, use the following for example in the OnLoaded() event of your Window (or in your constructor):

    var pixelsPerDip =  VisualTreeHelper.GetDpi(this).PixelsPerDip;

    https://stackoverflow.com/questions/40277388/in-net-framework-4-6-2-the-formattedtext-is-obsoleted-how-can-i-fixed-it

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 7, 2017 5:17 AM
    Moderator
  • Hi Cole,

      Thank you for your reply. I have read that link where you provided before. I am aware of this implementation.

      Though, I may not fully understand the DPI thing in .NET 4.6. Hence, I am trying to see if it is advisable to just set "PixelsPerDip" in FormattedText() to just 1.0 or some other numerical default values. I knew I can just set to 1.0, but I am not too sure if it is appropriate.



    Monday, August 7, 2017 5:49 AM
  • Hi NetworkNovice,

    >> Though, I may not fully understand the DPI thing in .NET 4.6.

    Based on the comments, if you use "1.0" so that the function expects a monitor with 96 DPI, For more information about DPI, please refer to:

    https://msdn.microsoft.com/en-us/library/windows/desktop/dn469266(v=vs.85).aspx

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 14, 2017 2:02 AM
    Moderator
  • Hi Cole,

      Thanks again for your reply.

      I realized that I couldn't used your suggestion to get the PixelsPerDip information during OnLoaded event or Constructor. The class itself cannot be converted to System.Windows.Media.Visual.

    Monday, August 21, 2017 6:24 AM
  • Hi NetworkNovice,

    >>The class itself cannot be converted to System.Windows.Media.Visual.

     you could use fixed value with 1.0 if The class itself cannot be converted to System.Windows.Media.Visual.  

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 21, 2017 10:17 AM
    Moderator