Resizing Images Stored in SQL-Server

Resizing Images Stored in SQL-Server

Rate This
  • Comments 28

In SQL-Server we can store images inside database tables directly using the Image column type. And with .NET 2.0 data binding it can automatically convert these images (stored as byte arrays) into System.Drawing.Image classes for you. For instance, say you have a table called Pictures that has a column called Picture of the data type Image. You can create a new data source to this table to generate a strongly typed DataSet and use drag-and drop data binding to automatically display the image in a PictureBox. (For information on connecting to your database and creating strongly typed DataSets watch this video.)

If you don't see a PictureBox icon in the Data Sources window next to the Picture column in your DataTable, you may need to select the PictureBox to associate the byte array with the control:


Now you can drag-and-drop the Picture onto your form to set up the data binding to the PictureBox. The binding will automatically handle converting the byte array stored in the column into an image using the System.Drawing.ImageConverter. You can then set the SizeMode property on the PictureBox depending on how you want the image displayed; resized, stretched or otherwise.

But what if you just want to resize the image and not use a PictureBox? In that case you just need to convert the byte array stored in the Picture column into an System.Drawing.Image. Once you have that then you can use methods on this Image class to perform all sorts of transformations. To resize the image, just call GetThumbnailImage and pass it the new size requirements:

Dim resizeImg, origImg As Image


'Get the current row

Dim row As PictureDemoDataSet.PictureRow

row = CType(CType(Me.PictureBindingSource.Current, DataRowView).Row, _



'Convert byte array to image

Using ms As New System.IO.MemoryStream(row.Picture)

    origImg = Image.FromStream(ms)


    Dim width As Integer = 25

    Dim height As Integer = 25


    'Resize image

    resizeImg = origImg.GetThumbnailImage(width, height, Nothing, Nothing)

End Using

I've attached a simple application that demonstrates these techniques for you to play with. You'll need Visual Basic 2005 and SQL Server 2005 (or Express) to run.


Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
  • Hi Beth,

    I have a question about binding a PictureBox to a datasource on a different form. Right now I am using this method to bind the other controls but it is not working for the PictureBox control.

    Me.TextBox1.DataBindings.Add _

           (New Binding("Text", Form1.MyBindingSource _

           , "Field", False, DataSourceUpdateMode.OnValidation)

    I know this is usually not a good idea to bind controls on other forms but in my case it beats the alternative. That said, do you have any suggestions?

  • Hi Jeremy,

    Check out my post on working with data between multiple forms:



  • Hi Beth,


    I did the illustrated tut. about binding image as shown above, but it doesn't store the image to the data base, i think it doesn't convert the image to bytes array to be transfered to data base. also i don't know wht to do.

    please Beth help me.

  • Hi beth,

    Could you help me? How can I display image from access DataBAse to report? I have in filed path string


  • hi beth massi can u help me in deisgn of report viwer

    i  have sevral problems in this my email is given in the url i want to make checkbox

  • Hi mahmoudfr,

    Take a look at this video on using the report viewer:

    You may also want to ask your question in one of the forums:





  • Good guide ,  Your videos on database ect have been a  really big help.  Im new to vb and want to Convert images to byte arrays so i can store them in my database  But have been unable to find a simple guide.  

    Im conecting to the database though the wizard in vb 2008 like your examples do.  Could someone point me in the right direction as i had no luck on forums.



  • Hi Beth. I'm having a problem with a bound datagridview. It is bound to an access table and it displays all the record on the table it was bound to correctly. However, I need to filter the data that it displays and I tried doing that using the Query Builder. What I did was go to the Filter Properties, set parameter Identification prefix to @ and put a parameter @proj_ID in the filter column. When i click execute sql in query builder, it works fine but when i exit, it gives me errors like "Unable to parse query text,..blah blah, and others give no value given for one or  more required parameters... Hope you can help me with this..

  • Hi Beth, nice to read your article.

    I have a question dealing with your article: how to convert byte to image.

    I have code below. I can't display my picture field.

    Dim List As List(Of room) = New List(Of room)

    Dim r as new room

    Using DRs = Coms.ExecuteReader

    While DRs.Read

    r.ID = DRs(0).ToString

    r.Name = DRs(1).ToString

    r.Price= CInt(DRs(2))

    r.Picture = DRs(3)  ''this is error= unable to cast object of type 'system.byte[]' to type 'system.drawing.image'

    End While

    End Using

    I hope you would give me a solution about my error code.



    I wanna ask how to save image with varbinary type, besides image type in SQL Server.

    I wait your comment.

  • HI BETH.

    I still wait your comment. Recently I try to work with image by VB 2005 so I wish you are willing to answer my question


  • Hi Beth,

    I am new to VB 2008 and want to know if you could please tell me about:

    How to add picture in SQL server table using VB windows form picture control.

    Regards with Thanks,


  • Hi Beth, I'm using VB 2008 and Binding my PictureBox.image to my DataContext Binding Table- Picture, but in my form I can see data linke FirstName, LastName, but not the photo, I can navigate, but i can't see any photo. In VB2005 the same type of binding works but not in VB2008 using datacontext, Do yo have some material about this?

    Thanks for your help

Page 2 of 2 (28 items) 12