Hi smile,
Refer below sample.
Namespaces
C#
using System.IO;
using System.Drawing.Imaging;
using System.Data.SqlClient;
VB.Net
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Data.SqlClient
Code
C#
public Form1()
{
InitializeComponent();
string constr = @"Server=\SQL2005;DataBase=Northwind;UID=sa;PWD=pass@123";
DataTable dt;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, ContactName FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
dt = new DataTable();
da.Fill(dt);
}
}
}
DataTable dtfinal = new DataTable();
dtfinal.Columns.AddRange(new DataColumn[] { new DataColumn("Image", typeof(Bitmap)), new DataColumn("CustomerId", typeof(string)), new DataColumn("Name", typeof(string)) });
for (int i = 0; i < dt.Rows.Count; i++)
{
dtfinal.Rows.Add(GenerateBarCode(dt.Rows[i]["CustomerId"].ToString()), dt.Rows[i]["CustomerId"].ToString(), dt.Rows[i]["ContactName"].ToString());
}
dataGridView1.DataSource = dtfinal;
dataGridView1.Columns[0].Width = 100;
dataGridView1.Columns[1].Width = 50;
dataGridView1.Columns[2].Width = 150;
}
private Bitmap GenerateBarCode(string barCode)
{
Bitmap bitMap = new Bitmap(barCode.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("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, ImageFormat.Png);
}
return bitMap;
}
private void Save(object sender, System.EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string id = row.Cells[0].Value.ToString();
string name = row.Cells[1].Value.ToString();
byte[] byteImage;
Bitmap bitMap = GenerateBarCode(id);
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byteImage = ms.ToArray();
}
string constr = @"Server=.\SQL2005;DataBase=dbFiles;UID=sa;PWD=pass@123";
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles values (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", name.Trim() + ".jpeg");
cmd.Parameters.AddWithValue("@ContentType", "image/jpeg");
cmd.Parameters.AddWithValue("@Data", byteImage);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Height = 100;
}
}
VB.Net
Public Sub New()
InitializeComponent()
Dim constr As String = "Server=\SQL2005;DataBase=Northwind;UID=sa;PWD=pass@123"
Dim dt As DataTable
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, ContactName FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
dt = New DataTable()
da.Fill(dt)
End Using
End Using
End Using
Dim dtfinal As DataTable = New DataTable()
dtfinal.Columns.AddRange(New DataColumn() {New DataColumn("Image", GetType(Bitmap)), New DataColumn("CustomerId", GetType(String)), New DataColumn("Name", GetType(String))})
For i As Integer = 0 To dt.Rows.Count - 1
dtfinal.Rows.Add(GenerateBarCode(dt.Rows(i)("CustomerId").ToString()), dt.Rows(i)("CustomerId").ToString(), dt.Rows(i)("ContactName").ToString())
Next
dataGridView1.DataSource = dtfinal
dataGridView1.Columns(0).Width = 100
dataGridView1.Columns(1).Width = 50
dataGridView1.Columns(2).Width = 150
End Sub
Private Function GenerateBarCode(ByVal barCode As String) As Bitmap
Dim bitMap As Bitmap = New Bitmap(barCode.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("*" & barCode & "*", oFont, blackBrush, point)
End Using
Using ms As MemoryStream = New MemoryStream()
bitMap.Save(ms, ImageFormat.Png)
End Using
Return bitMap
End Function
Private Sub Save(ByVal sender As Object, ByVal e As System.EventArgs)
For Each row As DataGridViewRow In dataGridView1.Rows
Dim id As String = row.Cells(0).Value.ToString()
Dim name As String = row.Cells(1).Value.ToString()
Dim byteImage As Byte()
Dim bitMap As Bitmap = GenerateBarCode(id)
Using ms As MemoryStream = New MemoryStream()
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
byteImage = ms.ToArray()
End Using
Dim constr As String = "Server=.\SQL2005;DataBase=dbFiles;UID=sa;PWD=pass@123"
Using con As SqlConnection = New SqlConnection(constr)
Dim query As String = "insert into tblFiles values (@Name, @ContentType, @Data)"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Name", name.Trim() & ".jpeg")
cmd.Parameters.AddWithValue("@ContentType", "image/jpeg")
cmd.Parameters.AddWithValue("@Data", byteImage)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub
Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)
For i As Integer = 0 To dataGridView1.Rows.Count - 1
dataGridView1.Rows(i).Height = 100
Next
End Sub
Screenshot