Loading and Saving Images Using Databases Without Data Binding

Show in webframe

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.

  1. Create a new C#/VB.NET windows application.
  2. 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.

    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.

  3. 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.
  4. Add the RasterImageViewer control to the form by dragging and dropping from the toolbox.
  5. Add the following lines at the top of the Form1.cs file:

    C#

    
                using System.Data.SqlClient;
                using Leadtools.Codecs;
                using System.IO;
                
    
    VB.NET
    
                Imports System.Data.SqlClient
                Imports Leadtools.Codecs
                Imports System.IO
                
    
  6. Add the following members to the Form1 class:

    C#

    
                private SqlConnection connection;
                private SqlCommand command;
                
    
    VB.NET
    
                Private connection As SqlConnection
                Private command As SqlCommand
                
    
  7. Add the following code to the Load event of the form:

    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
                
    
  8. 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:

    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
                
    
  9. 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#

    
                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
                
    
  10. Run your project to test it.

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.