How to render text in .NET in the same size as browsers does given CSS for the text

前端 未结 2 707
旧巷少年郎
旧巷少年郎 2021-01-19 04:46

I am trying to create a \"save webpage as bitmap\"-function on a website and i have some problems rendering the text in the correct size on the server side.

The font

2条回答
  •  走了就别回头了
    2021-01-19 05:06

    Actually, the docs say "em-size", not "em-point" ("The em-size, in points, of the new font"). It's asking you to specify the size in points. There are 72 points per inch. You need to figure out the DPI of the user's screen and the DPI of the canvas you're drawing on and multiply the 16px size by the difference in that ratio.

    e.g.

    (CSS_Font_Size_Pixels * Canvas_DPI) / (User_Screen_DPI * 72) = Equivalent_Point_Size
    

    You could save yourself a mathematical operation by using the Font constructor overload that takes a GraphicUnit and specify Pixels. This way, the appropriate size would be:

    (CSS_Font_Size_Pixels  / User_Screen_DPI) * Canvas_DPI
    

提交回复
热议问题