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 は特別なフォントです。詳しく知りたい場合は、以下のリンクをクリックして詳細を確認してください)

だからこれはうまくいっている、私は自分のイメージを得る。今、私は WindowsForm の Label と同じ VS Project に同じフォントで同じ文字列を追加しています。そして、いくつかの違いがあります。違いは、 で作成されたイメージTextRendererは最初の位置で常に異なる文字を取得することです。他のすべては大丈夫です!理解できません。WinForm VS Image を見てみましょう:

Label VS TextRenderer Image を使用した WindowsForm (同じ文字列とフォント)

ご覧のとおり、最初の「文字」(この例で「文字」と言える場合) は、どういうわけか同じではありません。ここで、TextRenderer Image 関数の文字列を "TTEST" に変更します。WinForm の "TEST" と TextRenderer イメージの "TTEST" を比較します。

ここに画像の説明を入力

面白そうですよね?だから、TextRenderer.DrawText()+特殊フォント=最初の文字は変です。しかし、なぜ?そして、これを修正する方法は?助言がありますか?ありがとうございました!

編集:それが役立つ場合は、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のように見えますが、1つずつエラーが目立ちます。

これだけが問題ではありません。これを修正しても、有効なCode128バーコードではありません。このウィキペディアの記事に記載されているように、開始、チェック、および停止の文字も必要です。あなたはおそらく、避けられない「買う、構築しない」という結論に近づいています。バーコードを生成するコントロールを見つけることは難しくも高価でもありません。

于 2012-08-10T22:11:40.060 に答える