0

One of the part of my project is RichTextBox with string length form 10 to 50 chars, and font's size 175-225 in single line. I use special printer: 100 mm height, and 3000 mm (3m) width ribbon. Very long width is a real problem.

I've got problem with Printing.PaperSize width element (I have to use it). It's (MSDN) "The width of the paper, in hundredths of an inch".

I tried to get this from:

RichTextBox1.PreferredSize.Width
g.MeasureString(RichTextBox1.Text, RichTextBox1.Font).Width
g.MeasureCharacterRanges(Text, Font, Rect, Format)

All of them gives me "pixels" but I have no idea how can I use it with Printing.PaperSize - all of them are too short, and dependent on used font family.

Tricky part of this is that I need very precise length of my PaperSize, because I have to print several items before and after the string.

Is any way to estimate printed width (in cm/inches) when I have only pixel size of element?

If anyone can give me any answer in C#, or C++ it doesn't matter - I will be grateful.

  • The Graphics object has .DpiX and .DpiY properties: [Graphics.DpiX Property](https://msdn.microsoft.com/en-us/library/system.drawing.graphics.dpix%28v=vs.110%29.aspx). If that doesn't work out, you could print a known number of pixels and measure the result with a ruler. – Andrew Morton Jun 24 '15 at 09:42
  • But how I can use '.DpiX' with 'String' from 'RichTextBox'? If I try use 'DrawToBitmap' I have to give proper size of bitmap? Thank you for answer. – Marcin Goworek Jun 25 '15 at 18:15
  • (Printed width) = (width in pixels) / (horizontal resolution). 1 in = 25.4 mm. – Andrew Morton Jun 25 '15 at 18:34
  • I know it, but still I don't know how I can estimate string. For example: what could be size in in/cm for: "lorem ipsum dolor sit amet" where font is Times New Roman 225pt? – Marcin Goworek Jun 26 '15 at 14:08

1 Answers1

0

You can measure the width of the string in a given font:

Sub ShowWidthOfText(s As String)
    Using img As New Bitmap(1, 1)
        img.SetResolution(300, 300)

        Using f As New Font("Times New Roman", 225, GraphicsUnit.Point)
            Using g = Graphics.FromImage(img)
                g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
                Dim w = g.MeasureString(s, f)
                MessageBox.Show(String.Format("{0} pixels = {1:N2} cm at {2} ppi", w.Width, w.Width * 2.54 / g.DpiX, g.DpiX))

            End Using
        End Using
    End Using

End Sub

which gives 90.75 cm (35.73 in) for "lorem ipsum dolor sit amet" in the font you suggest, which seems reasonable to me. If you are intending to set the the width of the paper size from that, you would of course need a minimum of 3573 hundredths of an inch.

If you want to fit the text to a particular width, it would probably be best to use a successive approximation method rather than a direct calculation because the kerning of the text may depend on the size of the font.

The .SetResolution is in there in case you use that code to construct your output, it makes no difference to the measurement.

(The resolution names in .NET are wrong: they should be, e.g. PpiX rather than DpiX etc.)

Ref: Measure a String without using a Graphics object?

Community
  • 1
  • 1
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thank you. It was exactly what I needed. One more question with this project - is any way to make vb to print one thing after another, if I don't know size of previous element? I have problem - after my string I need print next one, or btm graphic from file. I'm unable to give specific starting point for next element to printing. – Marcin Goworek Jun 27 '15 at 10:05
  • @MarcinGoworek You should be able to find out the size of all elements. `.MeasureString` gives a `SizeF` structure which also has a `.Height` property. Bitmaps also have a width and height (in pixels). – Andrew Morton Jun 27 '15 at 16:07
  • '.MeasureString' is not to precise, and when I use it to put gfx after the string, they are too close, or too wide (depending on the font). But error is not to big, and I think that I can do it. Thank you very much - without your help I would never end this project. – Marcin Goworek Jun 29 '15 at 07:26
  • @MarcinGoworek You're welcome :) Have you tried any of the alternative values for the `g.TextRenderingHint` to see if they give better accuracy? – Andrew Morton Jun 29 '15 at 10:28