【发布时间】:2021-09-04 03:29:56
【问题描述】:
我正在尝试使用 c# 和 aspx 创建一个动态二维码生成器,我不太了解 html,当我尝试在浏览器(opera)上运行它时出现一些错误
这是我的 c# 代码 QCCode.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ZXing;
namespace QRCodeSample
{
public partial class QCCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Event to generate the QC Code
protected void btnGenerate_Click(object sender, EventArgs e)
{
GenerateMyQCCode(txtQCCode.Text);
}
protected void btnRead_Click(object sender, EventArgs e)
{
ReadQRCode();
}
private void GenerateMyQCCode(string QCText)
{
var QCwriter = new BarcodeWriter();
QCwriter.Format = BarcodeFormat.QR_CODE;
var result = QCwriter.Write(QCText);
string path = Server.MapPath("~/images/MyQRImage.jpg");
var barcodeBitmap = new Bitmap(result);
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(path,
FileMode.Create, FileAccess.ReadWrite))
{
barcodeBitmap.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
imgageQRCode.Visible = true;
imgageQRCode.ImageUrl = "~/images/MyQRImage.jpg";
}
private void ReadQRCode()
{
var QCreader = new BarcodeReader();
string QCfilename = Path.Combine(Request.MapPath
("~/images"), "MyQRImage.jpg");
var QCresult = QCreader.Decode(new Bitmap(QCfilename));
if (QCresult != null)
{
lblQRCode.Text = "My QR Code: " + QCresult.Text;
}
}
}
}
这是我的 html QCCode.aspx
"<%@ Page Language = "C#" AutoEventWireup="true"
CodeBehind="QCCode.aspx.cs"
Inherits="QRCodeSample.QCCode" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml%22%3E
<head runat="server">
<title>Sample ASP.NET application to Generate and
Read QR Code from a Browser</title>
</head>
<body>
<form id="QCFrom" runat="server">
<div>
<asp:TextBox ID = "txtQCCode" runat="server">
</asp:TextBox >
<asp:Button ID = "btnQCGenerate" runat="server"
Text="Create My QR Code"
OnClick="btnQCGenerate_Click" />
<hr/>
<asp:Image ID = "imgageQRCode" Width="100px"
Height="100px" runat="server"
Visible="false" /> <br /><br />
<asp:Button ID = "btnQCRead" Text="Read My QR Code"
runat="server" OnClick="btnQCRead_Click" />
<br /><br />
<asp:Label ID = "lblQRCode" runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
这是 QCCode.aspx.designer.cs 页面我不知道这是什么
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace QRCodeSample
{
public partial class QCCode
{
/// <summary>
/// QCFrom control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm QCFrom;
/// <summary>
/// txtQCCode control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox txtQCCode;
/// <summary>
/// btnQCGenerate control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnQCGenerate;
/// <summary>
/// imgageQRCode control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Image imgageQRCode;
/// <summary>
/// btnQCRead control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnQCRead;
/// <summary>
/// lblQRCode control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblQRCode;
}
}
这是我运行时遇到的错误
CS1061: 'qccode_aspx' does not contain a definition for 'btnQCGenerate_Click' and no accessible extension method 'btnQCGenerate_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
Line 13: <asp:TextBox ID = "txtQCCode" runat="server">
Line 14: </asp:TextBox >
Line 15: <asp:Button ID = "btnQCGenerate" runat="server" Text="Create My QR Code" OnClick="btnQCGenerate_Click" />
Line 16: <hr/>
Line 17: <asp:Image ID = "imgageQRCode" Width="100px" Height="100px" runat="server" Visible="false" /> <br /><br />
C:\Users\bugra\source\repos\QrCode\QCCode.aspx(15,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCGenerate_Click' and no accessible extension method 'btnQCGenerate_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
C:\Users\bugra\source\repos\QrCode\QCCode.aspx(15,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCGenerate_Click' and no accessible extension method 'btnQCGenerate_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
C:\Users\bugra\source\repos\QrCode\QCCode.aspx(18,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCRead_Click' and no accessible extension method 'btnQCRead_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
C:\Users\bugra\source\repos\QrCode\QCCode.aspx(18,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCRead_Click' and no accessible extension method 'btnQCRead_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
【问题讨论】: