Hello!
Good Day to all,
I have a VB.Net windows form project with MS SQL Server as back end database. I am storing images to database from the application. This is required by client, for example when a user posts a Payment Voucher then he needs to store some manual documents as supporting proofs of the transaction. Everything is going fine. Images are stored and retrieved perfectly. But due to this the size of the database is increasing much more than expected.
Now i want to know how can i do the same task occupying the database storage as minimum as possible.
If anyone can help in this regard, I will be very grateful.
Thanks and regards.
What I have tried:
Firstly i insert a picture in PictureBox control by windows browsing using OpenFileDialog.
Public Sub Select_Picture()
Try
OpenFile.ShowDialog()
Dim PicPath As String = OpenFile.FileName
MyPicBox.ImageLocation = PicPath
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Then to store the image i use the following code.
Public Sub SaveImage_()
Dim SQL_Connection As New SqlConnection
Try
Dim MyImage As Image
MyImage = MyPicBox.Image
SQL_Connection.ConnectionString = MyConnectionString
SQL_Connection.Open()
Dim MyQuery As String = "insert into tbl_Setup_Companies Values(@Image_)"
Dim MyCommand As SqlCommand
MyCommand = New SqlCommand(MyQuery, SQL_Connection)
MyCommand.Parameters.Add(New SqlParameter("@Image_", SqlDbType.Image))
MyCommand.Parameters("@Image_").Value = ConvertImage(MyImage)
MyCommand.ExecuteNonQuery()
SQL_Connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
To convert the Image into Byte, I wrote a following function named "ConvertImate"
Public Function ConvertImage(ByVal myImage As Image) As Byte()
Dim mstream As New MemoryStream
myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim myBytes(mstream.Length - 1) As Byte
mstream.Position = 0
mstream.Read(myBytes, 0, mstream.Length)
Return myBytes
End Function