' Loads the image to the picture box control from SQL Server Dim ImageByteArr(0) As Byte ' An array to hold image (bytes) Dim patientIC As String ' Patient's IC for Image ' Get ID from a text box control and remove spaces patientIC = txtId.Text.Trim ' Load an array of bytes from SQL Server's field of type Image LoadByteArray(ImageByteArr, patientIC) If ImageByteArr.Length <= 1 Then MsgBox("Empty byte array! patientIC='" & patientIC & "' might not exist?") End If ' Convert an array of bytes to image Dim TempImage As Image = Nothing Byte2Image(TempImage, ImageByteArr) If TempImage IsNot Nothing Then pbBody.Image = TempImage Else MsgBox("Image is nothing!") End If
SELECT [ImageID], [BlobField] FROM [ImageTable] WHERE [ImageID]='patient101'
ImageID BlobField
patient101 0xFFD8FFE000104A46494600010101006...
SELECT DATALENGTH([BlobField]) AS DataLen FROM [ImageTable] WHERE [ImageID]='patient101'
DataLen
2319
Image2Byte(Form2.BitmapCanvas, ImageByteArr) in your code. Write after that line MsgBox(ImageByteArr.Length) and compare those two lengths, they should be the same.
There's some small bug left in the code, but it's hard to catch. If ImageByteArr.Length < 1 Then
to
If ImageByteArr.Length <= 1 Then
Image2Byte(Form2.BitmapCanvas, ImageByteArr)
Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte)
patientIC image
S7654321W 0x00
DataLen
1
and i had try the excute SQL statement in SQL Studio and the result I get is:
patientIC image
S7654321W 0x00
and for datalen
DataLen
1
doesnt that means my image is an invalid 1?
'Memory stream Dim ms As New System.IO.MemoryStream 'Write an image data into memory stream DrawingForm.BitmapCanvas.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) 'Byte array Dim b() As Byte = ms.ToArray Dim cn As New System.Data.SqlClient.SqlConnection(m_CnStr) ' <-- your connection string goes here 'Parameterized Query Dim cmd As New SqlClient.SqlCommand("insert into image (patientIC,picture) values (@p1,@p2)", cn) ' <-- your table/field names go here 'Create/add parameters Dim oVarCharParam As SqlParameter = New SqlParameter("p1", SqlDbType.VarChar, 50, ParameterDirection.Input.ToString) oVarCharParam.Value = "image01" ' lblId.Text, I use a hard coded ID here for debugging cmd.Parameters.Add(oVarCharParam) 'Create/add parameters Dim oBLOBParam As SqlParameter = New SqlParameter("@p2", SqlDbType.Binary, b.Length, ParameterDirection.Input.ToString) oBLOBParam.Value = b cmd.Parameters.Add(oBLOBParam) 'Execute command cn.Open() cmd.ExecuteNonQuery() cn.Close() ' <-- close the connection unless you need it
System.Drawing.Imaging.ImageFormat enumeration value you use with images. It shouldn't make any difference what image format you use, but you have to use the same format (like Jpeg) for both saving and retrieving the image from the DB. If you used the original posting with Png format and later tried to get image back with my code (Jpeg format), that didn't cause an empty image to be saved.MsgBox(b.Length) to the code above ("between" lines 7 and 8) to check the original image size. You should get the same value with SQL statement (from my previous posting) containing DataLen function after you've saved a new image.
Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte) ' Dim ImageStream As MemoryStream Try ReDim ByteArr(0) If NewImage IsNot Nothing Then ImageStream = New MemoryStream NewImage.Save(ImageStream, ImageFormat.Jpeg) ReDim ByteArr(CInt(ImageStream.Length - 1)) ImageStream.Position = 0 ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length)) End If Catch ex As Exception End Try End Sub Public Sub SaveByteArray(ByVal ByteArr() As Byte, ByVal patientIC As String) ' Dim mySQL As String Dim cmdAdd4 As SqlCommand Dim oVarCharParam As SqlParameter Dim oBLOBParam As SqlParameter Try ' Create and open connection object Dim connAdd4 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=DBNAME;" + "User ID=UID;" + "Password=PW;") connAdd4.Open() ' Insert statement ' Notice that @BLOBValue is a placeholder for the actual data mySQL = "INSERT INTO image (patientIC, picture) VALUES (@IDValue, @BLOBValue)" ' Create a command object cmdAdd4 = connAdd4.CreateCommand() ' Set SQL statement cmdAdd4.CommandText = mySQL ' Create a command parameter oVarCharParam = New SqlParameter("@IDValue", SqlDbType.VarChar, _ 50, ParameterDirection.Input.ToString) ' Set the actual data oVarCharParam.Value = patientIC ' Add this parameter to the command cmdAdd4.Parameters.Add(oVarCharParam) ' Create a command parameter oBLOBParam = New SqlParameter("@BLOBValue", SqlDbType.Binary, _ ByteArr.Length, ParameterDirection.Input.ToString) ' Finally, set the actual data oBLOBParam.Value = ByteArr ' Add this parameter to the command cmdAdd4.Parameters.Add(oBLOBParam) ' Execute SQL statement cmdAdd4.ExecuteNonQuery() ' Close the connection connAdd4.Close() Catch ex As Exception End Try End Sub
m i wrong?
Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte) ' Dim ImageStream As MemoryStream Try ReDim ByteArr(0) If NewImage IsNot Nothing Then ImageStream = New MemoryStream NewImage.Save(ImageStream, ImageFormat.Jpeg) ReDim ByteArr(CInt(ImageStream.Length - 1)) ImageStream.Position = 0 ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length)) MsgBox(ByteArr.Length) ' DEBUG End If Catch ex As Exception MsgBox(ex.Message) ' DEBUG End Try End Sub Public Sub SaveByteArray(ByVal ByteArr() As Byte, ByVal patientIC As String) ' Dim mySQL As String Dim cmdAdd4 As SqlCommand Dim oVarCharParam As SqlParameter Dim oBLOBParam As SqlParameter Try MsgBox(ByteArr.Length) ' DEBUG ' Create and open connection object Dim connAdd4 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=DBNAME;" + "User ID=UID;" + "Password=PW;") connAdd4.Open() ' Insert statement ' Notice that @BLOBValue is a placeholder for the actual data mySQL = "INSERT INTO image (patientIC, picture) VALUES (@IDValue, @BLOBValue)" ' Create a command object cmdAdd4 = connAdd4.CreateCommand() ' Set SQL statement cmdAdd4.CommandText = mySQL ' Create a command parameter oVarCharParam = New SqlParameter("@IDValue", SqlDbType.VarChar, _ 50, ParameterDirection.Input.ToString) ' Set the actual data oVarCharParam.Value = patientIC ' Add this parameter to the command cmdAdd4.Parameters.Add(oVarCharParam) ' Create a command parameter oBLOBParam = New SqlParameter("@BLOBValue", SqlDbType.Binary, _ ByteArr.Length, ParameterDirection.Input.ToString) ' Finally, set the actual data oBLOBParam.Value = ByteArr ' Add this parameter to the command cmdAdd4.Parameters.Add(oBLOBParam) ' Execute SQL statement cmdAdd4.ExecuteNonQuery() ' Close the connection connAdd4.Close() Catch ex As Exception MsgBox(ex.Message) ' DEBUG End Try End Sub
Image2Byte(Form2.BitmapCanvas, ImageByteArr)
I didnt manage to find the above code in my codes... i only found
Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte)
Image2Byte(DrawingForm.BitmapCanvas, ImageByteArr) SaveByteArray(ImageByteArr, patientIC)
Catch ex As Exception End Try
Catch ex As Exception MsgBox(ex.Message) ' DEBUG End Try
You should have a call like
Image2Byte(DrawingForm.BitmapCanvas, ImageByteArr)
followed by
SaveByteArray(ImageByteArr, patientIC)
the above "ImageByteArr" is in another form so how can i input the codes to saving form whereby the ImageByteArr will not have a curly line under it?
Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
Image2Byte(DrawingForm.BitmapCanvas, ImageByteArr) ' and after that SaveByteArray(ImageByteArr, patientIC)
MsgBox(ByteArr.Length) ' DEBUG to MsgBox("Image2Byte " & ByteArr.Length) ' DEBUG in Image2Byte procedure and MsgBox(ByteArr.Length) ' DEBUG to MsgBox("SaveByteArray " & ByteArr.Length) ' DEBUG in SaveByteArray procedure, so that it would be clear at which point byte array remains or gets empty.
If NewImage IsNot Nothing Then ImageStream = New MemoryStream NewImage.Save(ImageStream, ImageFormat.Jpeg) ReDim ByteArr(CInt(ImageStream.Length - 1)) ImageStream.Position = 0 ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length)) MsgBox(ByteArr.Length) ' DEBUG Else ' <-- ADD THIS ELSE PART TO TRAP MISSING IMAGE PARAM MsgBox("Image is nothing") ' DEBUG End If
In the popup box, there is number "1" or "ok" button. does that means the datalen?
when i retrieve, it still prompt mi ""Empty byte array..." message and "image is nothing" kind of prompt. Other data appear except images.
''' <summary> ''' Convert an image to array of bytes ''' </summary> ''' <param name="NewImage">Image to be converted</param> ''' <param name="ByteArr">Returns bytes</param> ''' <remarks></remarks> ''' Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte) Dim ImageByteArr(0) As Byte ' An array to hold image (bytes) Dim patientIC As String ' ID for the image Image2Byte(Drawing.BitmapCanvas, ImageByteArr) SaveByteArray(ImageByteArr, patientIC) Dim ImageStream As MemoryStream Try If NewImage IsNot Nothing Then ImageStream = New MemoryStream NewImage.Save(ImageStream, ImageFormat.Jpeg) ReDim ByteArr(CInt(ImageStream.Length - 1)) ImageStream.Position = 0 ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length)) MsgBox("Image2Byte " & ByteArr.Length) ' DEBUG Else MsgBox("Image is nothing") 'DEBUG End If Catch ex As Exception MsgBox(ex.Message) ' DEBUG End Try End Sub ''' <summary> ''' Save a byte array to database ''' </summary> ''' <param name="ByteArr">Contains bytes to be saved</param> ''' <param name="patientIC">ID for the image</param> ''' <remarks></remarks> Public Sub SaveByteArray(ByVal ByteArr() As Byte, ByVal patientIC As String) ' Dim mySQL As String Dim cmdAdd4 As SqlCommand Dim oVarCharParam As SqlParameter Dim oBLOBParam As SqlParameter Try MsgBox("SaveByteArray " & ByteArr.Length) ' DEBUG ' Create and open connection object Dim connAdd4 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=DBNAME;" + "User ID=UID;" + "Password=PW;") connAdd4.Open() ' Insert statement ' Notice that @BLOBValue is a placeholder for the actual data mySQL = "INSERT INTO image (patientIC, picture) VALUES (@IDValue, @BLOBValue)" ' Create a command object cmdAdd4 = connAdd4.CreateCommand() ' Set SQL statement cmdAdd4.CommandText = mySQL ' Create a command parameter oVarCharParam = New SqlParameter("@IDValue", SqlDbType.VarChar, _ 50, ParameterDirection.Input.ToString) ' Set the actual data oVarCharParam.Value = patientIC ' Add this parameter to the command cmdAdd4.Parameters.Add(oVarCharParam) ' Create a command parameter oBLOBParam = New SqlParameter("@BLOBValue", SqlDbType.Binary, _ ByteArr.Length, ParameterDirection.Input.ToString) ' Finally, set the actual data oBLOBParam.Value = ByteArr ' Add this parameter to the command cmdAdd4.Parameters.Add(oBLOBParam) ' Execute SQL statement cmdAdd4.ExecuteNonQuery() ' Close the connection connAdd4.Close() Catch ex As Exception MsgBox(ex.Message) ' DEBUG End Try End Sub Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click Dim ImageByteArr(0) As Byte ' An array to hold image (bytes) Dim patientIC As String ' ID for the image Drawing.ShowDialog() ' Draw the image first ' Get ID from a text box control and remove spaces patientIC = txtId.Text.Trim ' Convert image to an array of bytes Image2Byte(pbBody.Image, ImageByteArr) ' Save an array of bytes to SQL Server's field of type Image SaveByteArray(ImageByteArr, patientIC) End Sub
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click ' Saves the image from the bitmap buffer to SQL Server Dim ImageByteArr(0) As Byte ' An array to hold image (bytes) Dim patientIC As String ' ID for the image Drawing.ShowDialog() ' Draw the image first ' Get ID from a text box control and remove spaces patientIC = lblId.Text.Trim ' Convert image to an array of bytes ' USE FORM2 BITMAP, NOT PICTURE BOX CONTROL [COLOR="Red"] Image2Byte(Drawing.BitmapCanvas, ImageByteArr)[/COLOR] ' Save an array of bytes to SQL Server's field of type Image SaveByteArray(ImageByteArr, patientIC) End Sub
| DaniWeb Message | |
| Cancel Changes | |