System.Drawing.Image 无法在 Web 窗体 C# 2010 中工作

System.Drawing.Image not working in Web Form C# 2010

我在 C# 中有一个桌面 Barcode 应用程序,如下所示

现在我想在 C# 2010 ASP.NET 中将桌面应用程序转换为 Web 应用程序。

Interface如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Barcode.aspx.cs"       
Inherits="Barcode" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"             
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Table ID="BarcodeTable" runat="server">
       <asp:TableRow>
            <asp:TableCell>
                <asp:Label ID="lblToEncode" runat="server" Text=
              "Text To  Encode"></asp:Label>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox runat="server" ID="textToEncode"></asp:TextBox>
            </asp:TableCell>
       </asp:TableRow>

       <asp:TableRow>
            <asp:TableCell>
                <asp:Label ID="lblBarcodeWeight" runat="server" Text="Barcode Weight"></asp:Label>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="textBarcodeWeight" runat="server"></asp:TextBox>
            </asp:TableCell>
       </asp:TableRow>


        <asp:TableRow>
            <asp:TableCell>
                <asp:Label ID="lblEncodedText" runat="server" Text="Encoded Text"></asp:Label>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="encodedText" runat="server"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>

        <asp:TableRow>
            <asp:TableCell>
                <asp:Button ID="btnMakeBarcode" runat="server" Text="Make Barcode" />
            </asp:TableCell>

            <asp:TableCell>
                <asp:Image ID="imgBarcode" runat="server" />
            </asp:TableCell>
        </asp:TableRow>

    </asp:Table>

</div>
</form>

Barcode.aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GenCode128;
using System.Drawing;
public partial class Barcode : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
private void btnMakeBarcode_Click(object sender, EventArgs e)
{
    try
    {
           Image myimg =  
           Code128Rendering.MakeBarcodeImage(textToEncode.Text, 
           int.Parse(textBarcodeWeight.Text), true);
            imgBarcode.ImageUrl = myimg;
    }
    catch (Exception ex)
    {
        String errMsg = ex.Message;

        System.Web.HttpContext.Current.Response.Write("<script    
        language='javascript'>alert(" +errMsg+")</script>");
        //MessageBox.Show(this, ex.Message, this.Text);
    }
}

Image myimg 行给我一个错误:

错误 19 无法将类型 'System.Web.UI.WebControls.Image' 隐式转换为 'string'

System.Web.UI.WebControls.Image 只是图像 url 的 HTML 容器。使用 'MakeBarcodeImage' 创建图像,即 System.Drawing.Image 并将图像 url 设置为 'imgBarcode'。

string barcodeImageName = "~/images/barcode.jpg";
string savePath =Server.MapPath(@"images\barcode.jpg");

myimg.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
imgBarcode.ImageUrl = barcodeImageName;

您可以将图像转换为 base64 string 并从代码分配给 html 标签。