我正在嘗試創建一個將生成條形碼的應用程序。到目前爲止,我無法讓它正常工作。我得到兩個錯誤:生成條形碼
- 錯誤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());
}
}
}
'使用(MemoryStream ms = MemoryStream())'應該使用MemoryStream ms = new MemoryStream()) - 你正在實例化一個不調用本地方法的對象 – Rhumborl
至於生成錯誤,不能說,也許我們沒有所有的代碼?我沒有看到任何地方在你的代碼中生成的單詞 – Wjdavis5
對不起,剛剛添加了來自program.cs的代碼 – Truex