Take the following steps to start a project and to add some code that demonstrates writing images to or reading images from the BLOB field of a database.
This tutorial assumes you have Microsoft SQL Server installed on your machine, and uses classes from the System.Data.SqlClient namespace for data access. If you are connecting to a DBMS other than SQL Server, then use the OleDb namespace and its corresponding classes instead of SqlClient.
To use this tutorial, you need to create a database named SampleDB and a table named Images with a field named Picture of type Image.
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Winforms.dll
Leadtools.Codecs.Cmp.dll
Note that the last DLL is for loading/saving CMP and JPEG files only. To load/save other formats, add the necessary Leadtools.Codecs.* DLLs. Refer to the Files To Be Included With Your Application topic for more information on which DLLs are reqired for eah format.
The DLLs will be located in the following path:
<LEADTOOLS_INSTALLDIR>\Bin\Dotnet
If you are programming in 32 bits, then use the DLLs in the Win32 folder. If you are programming in 64 bits, then use the DLLs in the x64 folder.
C#
using System.Data.SqlClient;
using Leadtools.Codecs;
using System.IO;
VB.NET
Imports System.Data.SqlClient
Imports Leadtools.Codecs
Imports System.IO
C#
private SqlConnection connection;
private SqlCommand command;
VB.NET
Private connection As SqlConnection
Private command As SqlCommand
C#
RasterCodecs codec = new RasterCodecs();
rasterImageViewer1.Image = codec.Load(@"C:\Image.jpg");
connection = new SqlConnection("server=localhost\\SQLExpress;" + "Trusted_Connection=yes;" + "database=SampleDB;" + "connection timeout=30");
command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.Text;
VB.NET
Dim codec As RasterCodecs = New RasterCodecs()
rasterImageViewer1.Image codec.Load("C:\Image.jpg")
connection = new SqlConnection("server=localhost\SQLExpress;" & "Trusted_Connection=yes;" & "database=SampleDB;" & "connection timeout=30")
command = New SqlCommand()
command.Connection = connection
command.CommandType = CommandType.Text
C#
RasterCodecs codec = new RasterCodecs();
MemoryStream stream = new MemoryStream();
codec.Save(rasterImageViewer1.Image, stream, Leadtools.RasterImageFormat.Jpeg, 24);
byte[] array = stream.GetBuffer();
command.CommandText = "insert into Images (Picture) values (@Picture);";
SqlParameter Parameter = new SqlParameter("@Picture", SqlDbType.VarBinary, array.Length, ParameterDirection.Input, false, 0, 0, "Picture", DataRowVersion.Current, array);
command.Parameters.Add(Parameter);
try
{
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("Image Saved Successfully");
rasterImageViewer1.Image = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
VB.NET
Dim codec As RasterCodecs = New RasterCodecs()
Dim stream As MemoryStream = New MemoryStream()
codec.Save(rasterImageViewer1.Image, stream, Leadtools.RasterImageFormat.Jpeg, 24)
Dim array As Byte() = stream.GetBuffer()
command.CommandText = "insert into Images (Picture) values (@Picture);"
Dim Parameter As SqlParameter = New SqlParameter("@Picture", SqlDbType.VarBinary, array.Length, ParameterDirection.Input, False, 0, 0, "Picture", DataRowVersion.Current, array)
command.Parameters.Add(Parameter)
Try
connection.Open()
command.ExecuteNonQuery()
MessageBox.Show("Image Saved Successfully")
rasterImageViewer1.Image = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Close()
End Try
C#
command.CommandText = "select Picture from Images";
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
reader.Read();
long length = reader.GetBytes(0, 0, null, 0, Int32.MaxValue);
Byte[] array = new Byte[length];
reader.GetBytes(0, 0, array, 0, array.Length);
MemoryStream stream = new MemoryStream(array);
RasterCodecs codec = new RasterCodecs();
rasterImageViewer1.Image = codec.Load(stream);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
VB.NET
command.CommandText = "select Picture from Images"
Try
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader()
reader.Read()
Dim length As Long = reader.GetBytes(0, 0, Nothing, 0, Int32.MaxValue)
Dim array As Byte() = New Byte(Convert.ToInt32(length) - 1){}
reader.GetBytes(0, 0, array, 0, array.Length)
Dim stream As MemoryStream = New MemoryStream(array)
Dim codec As RasterCodecs = New RasterCodecs()
rasterImageViewer1.Image = codec.Load(stream)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Close()
End Try