943,519 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 9528
  • VB.NET RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Jul 18th, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

Quote ...
so do I stil put in the Byte2Image, Image2Byte, SaveByteArray, LoadByteArray or do I just leave it as it was now?
If the code works, don't change it (if it ain't broke, don't fix it, you know ) And like I posted before, Adatapost's code is pretty much same as mine.

Quote ...
And I don't know what is contain in the binary data when i retrieve as I havent tried.
Quote ...
Btw my retrieval form is different form with this current form , do I stil do the same for the retrieval side in order to see wat is being save in the binary data? as for your codes is retrieval and sendin is in the same form right?
I used the same form. That's why I had Button3 to clear the picture box control. It doesn't make any difference if you use another form or even another application to read data back.

In your data retrieval form, copy my Byte2Image and LoadByteArray routines. The Button2_Click event handler contains the rest of the code you need to read image back from the database and put it in the picture box control. That's only three lines of code (without dim and comment lines). Of course you need also connection string etc. from the first form. After that you should have everything ready
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jul 22nd, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

now the codes are able to show the picture (binary data) that I have save but the picture display doesnt have the pts that i had circle..

in such case, i tink is the drawing or saving part when wrong isit?
Reputation Points: 10
Solved Threads: 0
Light Poster
DAWNIE is offline Offline
48 posts
since Apr 2009
Jul 22nd, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

Quote ...
the picture display doesnt have the pts that i had circle
Damn

Ok, I can think of two options. User drawings should be somehow merged to image before saving. They seem to be separate objects right now. I don't have code for that at the moment but I'll try to look for that.

The second option would be to save user drawings separately and repaint them after reading image back from the DB. This would allow the undo operation after saving the image but I believe you would prefer the first solution, right?
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jul 22nd, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

ya, i think i prefer the 1st option as it sound easier comparing to the second option and i should be able to understand it better i guess

meanwhile I will try googling if there is codes for merging image and drawing
Reputation Points: 10
Solved Threads: 0
Light Poster
DAWNIE is offline Offline
48 posts
since Apr 2009
Jul 23rd, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

Got it. The very basic principle is not to draw on a (picture box) control. Instead, you have to use some in-memory buffer (i.e. bitmap). You draw to the buffer and then display the buffer in the picture box control. When you save the image to DB, you save the buffer, not picture box's image.

I've used two forms to test it. First form (Form2) is for drawing and it contains just a picture box control and a close button. Second form has a button (Button1) which opens the drawing form and after closing the second form the buffer (bitmap) is saved to DB. ID is taken from a text box control. Second button (Button2) loads the image from the DB and displays it in the second form's picture box control. Here's the drawing form
VB.NET Syntax (Toggle Plain Text)
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Public Class Form2
  5.  
  6. Public BitmapCanvas As Bitmap
  7.  
  8. Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  9. '
  10. ' Saveing graphics(drawline,drawellipse,ect.) to a file
  11. ' http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/0184b1c0-90d0-4660-ba78-a615a8231fe9/
  12. '
  13. ' Drawing in VB .Net Win Forms
  14. ' http://channel9.msdn.com/forums/TechOff/229119-Drawing-in-VB-Net-Win-Forms/
  15.  
  16. BitmapCanvas = New Bitmap("D:\image.jpg")
  17. PictureBox1.Image = BitmapCanvas
  18. PictureBox1.Invalidate() ' Force redraw
  19.  
  20. End Sub
  21.  
  22. ' "Drawing" arrays. ARRAYS ARE OBSOLETE NOW, YOU CAN USE "NORMAL" VARIABLES
  23. Private sStartX() As Integer
  24. Private sStartY() As Integer
  25. Private sEndX() As Integer
  26. Private sEndY() As Integer
  27. ' Remove New and (Color.White), they're just for debugging
  28. Private pPen As New System.Drawing.Pen(Color.White) ' DECLARE THIS AS AN ARRAY IF THE PEN CAN BE CHANGED
  29. ' Counter for the arrays, -1 = nothing drawed yet
  30. Private m_DrawIndex As Integer = -1
  31.  
  32. Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  33.  
  34. 'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down
  35. ' Start a new drawing: increase the counter and redim the arrays
  36. m_DrawIndex += 1
  37. ReDim Preserve sStartX(m_DrawIndex)
  38. ReDim Preserve sStartY(m_DrawIndex)
  39. ReDim Preserve sEndX(m_DrawIndex)
  40. ReDim Preserve sEndY(m_DrawIndex)
  41. 'ReDim Preserve pPen(m_DrawIndex) ' Only if pPen is an array too
  42. '
  43. sStartX(m_DrawIndex) = e.X
  44. sStartY(m_DrawIndex) = e.Y
  45.  
  46. End Sub
  47.  
  48. Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
  49.  
  50. Dim Gr As Graphics
  51.  
  52. 'Initialise Ending Points Of Shape, Once Mouse Button Is Released
  53. sEndX(m_DrawIndex) = e.X
  54. sEndY(m_DrawIndex) = e.Y
  55.  
  56. 'Draw The Circle With The Current Starting, And Ending Values.
  57. 'We must subtract the Starting values from the Ending values,
  58. 'to make sure the shape's Starting and ending values are
  59. 'precisely those where you started drawing, and where you
  60. 'ended drawing.
  61.  
  62. ' Get graphics object from the bitmap to draw on
  63. Gr = Graphics.FromImage(BitmapCanvas)
  64. ' Draw
  65. Gr.DrawEllipse(pPen, sStartX(m_DrawIndex), sStartY(m_DrawIndex), _
  66. sEndX(m_DrawIndex) - sStartX(m_DrawIndex), sEndY(m_DrawIndex) - sStartY(m_DrawIndex))
  67. ' Assign bitmap to picture box control
  68. PictureBox1.Image = BitmapCanvas
  69. PictureBox1.Invalidate() ' Force redraw
  70.  
  71. ' Dispose graphics!
  72. Gr.Dispose()
  73.  
  74. End Sub
  75.  
  76. Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
  77. '
  78. Me.Hide()
  79.  
  80. End Sub
  81.  
  82. End Class
A few points. You don't need to trap paint event anymore. Thus you don't need arrays anymore but I didn't fix that. And see the comments in form's load event. There are few links I used.

