Saving Drawing on PictureBox to SQL Database

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Saving Drawing on PictureBox to SQL Database

 
1
  #11
Jul 8th, 2009
If you use adatapost's code:
I have my default picture so do I edit my code this way? but there is this prompt "Too many arguments to 'Public Function Save() As System.Drawing.Drawing2D.GraphicsState". so how do i solve this issue?
use the Image property of the picture box
  1. 'Write an image data into memory stream
  2. pbBody.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

How do I get the p2? isit the picturebox's name?
No, it's the image from the picture box converted to an array of bytes. You can't use the following SQL syntax with Image data type
  1. Dim cmd As New SqlClient.SqlCommand("insert into image (myid,myimage) values ('" & Trim(lblId.text) & "','" & Trim(?) & "')", cn)
Instead check, how adatapost used the parameters in the query (lines 8 - 32 from his post)
  1. 'Memory stream
  2. Dim ms As New System.IO.MemoryStream
  3.  
  4. 'Write an image data into memory stream
  5. pbBody.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
  6.  
  7. 'To save an image into disk file
  8. 'pbBody.Image.Save("p1.png", System.Drawing.Imaging.ImageFormat.Png)
  9.  
  10. 'Byte array
  11. Dim b() As Byte = ms.ToArray
  12. 'Parameterized Query
  13. Dim cmd As New SqlClient.SqlCommand("insert into mytable (myid,myimage) values (@p1,@p2)", cn)
  14.  
  15. 'Create/add parameters
  16. '@p1 - name of parameter, datatype,size,"columnname"
  17. cmd.Parameters.Add("@p1", SqlDbType.Int, 4, "myid")
  18. cmd.Parameters.Add("@p2", SqlDbType.Image, b.Length, "myimage")
  19.  
  20. 'Assign value to the parameters
  21. cmd.Parameters("@p1").Value = CInt(lblId.text) ' ASSUMING YOU'RE USING INT TYPE AS ID
  22. cmd.Parameters("@p2").Value = b ' THIS IS BYTE ARRAY CONTAINING THE IMAGE. SEE FEW LINES ABOVE
If you looked my code, you'd seen that I used a parameter in the query too. And AFAIK that's the only way to do it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 48
Reputation: DAWNIE is an unknown quantity at this point 
Solved Threads: 0
DAWNIE DAWNIE is offline Offline
Light Poster

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #12
Jul 13th, 2009
I have this prompt when I try to do my saving after drawing circle. [COLOR] Format of the initialization string does not conform to specification starting at index 0. [/COLOR]

  1. Dim cn As New System.Data.SqlClient.SqlConnection("CnStr")

Above is the line that is being highlighted for error.

Did i did the code correctly this time? how do i declare varchar's object expression?

 Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

'Memory stream        
        Dim ms As New System.IO.MemoryStream
        'Write an image data into memory stream        
        pbBody.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

        'Byte array        
        Dim b() As Byte = ms.ToArray
        Dim cn As New System.Data.SqlClient.SqlConnection("CnStr")
        'Parameterized Query        
        Dim cmd As New SqlClient.SqlCommand("insert into image (patientIC,picture) values (@p1,@p2)", cn)
        'Create/add parameters        
        '@p1 - name of parameter, datatype,size,"columnname"       
        cmd.Parameters.Add("@p1", SqlDbType.VarChar, 50, "patientIC")
        cmd.Parameters.Add("@p2", SqlDbType.Image, b.Length, "picture")
        'Assign value to the parameters        
        cmd.Parameters("@p1").Value = __?__ (lblId.Text)
        cmd.Parameters("@p2").Value = b
        'Execute command        
        cn.Open()
        cmd.ExecuteNonQuery()

    End Sub

Sorry if i'm stil as blur.

and my code for drawing multi circle..

  1. Private Sub pbBody_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown
  2.  
  3. 'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down
  4. sStartX = e.X
  5. sStartY = e.Y
  6.  
  7. End Sub
  8.  
  9. Private Sub pbBody_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseUp
  10.  
  11. 'Initialise Ending Points Of Shape, Once Mouse Button Is Released
  12. sEndX = e.X
  13. sEndY = e.Y
  14.  
  15. 'Draw The Circle With The Current Starting, And Ending Values.
  16. 'We must subtract the Starting values from the Ending values,
  17. 'to make sure the shape's Starting and ending values are
  18. 'precisely those where you started drawing, and where you
  19. 'ended drawing.
  20. pbBody.CreateGraphics.DrawEllipse(pPen, sStartX, sStartY, _
  21. sEndX - sStartX, sEndY - sStartY)
  22.  
  23. End Sub

