开发者

How to convert a string size into inches in C#

开发者 https://www.devze.com 2023-03-17 13:40 出处:网络
I want to know the printing size of a string in inches. i.e. what will be the length (and width) of string \"Test String\" when printed on a paper. I can get the length in pixels using the Graphics ob

I want to know the printing size of a string in inches. i.e. what will be the length (and width) of string "Test String" when printed on a paper. I can get the length in pixels using the Graphics object and MeasureString method开发者_StackOverflow中文版. But don't know how to convert pixels into inches on paper.

I checked this SO question but can't find the solution.


The resolution of a digital graphic is its pixel dimensions. The size of the printed graphic is dependent of the printer settings.

  • If you print a picture of 300 x 300 pixels at 300dpi on a printer the picture becomes 1 x 1 inch. ( 300 / 300 )

  • If you print a picture of 300 x 300 pixels at 10dpi on a printer the picture becomes 30 x 30 inch. ( 300 / 10 )

  • If you print a picture of 300 x 300 pixels at 1000dpi on a printer the picture becomes 0,3 x 0,3 inch. (300 / 1000 )

However, the exact size depends on the accuracy of the printer (paper feeding, servo of the printer head etc.)

An excellent reference is found here: DPI has NOTHING to do with digital image quality!.

The Dpi of the display has NOTHING to do with this


I know this is an old question, but a quazi solution for it. I have tested my solution a few times and it seems to be accurate but not precise. The basis of this code is directly from an MSDN example and this answer on SO, the only thing I am doing differently is changing the units from points to inches.

MSDN Graphics.MeasureString()

private float GetTextWidth(string fontFace, float fontSize, string text)
{
    SizeF size = new SizeF();

    Font font = new Font(fontFace, fontSize);

    //Using a Bitmap object for frame of reference in order to get to a Graphics object
    using (Bitmap b = new Bitmap(1, 1))
    {
        //Graphics object allows string measurement
        using (Graphics g = Graphics.FromImage(b))
        {
            //change the units to inches
            g.PageUnit = GraphicsUnit.Inch;

            //Perform the measurement
            size = g.MeasureString(text, font);
        }
    }

    //Print to console if you want 
    //Console.WriteLine(size.Width);

    return size.Width;
}

I tested this out with the following inputs: ("Arial", 10, "Page 1 of 1") and I got 0.777972 inches

If you read the Remarks of the MSDN link above you will see that there is a little extra space padded onto that figure. You have to be aware that this happens:

The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs.

When I measured this on a piece of paper end to end I got about 0.625, that is a difference of 0.152972 which is essentially ~20% padding. I'm guessing it is distributed on either side evenly.

Also be careful with which Fonts you use - for example Helvetica is not a supported font by default, therefore the Font object will default to Arial if it can't find your font.


This is not possible, unless you know both the printer size and display size in hard numbers.

Say you're printing a full screen handout. On a 30 inch monitor, 1 pixel may be 1 unit large on paper. But on a 7 inch netbook, a pixel will be 2 units large on paper - twice the size, even though they are both one pixel on the electronic display.

Simply put, this measurement is not possible unless you are working with something fixed, like an iPhone's display size (which is well known), or your own hardware whose screen size you have exact measurements to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号