Hi smile,
Refer the below article i have created the example.
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="true"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hfBarcode" Value='<%# Eval("Barcode") %>' />
<asp:Image ID="imgBarcode" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Drawing;
using System.IO;
using System.Data;
VB.Net
Imports System.Drawing
Imports System.IO
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)),
new DataColumn("Barcode") });
dt.Rows.Add(1, "John Hammond", "United States", "1241972");
dt.Rows.Add(2, "Mudassar Khan", "India", "7208922");
dt.Rows.Add(3, "Suzanne Mathews", "France", "2387841");
dt.Rows.Add(4, "Robert Schidner", "Russia", "1254577");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as GridView).SelectedRow as GridViewRow;
string barCode = (row.FindControl("hfBarcode") as HiddenField).Value;
System.Web.UI.WebControls.Image imgBarCode = row.FindControl("imgBarcode") as System.Web.UI.WebControls.Image;
using (Bitmap bitMap = new Bitmap(barCode.Length * 20, 40))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), _
New DataColumn("Name", GetType(String)), _
New DataColumn("Country", GetType(String)), _
New DataColumn("Barcode", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States", "1241972")
dt.Rows.Add(2, "Mudassar Khan", "India", "7208922")
dt.Rows.Add(3, "Suzanne Mathews", "France", "2387841")
dt.Rows.Add(4, "Robert Schidner", "Russia", "1254577")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim row As GridViewRow = TryCast((TryCast(sender, GridView)).SelectedRow, GridViewRow)
Dim barCode As String = TryCast(row.FindControl("hfBarcode"), HiddenField).Value
Dim imgBarCode As System.Web.UI.WebControls.Image = TryCast(row.FindControl("imgBarcode"), System.Web.UI.WebControls.Image)
Using bitMap As New Bitmap(barCode.Length * 20, 40)
Using graphics__1 As Graphics = Graphics.FromImage(bitMap)
Dim oFont As New Font("IDAutomationHC39M", 16)
Dim point As New PointF(2.0F, 2.0F)
Dim blackBrush As New SolidBrush(Color.Black)
Dim whiteBrush As New SolidBrush(Color.White)
graphics__1.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
graphics__1.DrawString("*" & barCode & "*", oFont, blackBrush, point)
End Using
Using ms As New MemoryStream()
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Dim byteImage As Byte() = ms.ToArray()
Convert.ToBase64String(byteImage)
imgBarCode.ImageUrl = "data:image/png;base64," & Convert.ToBase64String(byteImage)
End Using
End Using
End Sub
Screenshot