簡體   English   中英

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只是圖像網址的HTML容器。 使用System.Drawing.Image的“ MakeBarcodeImage”創建圖像,並將圖像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標簽。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM