10

正如我在此处发布的那样,我能够在我的 WindowsForm 上显示一些特殊字体,我无法将此字体另存为图片。问题Graphic.DrawString()是无法显示所有或特殊字体。WindowsForm 正在使用TextRenderer它的更新版本(添加了 2.0),这就是 WindowsForm 能够显示这些字体的原因。

美好的。现在我坐在这里,尝试做同样的事情TextRenderer.DrawText()- 我不得不说我的输出图像现在​​好多了,但仍然与 WindowsForm 上的不同。让我们看看我的代码:

    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 是一种特殊字体,如果您真的想了解更多信息,请点击下面的链接了解更多信息)

所以这是有效的,我得到了我的形象,很好。现在我在同一个 VS 项目中添加具有相同字体的相同字符串作为我的 WindowsForm 上的标签。并且有一些区别。不同之处在于创建的图像TextRenderer在第一个位置总是有不同的字符。其他一切都很好!我不明白。让我们看看 WinForm VS Image:

带有标签的 WindowsForm VS TextRenderer 图像(相同的字符串和字体)

正如我们所看到的,第一个“字符”(如果我们可以在这个例子中说“字符”)在某种程度上是不一样的。现在让我将 TextRenderer Image 函数中的字符串更改为“TTEST”。比较 WinForm 上的“TEST”和 TextRenderer Image 上的“TTEST”:

在此处输入图像描述

看起来很有趣吧?所以,TextRenderer.DrawText()+ special font = first char 很奇怪。但为什么?以及如何解决这个问题?有什么建议么?谢谢!

编辑:如果有帮助,我可以发布 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;
}

正如我们所看到的,它具有相同的字体,具有相同的字符串。我不明白...

4

1 回答 1

2

您发布了错误的代码,这是关闭的位图版本。在我看来像 UEST,一个明显的错误。

这不是唯一的问题,即使您解决了这个问题,它仍然不是有效的 Code128 条形码。您还需要开始、检查和停止字符,如此Wikipedia 文章中所述。您可能已经接近不可避免的“购买,不要建造”的结论。找到生成条形码的控件并不困难或昂贵。

于 2012-08-10T22:11:40.060 回答