c# - Windows 窗体上的图像 != TextRenderer.DrawText() 图像

标签 c# .net winforms image

正如我发布的 here ,我能够在我的 WindowsForm 上显示一些特殊字体但是我无法将这种字体保存为图片。问题是 Graphic.DrawString() 无法显示所有或特殊字体。 WindowsForm 使用 TextRenderer 及其更新版本(随 2.0 添加),这就是 WindowsForm 能够显示这些字体的原因。

很好。现在我坐在这里并尝试用 TextRenderer.DrawText() 做同样的事情 - 我不得不说我的输出图像现在​​好多了仍然不一样在窗体上。让我们看看我的代码:

    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 图像:

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

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

enter image description here

看起来很有趣吧?所以,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;
}

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

最佳答案

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

这不是唯一的问题,即使您解决了这个问题,它仍然不是有效的 Code128 条形码。您还需要 Start、Check 和 Stop 字符,如本 Wikipedia article 中所述.您可能接近不可避免的“购买,不构建”结论。寻找生成条形码的控件并不困难也不昂贵。

关于c# - Windows 窗体上的图像 != TextRenderer.DrawText() 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11868465/

相关文章:

.net - 可靠地更新 winforms 应用程序中的 UI 线程

c# - 如何从第二个显示器隐藏 winform 控件?

c# - 在 WPF 中动态添加和删除 xaml 结构

c# - 如何检索在 string.Contains 方法中传递的值?

c# - Enumerator.Current 为空但集合有数据。我究竟做错了什么

c# - 例如,是否有一些 .NET 机器学习库可以为问题建议标签?

c# - 拉绳从上到下 textdirection right (like chinese old style)

c# - 在 INSERT 查询中嵌套 SELECT

c# - 如何提高通过声卡传输文件的比特率?

C# Web 服务从 WinForms 应用程序返回对象为 null