Resizing Images Stored in SQL-Server

Published 28 August 07 11:35 AM

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, _

      PictureDemoDataSet.PictureRow)

 

'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.

Enjoy! 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# EL said on August 30, 2007 8:57 PM:

THANKS FOR "THE REZISING IMAGE STORED IN SQL SERVER"  YOU 'VE MADE.

BUT COULD YOU TELL ME HOW TO ADD/EDIT IMAGE IN THE PICTUREBOX AND SAVED IN SQL SERVER IN IMAGE DATA TYPE?

I CAN CONNECT TO DATABASE, MAKE NEW DATASOURCE AND DRAG & DROP TO CREATE DATABINDING BUT I STILL DIDN'T KNOW MANIPULATE IMAGES.

THANKS

# Hal Taback said on September 2, 2007 10:56 AM:

Beth,  you continue to add to the enjoyment of my retirement day with your interesting posts and videoes.

I recently completed a voice recognition project using an object I downloaded from the net.  After buying my new Dell, I downloaded ORCAS and converted the project from express 2005 to 2008 and haven't been able to get it to work.  I see that many people on the net have similiar interests.  Could you post an example of a voice recognition program?

# Beth Massi said on September 4, 2007 12:26 PM:

Hi El,

You just have to change the Image property of the picturebox and then call the BindingSource.EndEdit and then the TableAdapter.Update just like normal to update the data in your database.

Cheers,

-B

# Sabino said on September 5, 2007 10:18 AM:

I have same problem as EL.

I tried to load image from file using picturebox.load() method without no results. Maybe the Load method doesn't change image property correctly...

I don't know  

# Sabino said on September 5, 2007 11:06 AM:

OK BETH. Very well!!!

I solved the problem.

The solution is in the image porperty, updated through an instruction like this:

picturebox1.Image=System.Drawing.Image.Fromfile(filename)

Very thanks

# Beth Massi said on September 5, 2007 11:14 AM:

Hi Sabino,

As I mentioned in the previous comment, you just have to set the Image property of the picturebox and then call the BindingSource.EndEdit and then the TableAdapter.Update just like you do to update other data-bound fields. The Image property is the property that is participating in the data binding when you drag-drop the image onto the form.

-B

# Beth Massi - Sharing the goodness that is VB said on September 6, 2007 3:47 PM:

So I decided to post a summary of all the content the VB team members, including myself, have created

# Ian Mars said on September 14, 2007 5:17 AM:

Hi,

I am an Application Developer using VB.NET 2005 I had seen the movie you posted on your blog about the Forms on Data.

I wanted to know how to implement a search in a DataGridView during runtime. I have an options on the search like:

1. Ignore Case

2. No Options

3. Search Up

4. Search Down

These are the type of search I wanted to implement on my application.

Can you please give me an idea on how to do this.

I had made some reference on the sample codes that microsoft posted in there download site on VB.NET - specifically Regular Expressions in Search.

# Beth Massi said on September 14, 2007 12:48 PM:

Hi Ian,

You can start with this video on building a search form: http://msdn2.microsoft.com/en-us/vbasic/bb643828.aspx

Cheers,

-B

# Ian said on September 14, 2007 8:37 PM:

Hi Beth,

Thanks alot I did not see all the movies you posted, so it  can be seen in movie 4.

Best Regards,

Ian

# Ian said on September 14, 2007 10:14 PM:

Hi Beth,

I seen the movie on adding search capabilities, It is quite blurry and I want to know if this is the right command you entered in the query builder for the LastName in Filter:

LIKE @LastName * '%'

I only want to clarify things up cause I only get the FillbyLastLastName button and I didn't get the textbox in which the user will enter the LastName.

And another question:

How to assign the column when doing a query using select, I mean how to assign column(do I use @ after the field name), I know how to use the SELECT command like this?

SELECT column_name FROM table_name

WHERE column_name = constraints

# Ian said on September 14, 2007 10:46 PM:

Hi,

When I do the WHERE clause in SELECT

I get a parameterized query error:

Error in WHERE clause near '@'.

Unable to parse query text.

I did it this way:

SELECT `Id`, `Surname`, `Firstname`, `Middle_Initial`, `Gender`, `Civil_Status`, `Address`, `Job_Position`, `SSS_No`, `TIN_No`, `PhilHealth`, `Pagibig`, `Date_of_Birth`, `Date_of_Employment`, `Employment_Status`, `Contact_No`, `Picture` FROM `british_armour`.`production_personnel`

WHERE 'Surname' = @Surname

is this correct, what is the right thing to do it.

please advise.

thanks,

ian

# Beth Massi said on September 17, 2007 4:07 PM:

Hi Ian,

That doesn't look like correct SQL syntax to me for SQL-Server (i.e. no quotes around field names and the table name looks incorrect). Have a look at SQL-Server books online for correct syntax: http://msdn2.microsoft.com/en-us/library/ms190659.aspx

Cheers,

-B

# Ian said on September 18, 2007 6:11 AM:

Hi Beth,

I made corrections to my query commands and I solved it.

However I have a new problem. I deployed my application for testing purposes and I used Innosetup Compiler 5.1.14 (http://www.jrsoftware.org/isdl.php).

Everything works fine after the installation. I deployed it on my development machine and I find no flaws.

The error comes up when I install it on another computer and when I click the report button which I created from the videos on your blog on how to create a report using ReportViewer I get an error message:

The error is: Could not load file or assembly 'Microsoft.ReportViewer.WinForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I already add Microsoft.ReportViewer.WinForms to the Reference. And also add a namespace -

Using Imports Microsoft.Reporting.Winforms

I still can't generate report on other machines,

I still get the same error,  but I can generate it on the machine in which I developed the software.

I will try the video you posted on deploying applications. The application will be use by multiple users on different departments.

I appreciate your help.

Thanks,

Ian

# Ian said on September 19, 2007 3:06 AM:

Hi Beth,

I solved my own problem regarding reporting. I let ReportViewer.exe be installed on their local machine.

Thanks for the support,

Ian

# Jeremy said on September 19, 2007 4:34 PM:

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?

# Beth Massi said on October 15, 2007 2:36 PM:

Hi Jeremy,

Check out my post on working with data between multiple forms: http://blogs.msdn.com/bethmassi/archive/2007/10/01/using-data-across-multiple-windows-forms.aspx

HTH,

-B

# M. Sharief said on October 18, 2007 6:55 AM:

Hi Beth,

greeting.

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.

# Jalal said on July 8, 2008 5:44 PM:

Hi beth,

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

thanks

# mahmoudfr said on July 13, 2008 1:13 AM:

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

# Beth Massi said on July 21, 2008 11:50 AM:

Hi mahmoudfr,

Take a look at this video on using the report viewer: http://msdn.microsoft.com/en-us/vbasic/bb643819.aspx

You may also want to ask your question in one of the forums: http://msdn.microsoft.com/en-us/vbasic/cc546545.aspx

HTH,

-B

HTH,

-B

# luke said on February 1, 2009 4:47 PM:

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.

cheers

luke

# samu said on February 5, 2009 5:08 PM:

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

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About Beth Massi

Beth is a Program Manager on the Visual Studio Community Team at Microsoft and is responsible for producing and managing content for business application developers, driving community features and team participation onto MSDN Developer Centers (http://msdn.com), and helping make Visual Studio one of the best developer tools in the world. She also produces regular content on her blog (http://blogs.msdn.com/bethmassi), Channel 9, and a variety of other developer sites and magazines. As a community champion and a long-time member of the Microsoft developer community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a Senior Architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, web and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and driving really fast.

This Blog

Syndication

Page view tracker