Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image on WindowsForm != TextRenderer.DrawText() image

As I posted here, I was able to display some special fonts on my WindowsForm but I was not able to save this font as picture. The problem was that Graphic.DrawString() is not able to display all or special fonts. WindowsForm is using TextRenderer and its newer (added with 2.0) thats why WindowsForm was able to display these fonts.

Fine. Now Im sitting here and trying to do the same stuff with TextRenderer.DrawText() - I have to say that my output image is now much better but still not the same as on the WindowsForm. Lets look at my code:

    Bitmap Bit = new Bitmap(500, 200);
    Graphics g = Graphics.FromImage(Bit);

    string s = "TEST";

    g.Clear(Color.White);
    TextRenderer.DrawText(g, s, new Font("Code 128", 72), new Point(20, 20), Color.Black, Color.White);
    Bit.Save(@"C:\myFolder\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
    Bit.Dispose();

(Code 128 is a special font, if you really want to know more about it just klick on the link below for more information)

So this is working, I get my image, fine. Now Im adding the same string with the same font on the same VS Project as Label on my WindowsForm. And there is some difference. The difference is that the image created with TextRenderer got always a different character at the first position. Everything else is fine! I dont get it. Lets have a look WinForm VS Image:

WindowsForm with Label VS TextRenderer Image (same string and the font)

As we can see the first "character" (if we can say 'character' for this example) is somehow not the same. Now let me change my string in my TextRenderer Image function to "TTEST". Compare "TEST" on WinForm with "TTEST" on TextRenderer Image:

enter image description here

Looks interesting right? So, TextRenderer.DrawText() + special font = first char is weird. But why? And how to fix this? Any suggestions? Thank you!

Edit: If it helps I can post the Code of the WindowsForm

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Font = new System.Drawing.Font("Code 128", 72F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
        this.label1.Location = new System.Drawing.Point(30, 41);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(144, 94);
        this.label1.TabIndex = 0;
        this.label1.Text = "TEST";
        this.label1.Click += new System.EventHandler(this.label1_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.label1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label label1;
}

As we see its the same font, with the same string. I dont get it...

like image 588
sabisabi Avatar asked Aug 08 '12 15:08

sabisabi


1 Answers

You posted the wrong code, it is the bitmap version that's off. Looks like UEST to me, a conspicuous off-by-one error.

That's not the only problem, even if you'd fix this it still isn't a valid Code128 barcode. You also need the Start, Check and Stop character, as documented in this Wikipedia article. You are possibly close to the inevitable "buy, don't build" conclusion. Finding controls that generate barcodes isn't difficult or expensive.

like image 149
Hans Passant Avatar answered Sep 20 '22 05:09

Hans Passant