VB.NET•Load an Image Into a PictureBox

Listing 3. You're able to load an image from SQL Server into a PictureBox and create a thumbnail of it at the same time. Reversing the process of loading an image into SQL Server, you read the data from SQL Server into a binary MemoryStream. From there, you can use the FromStream method of the Image class to display the image in a PictureBox control.

Private Sub DisplayImages _
   (ByVal ImageID As String)

   Dim bw As New BinaryWriter _
      (New MemoryStream)

   Dim dr As SqlDataReader = _
      GetImages(ImageID)

   While dr.Read
      bw.Write(dr("Image"))

      PictureBox1.Image = _
         Image.FromStream _
         (bw.BaseStream, True)

      ' Display the image Height and Width
      With lblInfo
         .Text = "Height: " & _
            Image.FromStream _
            (bw.BaseStream).Height & _
            ControlChars.CrLf

         .Text &= "Width: " & _
            Image.FromStream _
            (bw.BaseStream).Width & _
            ControlChars.CrLf
      End With

      ' Show a Thumbnail
      PictureBox2.Image = _
         Image.FromStream _
         (bw.BaseStream). _
         GetThumbnailImage _
         (125, 110, Nothing, _
         System.IntPtr.Zero)

   End While

End Sub