WindowsForm上的图像!= TextRenderer.DrawText()图像

sab*_*abi 10 .net c# image winforms

正如我在这里发布的,我能够在我的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();
Run Code Online (Sandbox Code Playgroud)

(代码128是一种特殊字体,如果您真的想了解更多信息,请点击以下链接获取更多信息)

所以这是有效的,我得到了我的形象,很好.现在我在WindowsForm上的标签上添加相同字体和相同字体的相同字符串.而且有一些不同.不同之处在于,创建的图像TextRenderer在第一个位置始终具有不同的字符.其他一切都很好!我不懂.让我们来看看WinForm VS Image:

带有Label VS TextRenderer Image的WindowsForm(相同的字符串和字体)

我们可以看到第一个"字符"(如果我们可以说这个例子中的'字符')在某种程度上是不一样的.现在让我将TextRenderer Image函数中的字符串更改为"TTEST".将WinForm上的"TEST"与TextRenderer上的"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;
}
Run Code Online (Sandbox Code Playgroud)

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

Han*_*ant 2

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

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