Dear All,
I want to save image from picture box into access database with size of 400x300, regardless of whatever the original size of picture in the picture box.
1). For example The Picture may be Desk or Chair. Chair size may be 50x75 varias Desk size might be 400x300. The rest area of Chair picture should be saved as transparent or white space into the database. So when we retreive the picture it won't be distorted.
Can anyone help me to solve this problem please?.
Following is the code which I'm using to save picture into access databse
Thanks a Lot in Advance
I want to save image from picture box into access database with size of 400x300, regardless of whatever the original size of picture in the picture box.
1). For example The Picture may be Desk or Chair. Chair size may be 50x75 varias Desk size might be 400x300. The rest area of Chair picture should be saved as transparent or white space into the database. So when we retreive the picture it won't be distorted.
Can anyone help me to solve this problem please?.
Following is the code which I'm using to save picture into access databse
VB.NET:
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim str As New MemoryStream
'save the image to the stream in .Jpeg format
pic.Image.Save(str, Imaging.ImageFormat.Jpeg)
Dim buffer(CInt(str.Length - 1)) As Byte
str.Position = 0
str.Read(buffer, 0, CInt(str.Length))
'create a connection to the images database in the root folder or our machine
Dim con As New OleDbConnection(conString)
'create a command object based on this command
Dim cmd As New OleDbCommand("UPDATE Products SET ProductID = @ProductID,ProductName = @ProductName,CategoryID = @CategoryID,ProductImage = @ProductImage WHERE (ProductID = @ProductID)", con)
cmd.CommandType = CommandType.Text
Dim prmPic As New OleDbParameter
'done step by step here
With prmPic
.ParameterName = "@ProductImage" 'the name used in the query for the parameter
.OleDbType = OleDbType.Binary 'set the database type
.Value = buffer 'assign the contents of the buffer to the value of the parameter
End With
cmd.Parameters.Add("@ProductID", OleDbType.VarChar, 20, "ProductID").Value = txtPartNo.Text
cmd.Parameters.Add("@ProductName", OleDbType.VarChar, 150, "ProductName").Value = txtDescription.Text
cmd.Parameters.Add("@CategoryID", OleDbType.Integer, 1, "CategoryID").Value = cboCategory.SelectedIndex
cmd.Parameters.Add(prmPic) 'add the parameter to the command
Try
'open the connection
con.Open()
'execute the command
cmd.ExecuteNonQuery()
'report success and clear the image to allow the user to select another image
MessageBox.Show("The Record was saved successfully.")
pic.Image = Nothing
Catch ex As Exception
MessageBox.Show("There was a problem saving the Record." & ControlChars.CrLf & ex.Message)
Finally
'close the connection and free the memory we were using
If con.State = ConnectionState.Open Then con.Close()
con.Dispose() : cmd.Dispose()
con = Nothing : cmd = Nothing : prmPic = Nothing
End Try
End Sub
Last edited by a moderator: