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.
Create a new C#/VB.NET windows application. Add references to the following dlls to your project: 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.
Assuming you installed LEADTOOLS in "C:\Program Files", then the DLLs will be located in the following path:
C:\Program Files\LEAD Technologies\LEADTOOLS 15\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.
Add the LEADTOOLS .NET controls to your VS.NET toolbox by right-clicking the toolbox and selecting "Choose Items". Browse to Leadtools.Winforms.dll as specified in the previous step. Add the RasterImageViewer control to the form by dragging and dropping from the toolbox. Add the following lines at the top of the Form1.cs file: C#
VB.NETusing System.Data.SqlClient; using Leadtools.Codecs; using System.IO;
Imports System.Data.SqlClient Imports Leadtools.Codecs Imports System.IO
Add the following members to the Form1 class: C#
VB.NETprivate SqlConnection connection; private SqlCommand command;
Private connection As SqlConnection Private command As SqlCommand
Add the following code to the Load event of the form: C#
VB.NETRasterCodecs.Startup(); 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;
RasterCodecs.Startup 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
Add a button to the form and set its Text property to Save Image To DB, then double-click it and add the following code to its hander: C#
VB.NETRasterCodecs 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(); }
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
Add a button to the form and set its Text property to Load Image From DB, then double-click it and add the following code to its handler: C#
VB.NETcommand.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(); }
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
Run your project to test it.