Y does my circle's line alway gone missing when i scroll up n down my image?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #13
Jul 13th, 2009
Dim cn As New System.Data.SqlClient.SqlConnection("CnStr")
Maybe you shouldn't have quotes around CnStr (assuming it's a variable). Your code should be similar to
  1. Dim CnStr As String
  2. CnStr = "Data Source=MyMachineName\SQLEXPRESS; INITIAL CATALOG=MyDatabase; User ID=XXX; Password=XXX;"
  3. ' or CnStr = "Data Source=MyMachineName; INITIAL CATALOG=MyDatabase; User ID=XXX; Password=XXX;"
  4. Dim cn As New System.Data.SqlClient.SqlConnection(CnStr)
See Connection strings for SQL Server 2005 for the correct format of the connection string.

cmd.Parameters("@p1").Value = __?__ (lblId.Text)
should be
  1. cmd.Parameters("@p1").Value = lblId.Text
assuming that label lblId contains a valid PatientID.

Y does my circle's line alway gone missing when i scroll up n down my image?
Because picture boxes Paint event redraws the image (without your drawings).

I found two solution with googling. I didn't either test or try them so you'll have to check them yourself. First solution may be easier to modify for your needs. The latter one is for Pocket PC but should be portable to WinForms application:
I'm having a problem with drawing on picturebox in vb.net programing
and PictureBox Drawing VB.NET, here's the correct link to sample code: Scribbler Technology Sample
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 48
Reputation: DAWNIE is an unknown quantity at this point 
Solved Threads: 0
DAWNIE DAWNIE is offline Offline
Light Poster

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #14
Jul 13th, 2009
i hav done the saving part but it shown in table patient's ID and binary data. m i on the right track? the image is save as binary data?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,638
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #15
Jul 14th, 2009
DAWNIE,
You are right. This technique insert content of any type (image, doc, sheet, program, html etc) of file into a table.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 48
Reputation: DAWNIE is an unknown quantity at this point 
Solved Threads: 0
DAWNIE DAWNIE is offline Offline
Light Poster

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #16
Jul 14th, 2009
okok. thanks adatapost & Teme64. I can manage to save the file but hav yet to try retrieving it back from database. =)

Thanks lots
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 48
Reputation: DAWNIE is an unknown quantity at this point 
Solved Threads: 0
DAWNIE DAWNIE is offline Offline
Light Poster

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #17
Jul 15th, 2009
I still having problem for the re-draw part. i have added the codes below to my application:

  1. Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint
  2.  
  3. 'Draw the Final rectangle, as a repaint was issued
  4. e.Graphics.DrawEllipse(pPen, sStartX, sStartY, _
  5. sEndX - sStartX, sEndY - sStartY)
  6.  
  7. End Sub Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint
  8.  
  9. 'Draw the Final rectangle, as a repaint was issued
  10. e.Graphics.DrawEllipse(pPen, sStartX, sStartY, _
  11. sEndX - sStartX, sEndY - sStartY)
  12.  
  13. End Sub

The above codes only save what is being drew the latest. which mean only the new circle that is being draw wont disappear when i scroll up & down whereby the remainin circle will disappear.

Teme64 > I had try the 2 website that u gave me but i don't really get the way on how they do it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Saving Drawing on PictureBox to SQL Database

 
1
  #18
Jul 15th, 2009
Here's the repainting code. All the user drawings are saved on the arrays. On the picture box's paint event all the saved user drawings are repainted from the arrays
  1. ' "Drawing" arrays
  2. Private sStartX() As Integer
  3. Private sStartY() As Integer
  4. Private sEndX() As Integer
  5. Private sEndY() As Integer
  6. ' Remove New and (Color.White), they're just for debugging
  7. Private pPen As New System.Drawing.Pen(Color.White) ' DECLARE THIS AS AN ARRAY IF THE PEN CAN BE CHANGED
  8. ' Counter for the arrays, -1 = nothing drawed yet
  9. Private m_DrawIndex As Integer = -1
  10.  
  11. Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  12.  
  13. 'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down
  14. ' Start a new drawing: increase the counter and redim the arrays
  15. m_DrawIndex += 1
  16. ReDim Preserve sStartX(m_DrawIndex)
  17. ReDim Preserve sStartY(m_DrawIndex)
  18. ReDim Preserve sEndX(m_DrawIndex)
  19. ReDim Preserve sEndY(m_DrawIndex)
  20. 'ReDim Preserve pPen(m_DrawIndex) ' Only if pPen is an array too
  21. '
  22. sStartX(m_DrawIndex) = e.X
  23. sStartY(m_DrawIndex) = e.Y
  24.  
  25. End Sub
  26.  
  27. Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
  28.  
  29. 'Initialise Ending Points Of Shape, Once Mouse Button Is Released
  30. sEndX(m_DrawIndex) = e.X
  31. sEndY(m_DrawIndex) = e.Y
  32.  
  33. 'Draw The Circle With The Current Starting, And Ending Values.
  34. 'We must subtract the Starting values from the Ending values,
  35. 'to make sure the shape's Starting and ending values are
  36. 'precisely those where you started drawing, and where you
  37. 'ended drawing.
  38. PictureBox1.CreateGraphics.DrawEllipse(pPen, sStartX(m_DrawIndex), sStartY(m_DrawIndex), _
  39. sEndX(m_DrawIndex) - sStartX(m_DrawIndex), sEndY(m_DrawIndex) - sStartY(m_DrawIndex))
  40.  
  41. End Sub
  42.  
  43. Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
  44.  
  45. 'Draw the Final rectangle, as a repaint was issued
  46.  
  47. ' Redraw user drawings from the array
  48. Dim i As Integer
  49.  
  50. For i = 0 To m_DrawIndex
  51. e.Graphics.DrawEllipse(pPen, sStartX(i), sStartY(i), _
  52. sEndX(i) - sStartX(i), sEndY(i) - sStartY(i))
  53. Next i
  54.  
  55. End Sub
