我想使用 IronBarCode 创建 QrCode,然后将其保存为 Stream 或 Byte[]。 但是,这两种方法都希望在创建文件之前保存文件:
var absolute = Request.Scheme + "://" + Request.Host + url;
var qrcode = IronBarCode.QRCodeWriter.CreateQrCode(absolute);
qrcode.AddAnnotationTextAboveBarcode(device.Name);
qrcode.AddBarcodeValueTextBelowBarcode(absolute);
var f = qrcode.ToJpegStream();
var y = qrcode.ToJpegBinaryData();
ToJpegStream() 和 ToJpegBinaryData 期望绝对字符串是实际文件路径。我想创建一个 QrCode 并将其保存为 Byte[] 或 Stream,但是抛出的错误是“文件名、目录名或卷标语法不正确。”
最佳答案
所提供的代码无法重现提到的问题。下面的代码改编自您的代码和 example 。已经测试过了。
创建新的 Windows 窗体应用程序 (.NET Framework)
下载/安装 NuGet 包:条形码
向 Form1 添加按钮(名称:btnCreateQRCode)
向 Form1 添加 PictureBox(名称:pictureBox1)
添加 using 指令:
使用 IronBarCode;
使用 System.IO;
Form1.cs:
public partial class Form1 : Form
{
private byte[] _qrCode = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private byte[] CreateBarCode(string url, string annotationText)
{
//create new instance
GeneratedBarcode qrCode = QRCodeWriter.CreateQrCode(url);
qrCode.AddAnnotationTextAboveBarcode(annotationText);
qrCode.AddBarcodeValueTextBelowBarcode(url);
byte[] qrCodeBytes = qrCode.ToJpegBinaryData();
return qrCodeBytes;
}
private void btnCreateQRCode_Click(object sender, EventArgs e)
{
_qrCode = CreateBarCode("https://www.google.com/search?q=how+to+search", "How To Search");
using (MemoryStream ms = new MemoryStream(_qrCode))
{
pictureBox1.Image = Image.FromStream(ms);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; //fit to size
pictureBox1.Refresh();
}
}
}
资源:
关于c# - 仅 IronBarCode 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74352439/