I have a Desktop Barcode
Application in C#
as given below
Now I want to convert the desktop app to a Web App in C# 2010 ASP.NET
.
Interface
is as given below
<%@ 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 code
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 line is giving me an error:
Error 19 Cannot implicitly convert type 'System.Web.UI.WebControls.Image' to 'string'
System.Web.UI.WebControls.Image is just a HTML container for an image url. Create image using 'MakeBarcodeImage' which is System.Drawing.Image and set image url to 'imgBarcode'.
string barcodeImageName = "~/images/barcode.jpg";
string savePath =Server.MapPath(@"images\barcode.jpg");
myimg.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
imgBarcode.ImageUrl = barcodeImageName;