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

前端 未结 2 704
旧巷少年郎
旧巷少年郎 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:20

    I am slightly confused because you are using two different units of measure in your question. I will try to explain both:

    PX

    This is the height in pixels. This should be as easy as setting the Font:

    new Font("Verdana", 16F);
    

    EM

    This is going to be much harder to control, because this is a multiple of your line height. Such as if 1em = 14px then 16em = (14 * 16)px. This is going to be hard to replicate unless you know your line height.

    Also the document says the following

    The em-size, in points, of the new font.

    So it is defined in PX or pixels for the constructor you are using. You could try this constructor, but EM is really a browser/CSS implementation that is dynamically adjusted according to the screen, almost like Vectors. Where the Font object is a Bitmap that works the number of pixels to draw.

    http://msdn.microsoft.com/en-us/library/ms141986.aspx

提交回复
热议问题