2016-04-13 10 views
1

最近、エラーなしで正しく動作するようにアプリケーションを管理しています。バーコードを生成する - アプリケーションはエラーフリーですが、バーコードは生成されません

問題は、テキストが下にあるバーコードを生成することになっていることです。バーコードなしの画像のみを生成します。

私はIDAutomationHC39Mというフォントを使用しています。アプリはテキストをバーコードに変換する必要があります。

以下のコードを参照してください。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Drawing.Imaging; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      String barcode = pole.Text; 
      Bitmap bitmap = new Bitmap(barcode.Length * 40, 150); 
      using (Graphics graphics = Graphics.FromImage(bitmap)) 
      { 
       Font ofont = new System.Drawing.Font("IDAutomationHC39M", 20); 
       PointF point = new PointF(2f, 2f); 
       SolidBrush black = new SolidBrush(Color.Black); 
       SolidBrush White = new SolidBrush(Color.White); 
       graphics.FillRectangle(White, 0, 0, bitmap.Width, bitmap.Height); 
       graphics.DrawString("*" + barcode + "*", ofont, black, point); 
      } 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       bitmap.Save(ms, ImageFormat.Png); 
       box.Image = bitmap; 
       box.Height = bitmap.Height; 
       box.Width = bitmap.Width; 
      } 
     } 

     private void pole_TextChanged(object sender, EventArgs e) 
     { 

     } 
    } 
} 
+0

あなたはArialのような別のフォント、代わりのバーコードフォントを使用するとどうなりますか – TaW

+0

同じ空白の画像に – Truex

+0

のテキストが入っていますか?ボックスは画像ボックスですか?あなたのコードはここでうまくいく:白い背景のテキスト。 – TaW

答えて

2

あなたのコードのいくつかのマイナーな問題、すなわち、GDIリソースのリークと、作成したビットマップの不正確な測定があります。ここで

は、これらの問題の世話をバージョンです:

String barcode = "*" + pole.Text + "*"; 
    PointF point = new PointF(5f, 5f); 
    float fontHeight = 20f; 
    Bitmap bitmap = new Bitmap(123,123); 
    using (Font ofont = new System.Drawing.Font("IDAutomationHC39M", fontHeight)) 
    { // create a Graphics object to measure the barcode 
     using (Graphics graphics = Graphics.FromImage(bitmap)) 
     { 
      Size sz = Size.Round(graphics.MeasureString(barcode, ofont)); 
      bitmap = new Bitmap(sz.Width + 10, sz.Height + 10); 
     } // create a new one with the right size to work on 
     using (Graphics graphics = Graphics.FromImage(bitmap)) 
     { 
      graphics.Clear(Color.White); 
      graphics.DrawString(barcode, ofont, Brushes.Black, point); 
     } 
    } 
    box.Image = bitmap; 
    box.ClientSize = bitmap.Size; 

    // bitmap.Save(fileName, ImageFormat.Png); 

しかし、あなたの主な問題はほとんど完全に互換性のないフォントを使用してから来ています。そこに多くのフリーフォントがあり、すべてがうまく機能するわけではありません。

this fontはうまく動作しますが、インストールされたフォントを列挙する私の古いプログラムの1つで動作しましたが、this one to work directlyを取得できませんでした。しかし、たとえ設計者は、ここで

...それは名前だけではありませんので、それを使用することを拒否したサンプルです:

enter image description here

+0

ありがとう、あなたが言及したフォントは私が使用するものですが、バーコード自体の代わりに文字と数字だけを生成します。 – Truex

+0

私のコードを試しましたか?そして、正しいもの(第1のもの)?バーコードフォントはバーの下に小さな文字などを生成します。私は画像を追加しました.. – TaW

+0

今、それは動作し、私はいくつかの変更を加えなければならず、それは魔法を働かせます。ありがとう、助けてくれてありがとう – Truex

関連する問題