我试图创建一个应用程序,将产生一个条形码。到目前为止,我无法使它正常工作。我有两个错误:
-Error CS0118 'generate‘是一个名称空间,但使用起来像类型generate;-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());
}
}
}发布于 2016-04-13 17:44:21
更改这一行
Application.Run(new generate());对此:
Application.Run(new Form1());此外,您还必须使用下面的行将Form1的命名空间导入到Program.cs文件中
using barcode;对于MemoryStream错误,请更改这一行:
using (MemoryStream ms = MemoryStream())对此:
using (MemoryStream ms = new MemoryStream()) 这里发生的事情是,您未能使用关键字MemoryStream初始化new实例,因此编译器将其视为一种方法。
https://stackoverflow.com/questions/36605024
复制相似问题