Here's the "main" form
VB.NET Syntax (Toggle Plain Text)
  1. Option Strict On
  2. Option Explicit On
  3.  
  4. Imports System.Drawing.Imaging ' Image related methods etc.
  5. Imports System.IO ' MemoryStream
  6. Imports System.Data.SqlClient ' SQl Server related classes
  7.  
  8. Public Class Form1
  9.  
  10. ' Class global variable
  11. Private m_CnStr As String = "Data Source=.\SQLEXPRESS; INITIAL CATALOG=<dbname>; UID=xxx; Password=xxx;" ' Connection string to DB
  12.  
  13. ' Table structure:
  14. ' ImageTable:
  15. '
  16. ' ImageID varchar(50)
  17. ' BlobField image
  18.  
  19. ''' <summary>
  20. ''' Convert a byte array to an Image
  21. ''' </summary>
  22. ''' <param name="NewImage">Image to be returned</param>
  23. ''' <param name="ByteArr">Contains bytes to be converted</param>
  24. ''' <remarks></remarks>
  25. Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
  26. '
  27. Dim ImageStream As MemoryStream
  28.  
  29. Try
  30. If ByteArr.GetUpperBound(0) > 0 Then
  31. ImageStream = New MemoryStream(ByteArr)
  32. NewImage = Image.FromStream(ImageStream)
  33. Else
  34. NewImage = Nothing
  35. End If
  36. Catch ex As Exception
  37. NewImage = Nothing
  38. End Try
  39.  
  40. End Sub
  41.  
  42. ''' <summary>
  43. ''' Convert an image to array of bytes
  44. ''' </summary>
  45. ''' <param name="NewImage">Image to be converted</param>
  46. ''' <param name="ByteArr">Returns bytes</param>
  47. ''' <remarks></remarks>
  48. Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte)
  49. '
  50. Dim ImageStream As MemoryStream
  51.  
  52. Try
  53. ReDim ByteArr(0)
  54. If NewImage IsNot Nothing Then
  55. ImageStream = New MemoryStream
  56. NewImage.Save(ImageStream, ImageFormat.Jpeg)
  57. ReDim ByteArr(CInt(ImageStream.Length - 1))
  58. ImageStream.Position = 0
  59. ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length))
  60. End If
  61. Catch ex As Exception
  62.  
  63. End Try
  64.  
  65. End Sub
  66.  
  67. ''' <summary>
  68. ''' Save a byte array to database
  69. ''' </summary>
  70. ''' <param name="ByteArr">Contains bytes to be saved</param>
  71. ''' <param name="ImageID">ID for the image</param>
  72. ''' <remarks></remarks>
  73. Public Sub SaveByteArray(ByVal ByteArr() As Byte, ByVal ImageID As String)
  74. '
  75. Dim strSQL As String
  76. Dim oConn As SqlConnection
  77. Dim oCmd As SqlCommand
  78. Dim oVarCharParam As SqlParameter
  79. Dim oBLOBParam As SqlParameter
  80.  
  81. Try
  82. ' Create and open connection object
  83. oConn = New SqlConnection(m_CnStr)
  84. oConn.Open()
  85. ' Insert statement
  86. ' Notice that @BLOBValue is a placeholder for the actual data
  87. strSQL = "INSERT INTO ImageTable (ImageID, BlobField) VALUES (@IDValue, @BLOBValue)"
  88. ' Create a command object
  89. oCmd = oConn.CreateCommand()
  90. ' Set SQL statement
  91. oCmd.CommandText = strSQL
  92. ' Create a command parameter
  93. oVarCharParam = New SqlParameter("@IDValue", SqlDbType.VarChar, _
  94. 50, ParameterDirection.Input.ToString)
  95. ' Set the actual data
  96. oVarCharParam.Value = ImageID
  97. ' Add this parameter to the command
  98. oCmd.Parameters.Add(oVarCharParam)
  99. ' Create a command parameter
  100. oBLOBParam = New SqlParameter("@BLOBValue", SqlDbType.Binary, _
  101. ByteArr.Length, ParameterDirection.Input.ToString)
  102. ' Finally, set the actual data
  103. oBLOBParam.Value = ByteArr
  104. ' Add this parameter to the command
  105. oCmd.Parameters.Add(oBLOBParam)
  106. ' Execute SQL statement
  107. oCmd.ExecuteNonQuery()
  108. ' Close the connection
  109. oConn.Close()
  110. Catch ex As Exception
  111.  
  112. End Try
  113.  
  114. End Sub
  115.  
  116. ''' <summary>
  117. ''' Load a byte array from database
  118. ''' </summary>
  119. ''' <param name="ByteArr">Contains bytes from the database</param>
  120. ''' <param name="ImageID">ID for the image</param>
  121. ''' <remarks></remarks>
  122. Public Sub LoadByteArray(ByRef ByteArr() As Byte, ByVal ImageID As String)
  123. '
  124. Dim strSQL As String
  125. Dim oConn As SqlConnection
  126. Dim oCmd As SqlCommand
  127. Dim ValueFromDB As Object ' ExecuteScalar method returns an object
  128.  
  129. Try
  130. ' Create and open connection object
  131. oConn = New SqlConnection(m_CnStr)
  132. oConn.Open()
  133. ' Select statement
  134. strSQL = "SELECT BlobField FROM ImageTable WHERE ImageID='" & ImageID & "'"
  135. ' Create a command object
  136. oCmd = oConn.CreateCommand()
  137. ' Set SQL statement
  138. oCmd.CommandText = strSQL
  139. ' Execute SQL statement
  140. ValueFromDB = oCmd.ExecuteScalar()
  141. ' Close the connection
  142. oConn.Close()
  143. ' Convert returned object to an array of bytes
  144. If ValueFromDB IsNot DBNull.Value Then
  145. ByteArr = CType(ValueFromDB, Byte())
  146. End If
  147. Catch ex As Exception
  148.  
  149. End Try
  150.  
  151. End Sub
  152.  
  153. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  154. '
  155. ' DEBUG: Initial ID
  156. TextBox1.Text = "patient101"
  157.  
  158. End Sub
  159.  
  160. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  161. ' Saves the image from the bitmap buffer to SQL Server
  162. Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
  163. Dim PatientID As String ' ID for the image
  164.  
  165. Form2.ShowDialog() ' Draw the image first
  166.  
  167. ' Get ID from a text box control and remove spaces
  168. PatientID = TextBox1.Text.Trim
  169. ' Convert image to an array of bytes
  170. ' USE FORM2 BITMAP, NOT PICTURE BOX CONTROL
  171. Image2Byte(Form2.BitmapCanvas, ImageByteArr)
  172. ' Save an array of bytes to SQL Server's field of type Image
  173. SaveByteArray(ImageByteArr, PatientID)
  174.  
  175. End Sub
  176.  
  177. Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  178. ' Loads the image to the picture box control from SQL Server
  179. Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
  180. Dim PatientID As String ' ID for the image
  181.  
  182. ' Get ID from a text box control and remove spaces
  183. PatientID = TextBox1.Text.Trim
  184. ' Load an array of bytes from SQL Server's field of type Image
  185. LoadByteArray(ImageByteArr, PatientID)
  186. ' Convert an array of bytes to image
  187. Byte2Image(PictureBox1.Image, ImageByteArr)
  188.  
  189. End Sub
  190.  
  191. End Class
I tried to comment code as best as I could. You should start a new project and test this code. If it works in a way you like, then you can fix your original code.

HTH
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jul 28th, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

sry for the late reply.

I would like to ask, am I suppose to be putting picturebox control in my main form and retrieval form so that wat is being drew can be display on picturebox control?

and which is the part of the codes that link form2's drew image into main form and retrieval form?

I didnt manage to get the drawing over to my main form
Reputation Points: 10
Solved Threads: 0
Light Poster
DAWNIE is offline Offline
48 posts
since Apr 2009
Aug 1st, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

Quote ...
am I suppose to be putting picturebox control in my main form and retrieval form so that wat is being drew can be display on picturebox control?
Just for testing I had a main form (Form1) and a separate drawing form (Form2). You can use just one form. In that case you have to copy the drawing routines from (my) Form2 to your main (drawing) form.

Quote ...
which is the part of the codes that link form2's drew image into main form and retrieval form?
Form1's Button1_Click event opens Form2. Form2 has Public BitmapCanvas As Bitmap . This is a public in-memory buffer (or bitmap) that holds the image and the user drawings, which can be saved to DB. With two forms you have to be careful not to dispose Form2 because then you'll lose that bitmap. If you use only one form, you'll have this in-memory buffer in that form and you don't have a possibility of "loosing" this bitmap.

Quote ...
I didnt manage to get the drawing over to my main form
Did you test the code which I posted "as is"? Or did you modify your previous code? Did you manage to save the image to DB? If the code saves the image then there's still some problem with retrieving the image back from the DB. Anyway, could you please post your current code?
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 3rd, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

I've tested the example you gave me and it works but when i try to implement it together with my codes, i can manage to save it but i cant retrieve in another form..

codes for drawing:

VB.NET Syntax (Toggle Plain Text)
  1.  
  2. Public Class DrawingForm
  3.  
  4. Public BitmapCanvas As Bitmap
  5.  
  6. Private Sub Drawing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  7.  
  8. BitmapCanvas = New Bitmap("D:\Image\Human Anatomy.JPG")
  9. pbBody.Image = BitmapCanvas
  10. pbBody.Invalidate() ' Force redraw
  11.  
  12. End Sub
  13.  
  14. Private sStartX As Integer
  15. Private sStartY As Integer
  16. Private sEndX As Integer
  17. Private sEndY As Integer
  18.  
  19. 'Create And Initialise Pens To Draw The Particular Outline Shapes With. Color : Black, Width : 1
  20. Dim pPen As New Pen(Color.Black, 1)
  21.  
  22. Private Sub pbBody_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown
  23.  
  24. 'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down
  25. sStartX = e.X
  26. sStartY = e.Y
  27.  
  28. End Sub
  29.  
  30. Private Sub pbBody_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseUp
  31.  
  32. Dim Gr As Graphics
  33.  
  34. 'Initialise Ending Points Of Shape, Once Mouse Button Is Released
  35. sEndX = e.X
  36. sEndY = e.Y
  37.  
  38. 'Draw The Circle With The Current Starting, And Ending Values.
  39. 'We must subtract the Starting values from the Ending values,
  40. 'to make sure the shape's Starting and ending values are
  41. 'precisely those where you started drawing, and where you
  42. 'ended drawing.
  43.  
  44. ' Get graphics object from the bitmap to draw on
  45. Gr = Graphics.FromImage(BitmapCanvas)
  46. ' Draw
  47. Gr.DrawEllipse(pPen, sStartX, sStartY, _
  48. sEndX - sStartX, sEndY - sStartY)
  49.  
  50. ' Assign bitmap to picture box control
  51. pbBody.Image = BitmapCanvas
  52. pbBody.Invalidate() ' Force redraw
  53.  
  54. ' Dispose graphics!
  55. Gr.Dispose()
  56.  
  57. End Sub
  58.  
  59. Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
  60.  
  61. Me.Hide()
  62.  
  63. End Sub
  64.  
  65. End Class

codes for MainForm:

VB.NET Syntax (Toggle Plain Text)
  1.  
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Imports System.IO
  5. Imports System.Drawing.Imaging
  6.  
  7. Private Sub Page_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.  
  9. ' DEBUG: Initial ID
  10. txtId.Text = ""
  11.  
  12. End Sub
  13.  
  14. ''' <summary>
  15. ''' Convert an image to array of bytes
  16. ''' </summary>
  17. ''' <param name="NewImage">Image to be converted</param>
  18. ''' <param name="ByteArr">Returns bytes</param>
  19. ''' <remarks></remarks>
  20. Public Sub Image2Byte(ByVal NewImage As Image, ByRef ByteArr() As Byte)
  21. '
  22. Dim ImageStream As MemoryStream
  23.  
  24. Try
  25. ReDim ByteArr(0)
  26. If NewImage IsNot Nothing Then
  27. ImageStream = New MemoryStream
  28. NewImage.Save(ImageStream, ImageFormat.Jpeg)
  29. ReDim ByteArr(CInt(ImageStream.Length - 1))
  30. ImageStream.Position = 0
  31. ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length))
  32. End If
  33. Catch ex As Exception
  34.  
  35. End Try
  36.  
  37. End Sub
  38.  
  39. ''' <summary>
  40. ''' Save a byte array to database
  41. ''' </summary>
  42. ''' <param name="ByteArr">Contains bytes to be saved</param>
  43. ''' <param name="patientIC">ID for the image</param>
  44. ''' <remarks></remarks>
  45. Public Sub SaveByteArray(ByVal ByteArr() As Byte, ByVal patientIC As String)
  46. '
  47. Dim mySQL As String
  48. Dim cmdAdd4 As SqlCommand
  49. Dim oVarCharParam As SqlParameter
  50. Dim oBLOBParam As SqlParameter
  51.  
  52. Try
  53. ' Create and open connection object
  54. Dim connAdd4 As New SqlConnection _
  55. ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
  56. connAdd4.Open()
  57. ' Insert statement
  58. ' Notice that @BLOBValue is a placeholder for the actual data
  59. mySQL = "INSERT INTO image (patientIC, picture) VALUES (@IDValue, @BLOBValue)"
  60. ' Create a command object
  61. cmdAdd4 = connAdd4.CreateCommand()
  62. ' Set SQL statement
  63. cmdAdd4.CommandText = mySQL
  64. ' Create a command parameter
  65. oVarCharParam = New SqlParameter("@IDValue", SqlDbType.VarChar, _
  66. 50, ParameterDirection.Input.ToString)
  67. ' Set the actual data
  68. oVarCharParam.Value = patientIC
  69. ' Add this parameter to the command
  70. cmdAdd4.Parameters.Add(oVarCharParam)
  71. ' Create a command parameter
  72. oBLOBParam = New SqlParameter("@BLOBValue", SqlDbType.Binary, _
  73. ByteArr.Length, ParameterDirection.Input.ToString)
  74. ' Finally, set the actual data
  75. oBLOBParam.Value = ByteArr
  76. ' Add this parameter to the command
  77. cmdAdd4.Parameters.Add(oBLOBParam)
  78. ' Execute SQL statement
  79. cmdAdd4.ExecuteNonQuery()
  80. ' Close the connection
  81. connAdd4.Close()
  82. Catch ex As Exception
  83.  
  84. End Try
  85.  
  86. End Sub
  87.  
  88. Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
  89.  
  90. Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
  91. Dim patientIC As String ' ID for the image
  92.  
  93. Drawing.ShowDialog() ' Draw the image first
  94.  
  95. ' Get ID from a text box control and remove spaces
  96. patientIC = txtId.Text.Trim
  97. ' Convert image to an array of bytes
  98. Image2Byte(pbBody.Image, ImageByteArr)
  99. ' Save an array of bytes to SQL Server's field of type Image
  100. SaveByteArray(ImageByteArr, patientIC)
  101.  
  102. End Sub

codes for RetrievalForm:

VB.NET Syntax (Toggle Plain Text)
  1.  
  2. Imports System.Drawing.Imaging ' Image related methods etc.
  3. Imports System.IO ' MemoryStream
  4. Imports System.Data.SqlClient ' SQl Server related classes
  5. Imports System.Data
  6.  
  7. Public Class Page2
  8.  
  9. ''' <summary>
  10. ''' Convert a byte array to an Image
  11. ''' </summary>
  12. ''' <param name="NewImage">Image to be returned</param>
  13. ''' <param name="ByteArr">Contains bytes to be converted</param>
  14. ''' <remarks></remarks>
  15. Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
  16. '
  17. Dim ImageStream As MemoryStream
  18.  
  19. Try
  20. If ByteArr.GetUpperBound(0) > 0 Then
  21. ImageStream = New MemoryStream(ByteArr)
  22. NewImage = Image.FromStream(ImageStream)
  23. Else
  24. NewImage = Nothing
  25. End If
  26. Catch ex As Exception
  27. NewImage = Nothing
  28. End Try
  29.  
  30. End Sub
  31.  
  32. ''' <summary>
  33. ''' Load a byte array from database
  34. ''' </summary>
  35. ''' <param name="ByteArr">Contains bytes from the database</param>
  36. ''' <param name="patientIC">Patient's IC for Image</param>
  37. ''' <remarks></remarks>
  38. Public Sub LoadByteArray(ByRef ByteArr() As Byte, ByVal patientIC As String)
  39. '
  40. Dim mySQL As String
  41. Dim cmdGet4 As SqlCommand
  42. Dim ValueFromDB As Object ' ExecuteScalar method returns an object
  43.  
  44. Try
  45. ' Create and open connection object
  46. Dim connGet4 As New SqlConnection _
  47. ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
  48. connGet4.Open()
  49.  
  50. ' Select statement
  51. mySQL = "SELECT picture FROM image WHERE patientIC='" & Trim(txtId.Text) & "'"
  52. ' Create a command object
  53. cmdGet4 = connGet4.CreateCommand()
  54. ' Set SQL statement
  55. cmdGet4.CommandText = mySQL
  56. ' Execute SQL statement
  57. ValueFromDB = cmdGet4.ExecuteScalar()
  58. ' Close the connection
  59. connGet4.Close()
  60.  
  61. ' Convert returned object to an array of bytes
  62. If ValueFromDB IsNot DBNull.Value Then
  63. ByteArr = CType(ValueFromDB, Byte())
  64. End If
  65. Catch ex As Exception
  66.  
  67. End Try
  68.  
  69. End Sub
  70.  
  71. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
  72.  
  73. Dim connGet As New SqlConnection _
  74. ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
  75.  
  76. Dim connGet1 As New SqlConnection _
  77. ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
  78.  
  79. Dim connGet2 As New SqlConnection _
  80. ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
  81.  
  82. Dim connGet3 As New SqlConnection _
  83. ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
  84.  
  85. connGet.Open()
  86. Dim cmdGet As New SqlCommand("select * from gcs where patientIC = '" & Trim(txtId.Text) & "'", connGet)
  87. Dim dr As SqlDataReader
  88. dr = cmdGet.ExecuteReader()
  89.  
  90. If dr.Read() Then
  91. lblEyes.Text = dr("eyesOpen")
  92. lblVerbal.Text = dr("verbal")
  93. lblMotor.Text = dr("motor")
  94. lblTotalGCS.Text = dr("totalGcs")
  95. End If
  96.  
  97. dr.Close()
  98. connGet.Close()
  99.  
  100. connGet1.Open()
  101. Dim cmdGet1 As New SqlCommand("select * from vitalSign where patientIC = '" & Trim(txtId.Text) & "'", connGet1)
  102. Dim dr1 As SqlDataReader
  103. dr1 = cmdGet1.ExecuteReader()
  104.  
  105. If dr1.Read() Then
  106. lblPulse.Text = dr1("pulse")
  107. lblRespiration.Text = dr1("respiration")
  108. lblSystolic.Text = dr1("systolic")
  109. lblDiastolic.Text = dr1("diastolic")
  110. lblMean.Text = dr1("mean")
  111. lblSpo2.Text = dr1("spo2")
  112. lblTemperature.Text = dr1("temperature")
  113. End If
  114.  
  115. dr1.Close()
  116. connGet1.Close()
  117.  
  118. connGet2.Open()
  119. Dim cmdGet2 As New SqlCommand("select * from respiratoryAssist where patientIC = '" & Trim(txtId.Text) & "'", connGet2)
  120. Dim dr2 As SqlDataReader
  121. dr2 = cmdGet2.ExecuteReader()
  122.  
  123. If dr2.Read() Then
  124. lblIntubationSize.Text = dr2("iSize")
  125. lblIntubationMarking.Text = dr2("marking")
  126. lblIntubation.Text = dr2("intubation")
  127. lblChestTubeSize.Text = dr2("cSize")
  128. lblChestTube.Text = dr2("chestTube")
  129. End If
  130.  
  131. dr2.Close()
  132. connGet2.Close()
  133.  
  134. connGet3.Open()
  135. Dim cmdGet3 As New SqlCommand("select * from drain where patientIC = '" & Trim(txtId.Text) & "'", connGet3)
  136. Dim dr3 As SqlDataReader
  137. dr3 = cmdGet3.ExecuteReader()
  138.  
  139. If dr3.Read() Then
  140. lblNasogastricTube.Text = dr3("nasogastricTube")
  141. lblUrinaryCatheter.Text = dr3("urinaryCatheter")
  142. lblLtArm.Text = dr3("ivPlugLeft")
  143. lblRtArm.Text = dr3("ivPlugRight")
  144. lblOthers.Text = dr3("others")
  145. End If
  146.  
  147. dr3.Close()
  148. connGet3.Close()
  149.  
  150. ' Loads the image to the picture box control from SQL Server
  151. Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
  152. Dim patientIC As String ' Patient's IC for Image
  153.  
  154. ' Get ID from a text box control and remove spaces
  155. patientIC = txtId.Text.Trim
  156. ' Load an array of bytes from SQL Server's field of type Image
  157. LoadByteArray(ImageByteArr, patientIC)
  158. ' Convert an array of bytes to image
  159. Byte2Image(pbBody.Image, ImageByteArr)
  160.  
  161. End Sub
  162.  
  163. Private Sub Page2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  164.  
  165. ' DEBUG: Initial ID
  166. txtId.Text = ""
  167.  
  168. End Sub
  169. End Class

* for RetrievalForm, i hav include the codes that i need to retrieve (other info besides the drawing part).

i duno wat went wrong whereby ur example works but not on my codes.

however i also try putting the drawing part and main form into 1 form but i didnt manage to get it worked.
Reputation Points: 10
Solved Threads: 0
Light Poster
DAWNIE is offline Offline
48 posts
since Apr 2009
Aug 3rd, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

Quote ...
i duno wat went wrong whereby ur example works but not on my codes.
I tested your code and it worked fine (Page2 form's code). I had to comment out all the data retrieval for labels to test btnSearch_Click but I don't believe that effects anything.

First, you did have a valid value for PatientIC (txtId control)?

Secondly. In
Dim connGet4 As New SqlConnection _
   ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
Initial Catalog=TABLENAME refers to database, not a table in the database. For example, If you have a DB called Patient and a table called Images, your Initial Catalog=Patient.

Here's a modified code for debugging (replace last few lines in btnSearch_Click event handler)
VB.NET Syntax (Toggle Plain Text)
  1. ' Loads the image to the picture box control from SQL Server
  2. Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
  3. Dim patientIC As String ' Patient's IC for Image
  4.  
  5. ' Get ID from a text box control and remove spaces
  6. patientIC = txtId.Text.Trim
  7. ' Load an array of bytes from SQL Server's field of type Image
  8. LoadByteArray(ImageByteArr, patientIC)
  9. If ImageByteArr.Length < 1 Then
  10. MsgBox("Empty byte array! patientIC='" & patientIC & "' might not exist?")
  11. End If
  12. ' Convert an array of bytes to image
  13. Dim TempImage As Image = Nothing
  14. Byte2Image(TempImage, ImageByteArr)
  15. If TempImage IsNot Nothing Then
  16. pbBody.Image = TempImage
  17. Else
  18. MsgBox("Image is nothing!")
  19. End If
If your app can't find the data in the first place (patientIC does not exist) you should get: "Empty byte array! patientIC=..." message. In that case, check the ID you're using. You could use SQL Server Management Studio or similar to check directly from the DB those patientIC values. I assume patientIC is of type VarChar. If you've declared it Char or NChar, the string inserted to that field gets padded with spaces! One more warning of text fields. If the user enters an apostrophe ('-character) to the ID field, your code might crash quite easily.

If the conversion from bytes to image fails, you'll get "Image is nothing!" message but I doubt that there's any bug.

Quote ...
i also try putting the drawing part and main form into 1 form but i didnt manage to get it worked
I suggest debugging the code as it is right now. After everything works fine, merge the codes from those two forms to a one form (remember to take a backup copy from your working code first!). The app should work just fine in a single form.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 3rd, 2009
0

Re: Saving Drawing on PictureBox to SQL Database

I have check the DB and there is this patientIC in my DB and also binary data but when i try to retrieve, it say "Image is nothing" whereby other info that stored in that database can be retrieve except for the image only
Reputation Points: 10
Solved Threads: 0
Light Poster
DAWNIE is offline Offline
48 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Login System
Next Thread in VB.NET Forum Timeline: canot find a column





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC