Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is it not possible to save any font as image? (But to display it on my windows form)
    primarykey
    data
    text
    <p>Im a little bit confused because I can display every string with every font on my windows form but as an image it is not always possible. Maybe there is just something wrong with my code. But let me show you what I was trying.</p> <p>At first I have this:</p> <pre><code> Label l = new Label(); l.Text = "Ì CSharp Î"; this.Font = new Font("Code 128", 80); l.Size = new System.Drawing.Size(300, 200); this.Controls.Add(l); this.Size = new Size(300, 200); </code></pre> <p><img src="https://i.stack.imgur.com/7WYTf.jpg" alt="string + code 128 font in windows form"></p> <p>Well this is very fine. Now I would like to try to save the same string with the same font as image. I found this code and I thought thats how to do this</p> <pre><code> private static Image DrawText(string text, Font font, Color textColor, Color backColor) { //first, create a dummy bitmap just to get a graphics object Image img = new Bitmap(1, 1); Graphics drawing = Graphics.FromImage(img); //measure the string to see how big the image needs to be SizeF textSize = drawing.MeasureString(text, font); //free up the dummy image and old graphics object img.Dispose(); drawing.Dispose(); //create a new image of the right size img = new Bitmap((int)textSize.Width, (int)textSize.Height); drawing = Graphics.FromImage(img); //paint the background drawing.Clear(backColor); //create a brush for the text Brush textBrush = new SolidBrush(textColor); drawing.DrawString(text, font, textBrush, 0, 0); drawing.Save(); textBrush.Dispose(); drawing.Dispose(); return img; } var i = DrawText("Ì CSharp Î", new Font("Code 128", 40), Color.Black, Color.White); </code></pre> <p>If I save the image I get this:</p> <p><img src="https://i.stack.imgur.com/xPtC7.jpg" alt="enter image description here"></p> <p>I dont get it. Im using the same string and the same font as I used it on my windows form. Why is that so? And how to avoid this problem?</p> <p>PS: The font that Im using was downloaded <a href="http://www.jtbarton.com/Barcodes/Code128.aspx" rel="nofollow noreferrer">here</a> but I tested it with other fonts too and its not always working.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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