With this code you could also implement very easily "undo" option simply by decreasing array pointer (i.e. m_DrawIndex variable) and redimming arrays with the new array pointer value.

HTH

P.s.
I had try the 2 website that u gave me but i don't really get the way on how they do it.
A short and quick answer: The first one is the code which converts image (i.e. Picturebox.Image property) to an array of bytes. This array of bytes can be saved to SQL Server's field of type Image. The latter code does that. There's also subroutines to retrieve the image back from the SQL Server to picture box control. Except that the latter link doesn't provide code for that, I just noticed I'll try to write and post that code ASAP (I'm on holiday). Saving the Image type requires parametrized insert statement, maybe that's a bit confusing? And finally, adatapost's code does the same thing i.e. shows how to use parametrized queries.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #19
Jul 16th, 2009
Here's a complete (example) code for saving a picture box control's image to SQL Server and loading it back
  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 ' 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(ByRef 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. ' DEBUG: Load an (initial) image to picture box control
  155. PictureBox1.Image = Image.FromFile("D:\image.jpg")
  156.  
  157. ' Initialize DB connection, REPLACE VALUES!
  158. ' m_CnStr = "Data Source=database name; INITIAL CATALOG=table name; User ID=user name; Password=user password;"
  159.  
  160. m_CnStr = "Data Source=xxx\SQLEXPRESS; INITIAL CATALOG=xxx; User ID=xxx; Password=xxx;"
  161.  
  162. ' DEBUG: Test image ID
  163. TextBox1.Text = "patient_image_001"
  164.  
  165. End Sub
  166.  
  167. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  168. ' Saves the image from the picture box control to SQL Server
  169. Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
  170. Dim PatientID As String ' ID for the image
  171.  
  172. ' Get ID from a text box control and remove spaces
  173. PatientID = TextBox1.Text.Trim
  174. ' Convert image to an array of bytes
  175. Image2Byte(PictureBox1.Image, ImageByteArr)
  176. ' Save an array of bytes to SQL Server's field of type Image
  177. SaveByteArray(ImageByteArr, PatientID)
  178.  
  179. End Sub
  180.  
  181. Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  182. ' Loads the image to the picture box control from SQL Server
  183. Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
  184. Dim PatientID As String ' ID for the image
  185.  
  186. ' Get ID from a text box control and remove spaces
  187. PatientID = TextBox1.Text.Trim
  188. ' Load an array of bytes from SQL Server's field of type Image
  189. LoadByteArray(ImageByteArr, PatientID)
  190. ' Convert an array of bytes to image
  191. Byte2Image(PictureBox1.Image, ImageByteArr)
  192.  
  193. End Sub
  194.  
  195. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  196. ' DEBUG: Clear the image
  197. PictureBox1.Image = Nothing
  198.  
  199. End Sub
  200.  
  201. End Class
Create a new project (or just a form). Drop three button controls, one text box control and one picture box control to the form. Button1 saves the image, Button2 loads the image and Button3 clears the image (just for testing). I tried to comment the code so it should be understandable. The code is missing error handling and some "sanity checks" for the input to keep it short. And of course, you have to create a table for the data and replace connection string, table name and field names in the code.

HTH
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 48
Reputation: DAWNIE is an unknown quantity at this point 
Solved Threads: 0
DAWNIE DAWNIE is offline Offline
Light Poster

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #20
Jul 16th, 2009
thanks alot for the effort for the example coding. before your codes above, i was using:

  1.  
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Imports System.IO
  5. Imports System.Drawing.Imaging
  6.  
  7. Public Class Page_2
  8.  
  9. 'Declare Starting Points For Drawn Objects
  10. Private sStartX() As Integer
  11. Private sStartY() As Integer
  12.  
  13. 'Declare Ending points For Drawn Objects
  14. Private sEndX() As Integer
  15. Private sEndY() As Integer
  16.  
  17. 'Create And Initialise Pens To Draw The Particular Outline Shapes With. Color : Black, Width : 1
  18. Dim pPen As New Pen(Color.Black, 1)
  19.  
  20. ' Counter for the arrays, -1 = nothing drawed yet
  21. Private m_DrawIndex As Integer = -1
  22.  
  23. Private Sub pbBody_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown
  24.  
  25. 'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down
  26. ' Start a new drawing: increase the counter and redim the arrays
  27. m_DrawIndex += 1
  28. ReDim Preserve sStartX(m_DrawIndex)
  29. ReDim Preserve sStartY(m_DrawIndex)
  30. ReDim Preserve sEndX(m_DrawIndex)
  31. ReDim Preserve sEndY(m_DrawIndex)
  32.  
  33. sStartX(m_DrawIndex) = e.X
  34. sStartY(m_DrawIndex) = e.Y
  35.  
  36. End Sub
  37.  
  38. Private Sub pbBody_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseUp
  39.  
  40. 'Initialise Ending Points Of Shape, Once Mouse Button Is Released
  41. sEndX(m_DrawIndex) = e.X
  42. sEndY(m_DrawIndex) = e.Y
  43.  
  44. 'Draw The Circle With The Current Starting, And Ending Values.
  45. 'We must subtract the Starting values from the Ending values,
  46. 'to make sure the shape's Starting and ending values are
  47. 'precisely those where you started drawing, and where you
  48. 'ended drawing.
  49. pbBody.CreateGraphics.DrawEllipse(pPen, sStartX(m_DrawIndex), sStartY(m_DrawIndex), _
  50. sEndX(m_DrawIndex) - sStartX(m_DrawIndex), sEndY(m_DrawIndex) - sStartY(m_DrawIndex))
  51.  
  52. End Sub
  53.  
  54. Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint
  55.  
  56. 'Draw the Final rectangle, as a repaint was issued
  57.  
  58. ' Redraw user drawings from the array
  59. Dim i As Integer
  60.  
  61. For i = 0 To m_DrawIndex
  62. e.Graphics.DrawEllipse(pPen, sStartX(i), sStartY(i), _
  63. sEndX(i) - sStartX(i), sEndY(i) - sStartY(i))
  64. Next i
  65.  
  66. End Sub
  67.  
  68. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  69.  
  70. Dim connAdd4 As New SqlConnection _
  71. ("Data Source=152.226.152.99\SQLEXPRESS,1433;" + "Initial Catalog=TAR;" + "User ID=Remote;" + "Password=123;")
  72.  
  73. Dim mySQL As String
  74.  
  75. 'Memory stream
  76. Dim ms As New System.IO.MemoryStream
  77. 'Write an image data into memory stream
  78. pbBody.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
  79.  
  80. 'Byte array
  81. Dim b() As Byte = ms.ToArray
  82. connAdd4.Open()
  83.  
  84. 'Parameterized Query
  85. mySQL = "insert into image (patientIC,picture) values (@p1,@p2)"
  86. Dim cmdAdd4 As New SqlCommand(mySQL, connAdd4)
  87.  
  88. 'Create/add parameters
  89. '@p1 - name of parameter, datatype,size,"columnname"
  90. cmdAdd4.Parameters.Add("@p1", SqlDbType.VarChar, 50, "patientIC")
  91. cmdAdd4.Parameters.Add("@p2", SqlDbType.Image, b.Length, "picture")
  92.  
  93. 'Assign value to the parameters
  94. cmdAdd4.Parameters("@p1").Value = txtId.Text
  95. cmdAdd4.Parameters("@p2").Value = b
  96.  
  97. 'Execute command
  98. cmdAdd4.ExecuteNonQuery()
  99. connAdd4.Close()
  100.  
  101. End Sub
  102. End Class

The above codes manage to save the image and patient's IC to MSSQL (binary data in MSSQL) and the multi selection also works (it wont disappeared when scroll up & down). so do I stil put in the Byte2Image, Image2Byte, SaveByteArray, LoadByteArray or do I just leave it as it was now?

And I don't know what is contain in the binary data when i retrieve as I havent tried.

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?

Thanks =)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC