| | |
Saving Drawing on PictureBox to SQL Database
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
I made a mistake in previous post. The code should have been
Code line 9 changes a bit. Do you get now "Empty byte array..." message?
Two possibilities left. The ID patientIC is still not found. Or the saving of the image fails in the first place.
If you view the DB data in SQL Server Management Studio, it shows <Binary data> in the Image type field, but it may not be valid data. You could try to execute following SQL statement in Management Studio (just replace correct field and table names and ID value). You should now see if there's actually data in Image field:
Image data is dumped in hex format but you'll see that there's actually some data in the field.
Another (and possible a better) way is to execute query
I got the result
for an image. This is a value you can compare to the length of data you're originally saving. You had (my variable names, sorry)
I hope this didn't get too complicated
There's some small bug left in the code, but it's hard to catch.
VB.NET Syntax (Toggle Plain Text)
' 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
Two possibilities left. The ID patientIC is still not found. Or the saving of the image fails in the first place.
If you view the DB data in SQL Server Management Studio, it shows <Binary data> in the Image type field, but it may not be valid data. You could try to execute following SQL statement in Management Studio
VB.NET Syntax (Toggle Plain Text)
SELECT [ImageID], [BlobField] FROM [ImageTable] WHERE [ImageID]='patient101'
•
•
•
•
ImageID BlobField
patient101 0xFFD8FFE000104A46494600010101006...
Another (and possible a better) way is to execute query
VB.NET Syntax (Toggle Plain Text)
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.I hope this didn't get too complicated
There's some small bug left in the code, but it's hard to catch. Teme64 @ Windows Developer Blog
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
•
•
•
•
If ImageByteArr.Length < 1 Then
to
If ImageByteArr.Length <= 1 Then
but if the id is not exist, then how could i retrieve the other data and not the image whereby they are in the same DB jus different table only?
•
•
•
•
Image2Byte(Form2.BitmapCanvas, ImageByteArr)
VB.NET Syntax (Toggle Plain Text)
Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte)
and i had try the excute SQL statement in SQL Studio and the result I get is:
•
•
•
•
patientIC image
S7654321W 0x00
•
•
•
•
DataLen
1
yaya, the bug is real hard to solve but hopefully is not a bug that due to my careless if not i will gonna go bang the wall =)
Last edited by DAWNIE; Aug 4th, 2009 at 6:20 am.
•
•
•
•
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?
I slightly modified the "save image to DB" code
VB.NET Syntax (Toggle Plain Text)
'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
You should also check all your code, what
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.You could also put
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. Teme64 @ Windows Developer Blog
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
i didnt put that codes cos i thought this codes already saving the image to DB
m i wrong?
VB.NET Syntax (Toggle Plain Text)
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?
•
•
•
•
m i wrong?
Here's the same code with four messagebox "dumps" added
VB.NET Syntax (Toggle Plain Text)
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) followed by
SaveByteArray(ImageByteArr, patientIC) If you don't have (notice that the actual parameter names may differ a bit), check that you do call them after you return from your DrawingForm back to main form and you haven't disposed DrawingForm yet.
The subroutines themselves are ok and your drawing form's code should be ok. You either didn't save the image buffer (i.e. DrawingForm.BitmapCanvas) in the first place or something goes wrong when you try to save a byte array to image field
My original sample code had "empty" exception handlers
VB.NET Syntax (Toggle Plain Text)
Catch ex As Exception End Try
VB.NET Syntax (Toggle Plain Text)
Catch ex As Exception MsgBox(ex.Message) ' DEBUG End Try
Teme64 @ Windows Developer Blog
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
after putting the four msgbox, i have a popup box. 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.
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?
when i retrieve, it still prompt mi ""Empty byte array..." message and "image is nothing" kind of prompt. Other data appear except images.
•
•
•
•
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?
VB.NET Syntax (Toggle Plain Text)
Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
VB=NET Syntax (Toggle Plain Text)
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.Also, change in Image2Byte procedure
VB.NET Syntax (Toggle Plain Text)
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.
Teme64 @ Windows Developer Blog
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
i've attached my printscreen for my error...
the codes that i hav edit according to ur instruction is so far like that:
but y cant i dim patientIC As String on the Image2Byte procedure? whereby i'm able to dim it as String in OPEN to drawingform prodecure?
the error i received for dimming patientIC as String is "Variable 'patientIC' is used before it has been assigned a value. A null reference exception could result at runtime"
should i dim it as integer instead?
the codes that i hav edit according to ur instruction is so far like that:
VB.NET Syntax (Toggle Plain Text)
''' <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
but y cant i dim patientIC As String on the Image2Byte procedure? whereby i'm able to dim it as String in OPEN to drawingform prodecure?
the error i received for dimming patientIC as String is "Variable 'patientIC' is used before it has been assigned a value. A null reference exception could result at runtime"
should i dim it as integer instead?
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
Teme64, I have solve the problem regarding my codes... it becos of 1 typo error...
the sentence that is red in colour is what it shld be... but that time i went to type [ Image2Byte(pbBody.Image, ImageByteArr)]..
sry for my typo error that time =)
VB.NET Syntax (Toggle Plain Text)
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
the sentence that is red in colour is what it shld be... but that time i went to type [ Image2Byte(pbBody.Image, ImageByteArr)]..
sry for my typo error that time =)
![]() |
Similar Threads
- txt to sql database (C#)
- Updating SQL database, Please help (C#)
- SQL Database Problem (C#)
- PLEASE HELP!....SQL Database problem.... (C#)
- Update SQL database automatically using VB6 (Visual Basic 4 / 5 / 6)
- SQL Database loop (C#)
- Insert into sql database (ASP.NET)
- Process very slow - SQL Database (MS SQL)
- Help with Roles Stored in SQL database (ASP.NET)
- Snyc'n Local SQL database online (MS SQL)
Other Threads in the VB.NET Forum
- Previous Thread: Help: Using Parameter Stored Procedure in Crystal Report
- Next Thread: canot find a column
| Thread Tools | Search this Thread |
.net .net2008 30minutes 2005 2008 access account arithmetic array basic bing button buttons center check code combobox component connectionstring crystalreport data database databasesearch datagrid datagridview date design dissertation dissertations dropdownlist excel fade file-dialog filter folder ftp generatetags google gridview hardcopy images input insert intel internet mobile monitor ms net networking objects output panel passingparameters peertopeervideostreaming picturebox picturebox1 port position print printing problem problemwithinstallation project save searchbox searchvb.net select serial shutdown soap survey table tcp temperature text textbox timer timespan toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year





