Load Images from and Save Images to a Database

Sometimes you need to store images in a database instead of as physical files. This sample application will show you how to build a Windows Forms interface that allows you to do the following:

  • Browse for an image on your hard disk
  • Load the selected image into a PictureBox control for viewing
  • Save an image displayed in the PictureBox control to the database
  • Select an image from a ListBox control, and load it from the database

New Concepts

The new concepts in this article center around the abstract Stream class and how it's used to convert an image file to and from the Image data type that SQL Server uses to store images. Be sure not to confuse the Image data type with the word image, as if to imply that only images can be stored therein. Rather, the Image data type can store anything as variable-length binary data.

A byte array is used to send data to an Image field. Thus, the main question is: How does one convert an image file—whether a JPEG, Bitmap, or other format—into an array of bytes? There are several ways to accomplish this in .NET. One of the easiest ways is to use a concrete implementation of the Stream class. A stream in .NET is essentially an abstraction of a sequence of bytes, whether these bytes came from a file, a TCP/IP socket, a database, or wherever. Stream classes allow you to work with binary data, reading and writing back and forth between streams and data structures (such as a byte array).

Once the image is converted to a byte array, it's saved to a database by using coding.

Creating Database

The first step you have to do is to create a Database table name it Pic, which should contain the two, fields 1: Name 2: Picture. The data Type of the Name field is (nVarChar) and data type of Picture is (Image) in Sql Server 2000.

Note:This Database should be in SQLS erver. I have included the database file in the zip file that you can attach in SQL Server databases. The name of database file is Images_Data.

Browsing for and Displaying an Image

The first task is to find an image on your hard disk. To do this, use an OpenFileDialog object in conjunction with a standard Button control. In the btnBrowse_Click event handler, you can see how this is done. The first few lines of code merely set properties of the OpenFileDialog object.

With OpenFileDialog1
   .InitialDirectory = "C:\"
   .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
   .FilterIndex = 2
End With

A pipe-delimited pair of file types is provided to determine the valid file types that can be accessed through the dialog box. Among other properties, you can also set FilterIndex to the default file type that you want to appear in the dialog box's Files Of Type menu. The index is not zero-based, so in this example, Bitmaps will appear as the default.

The dialog box is not actually opened until its ShowDialogmethod is called, which can be combined in an If/Then statement to check which button was pressed and perform follow-on tasks:

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
   With PictureBox1
      .Image = Image.FromFile(Me.OpenFileDialog1.FileName)
      .SizeMode = PictureBoxSizeMode.CenterImage
   End With

End If
Me.Label1.Text = Me.OpenFileDialog1.FileName.ToString

Although an OpenFileDialog object contains an Open button instead of an OK button, there is no DialogResult enumeration for the Open button. Instead, use the OK enumeration. Once it's confirmed that the Open button has been clicked, properties of the PictureBox control are set. Notice how the Image property—which requires an object of type System.Drawing.Image—is assigned. The Image class is abstract and exposes a number of shared methods for working with images, one of which is FromFile. This method creates an Image object from a fully qualified path; although the OpenFileDialog.FileName property might lead you to think that it contains only the file name, it actually has the full path.

Now that your image file is represented by an Image object, you can use a stream to convert it to a byte array. In the btnSave_Click event handler, the first line of code creates a MemoryStream object:

Dim ms As New MemoryStream()

A MemoryStream object is simply a stream that uses memory as its backup store instead of some other medium. As a result, a MemoryStream object usually provides better performance. Streams are flexible. You could, for example, have used a FileStream object to open the image file directly and read it in. There are certainly numerous other ways, too. The implementation here, however, is simple and straightforward.

The MemoryStream is then passed as an argument to the Save method, another member of the Image class. You can optionally pass the image format—for example, by accessing the Image's read-only RawFormat property:

picSave.Image.Save(ms, picSave.Image.RawFormat)

The actual byte array conversion comes in the next line. GetBuffer returns an array of unsigned bytes being held by the stream.

Dim arrImage() As Byte = ms.GetBuffer
ms.Close()    'It is good to always close the stream rather than
              ' to leave it for the garbage collector

The last data-gathering task is to extract the filename from the full path; there is no need to store the entire path in the database:

Dim strFilename As String = _
    lblFilePath.Text.Substring(lblFilePath.Text.LastIndexOf("\")"+"1)

This might look a bit complex and convoluted, but all you're doing is indicating that you want a substring of the full path that starts after the last backslash.

With the filename extracted and the image converted to a byte array, you're now ready to use the ADO.NET practices you've already learned to push these to the database.

Dim cnn As New SqlConnection(connectionString)
Dim strSQL As String = _
   "INSERT INTO Picture (Filename, Picture)" & _
   "VALUES (@Filename, @Picture)"
Dim cmd As New SqlCommand(strSQL, cnn)

With cmd
   .Parameters.Add(New SqlParameter("@Filename", _
      SqlDbType.NVarChar, 50)).Value = strFilename
   .Parameters.Add(New SqlParameter("@Picture", _
      SqlDbType.Image)).Value = arrImage
EndWith
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()

As you can see, at this point there is nothing new except the use of the SqlDbType.Image enumeration. Set the value of the @Picture parameter to the byte array, and execute the INSERT statement as you would with any other type of data.

Reading an image

From this point forward, you're essentially reversing the process. To display an image, you have to convert it from a byte array to an Image, and then assign it to the PictureBox.Image property:

Behind the ClickImagesInDatabase button, write this code:

Me.SqlConnection1.Open()
   Me.SqlDataAdapter1.Fill(Me.DataSet11.Pic)

   With Me.ListBox1
      .DataSource = Me.DataSet11.Pic
      .DisplayMember = "Name"
   End With
   Me.SqlConnection1.Close()

Choose from the SelectedIndexChanged Event from the Listbox event and write the code in the subroutine body:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, _
                                          ByVal e As System.EventArgs) _
                     Handles ListBox1.SelectedIndexChanged

Dim arrayImage() As Byte =

CType(Me.DataSet11.Tables(0).Rows(Me.ListBox1.SelectedIndex) _
                                 ("Picture"), Byte())
   Dim ms As New MemoryStream(arrayImage)
   With Me.PictureBox1
      .Image = Image.FromStream(ms)
      .SizeMode = PictureBoxSizeMode.CenterImage

   End With
End Sub

The SelectedIndex property of the ListBox control is used to to retrieve the contents of the associated Picture field in the DataSet object, which is then explicitly cast to a byte array. Following this, a MemoryStream is created by passing the byte array to its constructor. The last step is to invoke the shared FromStream method to convert the stream contents to an Image, and then assign this to the PictureBox.Image property.

Note: I have generated DataAdapter and Dataset via a wizard, but you can create the Dataset and DataAdapter objects explicitly and perform the required task.

References

Various Microsoft Press Books.



About the Author

Fatima Ahmed

Fatima Ahmed is a B.S. in Computer Sciences and recently working in VC++ and .NET applications. Her interests include teaching, helping others, travelling, writing articles on different topics and studying more and more.

Downloads

Comments

  • ghd es el pináculo absoluto de peluquería

    Posted by fyfuvg087 on 07/25/2013 08:45pm

    Somos pelo ghd planchas profesionales proveedores, tenemos muchos años de experiencia en la venta ghd. La comprensión de un gran número de personas pueden comprar problemas de productos. Más profesional para responder a sus preguntas. Hasta que esté satisfecho. Nuestra paciencia y cuidado, esto te hará sentir como si compra ghd es el más valioso. Puesto que vendemos muchos tipos de planchas ghd, y a todo color. Tipo que usted elija, tomar decisiones precisas para que no causan el mismo para usted. La mayoría de la gente se preguntará cómo se parece el precio? You Responder en este momento, nuestro precio es el más barato, usted puede buscar a través de, planchas GHD pertinentes para encontrar más tiendas, compara Internet to.du encontrará que nuestros productos son bedste.vi tiene un montón de ventajas para usted. Estamos aquí para comprar sus planchas de pelo ghd, se puede disfrutar de envío gratis. Si usted necesita preocuparse, ponemos a su disposición con los otros dos métodos. Para ir rápidamente a las manos. Cuando usted compra, usted no debe utilizar este producto, nos enseñará el método utilizado. Para ofrecerle un servicio de alta calidad. Si no te gusta para comprar productos, para que puedas ver nuestra política. Para el procesamiento. Planchas ghd en el producto, si usted tiene preguntas adicionales, por favor póngase en contacto con nosotros. Gracias por su apoyo y aliento de nuestros productos, creemos que la elección de planchas ghd, que son una hermosa opción. [url=http://ghdplanchasonline.manifo.com/]planchas ghd[/url] A planchas ghd es uno de los alisadores de pelo más avanzados tecnológicamente en el mercado, pero eso no quiere decir que es difícil de usar ofertas plancha GHD. Sólo quiere decir que es un muy buen alisadores! Por ejemplo. es una ghd sólo 20 segundos para calentar a la temperatura óptima, y el termostato digital asegura la temperatura se ajusta hasta 5 veces por segundo, por lo que su ghd nunca pierden calor o overopheder.Det significa que una ghd no sólo es fácil de utilizar - sino que también proporciona un resultado perfecto cada vez - incluyendo también a causa de las placas flexibles, asegurando que usted no hace "crack" en el pelo cuando suaviza, rizos o hacer el movimiento en håret.Desuden es una ghd diseñado para ser tan suave como sea posible por el cabello, entre otros utilizando el llamado teknologi.Køber iones negativos que una ghd planchas baratas, usted puede estar seguro de que usted ha hecho una inversión que nunca se arrepentirá. [url=http://ghd-baratas.manifo.com/]Comprar GHD Planchas[/url] Incluso tengo un hecho BABYLISS yo hago una buena capa con una línea de rotación correspondiente al control de la temperatura y la deficiencia de la hormona del crecimiento performance, peeeero su tambor redondo es un dolor y el costo real, por lo que las olas o el pelo rizado ... La diferencia es comprobar en el último Hoy en día, fui a mi habitual estilista y le pregunté su cabello ondularan y eliminar un déficit de hormona de crecimiento (GOLD modelo Classic), fue una verdadera sorpresa al ver lo cómodo que parece utilizar la velocidad de la rapidez final, un resultado muy profesional .

    Reply
  • Hnqkpwdw

    Posted by frevioure on 07/22/2013 01:20pm

    Dk borse burberry Hw hogan scarpe Pu http://www.infozentralschweiz.ch/link.php Xv louis vuitton online shop Rt louis vuitton outlet Zn

    Reply
  • Iguzvgpr

    Posted by shooriuch on 07/17/2013 08:04pm

    eqh burberry soldes qgr hermes birkin twf louis vuitton handbags kks oakley hkk christian louboutin uk ulw.

    Reply
  • Xhgkxhcd

    Posted by shooriuch on 07/17/2013 10:07am

    Favor, that's great! ysl handbags uk moncler jacket miu miu bag sale bottega veneta uk karen millen

    Reply
  • csklke

    Posted by ReliticyRek on 07/17/2013 12:28am

    rmw chaussure louis vuitton eiv hogan milano kog louis vuitton borse outlet wbt christian louboutin ihp mulberry tree dkr

    Reply
  • Vwdpdcty

    Posted by shooriuch on 07/16/2013 07:56pm

    ffc ysl bags outlet dld cheap moncler gvt miu miu bag uk fev bottega veneta sale ipc karen millen online wzl.

    Reply
  • Fntplq

    Posted by viellioma on 07/16/2013 12:20pm

    hef Scarpe Hogan uqa louis vuitton shoes pud christian louboutin sale wjh gucci toh louis vuitton outlet dwf.

    Reply
  • Mjztfwa

    Posted by VaRReambcax on 07/16/2013 10:27am

    Yhl mulberry Bbh christian louboutin Woh louis vuitton bags Txq mulberry bags Xey ralph lauren polo shirts Csi.

    Reply
  • Ffqppfvq

    Posted by shooriuch on 07/16/2013 05:46am

    gmr burberry soldes hyw ceinture hermes jbi louis vuitton handbags mis lentes oakley hua christian louboutin shoes trz.

    Reply
  • Vfhgiib

    Posted by ReliticyRek on 07/16/2013 01:44am

    ltb sac louis vuitton xop ralph lauren home sba borse louis vuitton zqd gafas de sol ray ban djp oakley gafas hud.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds