2016-04-13 111 views
0

我正在嘗試創建一個將生成條形碼的應用程序。到目前爲止,我無法讓它正常工作。我得到兩個錯誤:生成條形碼

- 錯誤CS0118'生成'是一個名稱空間,但使用像一個類型生成; -Error CS1955不可使用的成員'MemoryStream'不能像方法一樣使用。產生

請參閱下面的代碼:

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 barcode 
{ 
    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 = MemoryStream()) 
      { 
       bitmap.Save(ms, ImageFormat.Png); 
       box.Image = bitmap; 
       box.Height = bitmap.Height; 
       box.Width = bitmap.Width; 
      } 
     } 
    } 
    } 



PROGRAM.cs 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 

    namespace generate 
    { 
     static class Program 
     { 
      /// <summary> 
      /// The main entry point for the application. 
      /// </summary> 
      [STAThread] 
      static void Main() 
      { 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 
       Application.Run(new generate()); 
      } 
     } 
    } 
+2

'使用(MemoryStream ms = MemoryStream())'應該使用MemoryStream ms = new MemoryStream()) - 你正在實例化一個不調用本地方法的對象 – Rhumborl

+0

至於生成錯誤,不能說,也許我們沒有所有的代碼?我沒有看到任何地方在你的代碼中生成的單詞 – Wjdavis5

+0

對不起,剛剛添加了來自program.cs的代碼 – Truex

回答

4

改變這一行

Application.Run(new generate()); 

要這樣:

Application.Run(new Form1()); 

此外,你將不得不導入命名空間Form1到您的Program.cs文件中,並附有以下行

using barcode; 

對於MemoryStream錯誤,改變這一行:

using (MemoryStream ms = MemoryStream()) 

要這樣:

using (MemoryStream ms = new MemoryStream()) 

這裏發生了什麼是你沒有用關鍵字new所以初始化的MemoryStream實例編譯器將其視爲一種方法

+0

太棒了!感謝您的幫助。該應用程序現在工作,它確實生成一個圖像,但它不會創建一個條形碼。有任何想法嗎?謝謝 – Truex

+0

@Truex我會建議發佈一個新問題,提供與您的新問題有關的所有重要數據。很高興我能幫上忙! :) –

+0

乾杯隊友,我只是剛剛發佈了另一個問題。再次感謝您的幫助 – Truex