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

      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! 

Attachment: PictureResizing.zip
Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • 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

  • 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?

  • 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

  • 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  

  • 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

  • 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

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

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

  • Hi Ian,

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

    Cheers,

    -B

  • Hi Beth,

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

    Best Regards,

    Ian

  • 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

  • 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

  • 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

  • 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

  • Hi Beth,

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

    Thanks for the support,

    Ian

Page 1 of 2 (28 items) 12