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.
Prerequisite
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 ofSqlClient
.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
.
Add references to the following DLLs to your project:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Controls.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 required for each format.
The DLLs are located in the following path:
\Bin\Dotnet4
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 following lines at the top of the Form1.cs file:
using 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:
private SqlConnection connection;
private SqlCommand command;
Private connection As SqlConnection
Private command As SqlCommand
Add the following code to the Load event of the form:
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;
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 handler:
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();
}
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:
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();
}
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.
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document