Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are aware that <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.bitsperpixel.aspx" rel="nofollow noreferrer"><code>BitsPerPixel</code></a> describes the color depth (the number of memory bits that are used to describe the color of a pixel), and has nothing to do with resolution?</p> <p>I assume that what you want to do is to draw the text in a size that is related to the resolution, which you can do by referring to the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.bitsperpixel.aspx" rel="nofollow noreferrer"><code>DpiX</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.drawing.graphics.dpiy.aspx" rel="nofollow noreferrer"><code>DpiY</code></a> properties of the <a href="http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx" rel="nofollow noreferrer"><code>Graphics</code></a> object.</p> <p><strong>Update</strong></p> <p>I am not sure if yo need to bring Dpi into the calculation for this. All you need to do is to create a Rectangle that defines the desired size of your text, and then calculate the correct font size to make the text fit inside the rectangle. The following does that (but maximizes the text size both vertical and horizontal direction). It might give you some pointers to solve your problem:</p> <pre><code>Bitmap bm = new Bitmap(50, 50); using (Font font = new Font(fontName, 10, GraphicsUnit.Point)) using (Graphics g = Graphics.FromImage(bm)) { g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Near }; Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height); // measure how large the text is on the Graphics object with the current font size SizeF s = g.MeasureString(text, font); // calculate how to scale the font to make the text fit float fontScale = Math.Max(s.Width / rect.Width, s.Height / rect.Height); using (Font fontForDrawing = new Font(font.FontFamily, font.SizeInPoints / fontScale, GraphicsUnit.Point)) { g.DrawString(text, fontForDrawing, Brushes.Black, rect, stringFormat); } } </code></pre> <p>And if you want to print the text with a given point size, you don't need to go about measuring; just set the font size:</p> <pre><code>Bitmap bm = new Bitmap(20, 20); using (Font font = new Font(fontName, 6, GraphicsUnit.Point)) using (Graphics g = Graphics.FromImage(bm)) { g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Near }; Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height); g.DrawString(text, font, Brushes.Black, rect, stringFormat); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload