Hi Muhammad12,
Please refer below Sample.
Database
I have made use of the following table tblFileswith the schema as follows.
For more detail please refer below Article.
HTML
Default
<asp:Button Text="Save" ID="btnSave" runat="server" OnClick="Save" />
Home
<asp:Panel runat="server" ID="pnlDetails">
<asp:Label runat="server" ID="lblId"></asp:Label><br />
<asp:Label runat="server" ID="lblName"></asp:Label><br />
<asp:Image ID="imgBarcode" runat="server" />
</asp:Panel>
<asp:Button Text="Print" runat="server" OnClientClick="return PrintPanel()" />
<script type="text/javascript">
function PrintPanel() {
var contents = document.getElementById('pnlDetails').innerHTML;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head>');
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
return false;
}
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing
Imports System.IO
Code
C#
Default
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 Id, Name, ContentType, Data FROM tblFiles", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void Save(object sender, EventArgs e)
{
DataTable dt = GetData();
dt.Columns.Add("Barcode", typeof(byte[]));
foreach (DataRow dr in dt.Rows)
{
string id = dr["Id"].ToString();
string name = dr["Name"].ToString();
byte[] bytes = GetBarcodeByte(id);
dr["Barcode"] = bytes;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblFiles (Name, ContentType, Data) VALUES(@Name, @ContentType, @Data)", con))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@ContentType", "image/png");
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Session["Data"] = dt;
Response.Redirect("~/Home.aspx");
}
private byte[] GetBarcodeByte(string id)
{
byte[] byteImage;
using (Bitmap bitMap = new Bitmap(id.Length * 40, 80))
{
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("*" + id + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteImage = ms.ToArray();
}
}
return byteImage;
}
Home
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = Session["Data"] as DataTable;
lblId.Text = dt.Rows[0]["Id"].ToString();
lblName.Text = dt.Rows[0]["Name"].ToString();
string imageUrl = "data:image/png;base64," + Convert.ToBase64String(dt.Rows[0]["Barcode"] as byte[]);
imgBarcode.ImageUrl = imageUrl;
}
VB.Net
Default
Private Function GetData() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 1 Id, Name, ContentType, Data FROM tblFiles", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData()
dt.Columns.Add("Barcode", GetType(Byte()))
For Each dr As DataRow In dt.Rows
Dim id As String = dr("Id").ToString()
Dim name As String = dr("Name").ToString()
Dim bytes As Byte() = GetBarcodeByte(id)
dr("Barcode") = bytes
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblFiles (Name, ContentType, Data) VALUES(@Name, @ContentType, @Data)", con)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@ContentType", "image/png")
cmd.Parameters.AddWithValue("@Data", bytes)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
Session("Data") = dt
Response.Redirect("~/Home.aspx")
End Sub
Private Function GetBarcodeByte(ByVal id As String) As Byte()
Dim byteImage As Byte()
Using bitMap As Bitmap = New Bitmap(id.Length * 40, 80)
Using graphics As Graphics = Graphics.FromImage(bitMap)
Dim oFont As Font = New Font("IDAutomationHC39M", 16)
Dim point As PointF = New PointF(2.0F, 2.0F)
Dim blackBrush As SolidBrush = New SolidBrush(Color.Black)
Dim whiteBrush As SolidBrush = New SolidBrush(Color.White)
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
graphics.DrawString("*" & id & "*", oFont, blackBrush, point)
End Using
Using ms As MemoryStream = New MemoryStream()
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
byteImage = ms.ToArray()
End Using
End Using
Return byteImage
End Function
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = TryCast(Session("Data"), DataTable)
lblId.Text = dt.Rows(0)("Id").ToString()
lblName.Text = dt.Rows(0)("Name").ToString()
Dim imageUrl As String = "data:image/png;base64," & Convert.ToBase64String(TryCast(dt.Rows(0)("Barcode"), Byte()))
imgBarcode.ImageUrl = imageUrl
End Sub