Saving Drawing on PictureBox to SQL Database

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

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

Saving Drawing on PictureBox to SQL Database

 
0
  #1
Jul 6th, 2009
Hi guy,

I currently having problem saving my drawing into the sql database.

Can I get some help here pls.

my current codes:
  1.  
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Imports System.IO
  5. Imports System.Drawing
  6. Imports System.Drawing.Drawing2D
  7.  
  8. Public Class Page_2
  9. ' for GCS TABLE
  10. Private value_GCS As Integer = 0
  11. Dim value_Eyes As String = ""
  12. .
  13. .
  14. .
  15. Dim value_UrinaryCatheter As String = ""
  16.  
  17. ' for freehand drawing
  18. Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement
  19. Dim myAlpha As Integer = 100 ' declare a Alpha variable
  20. Dim myUserColor As New Color() 'this is a color the user selects
  21. Dim myPenWidth As Single = 5 'set pen width variable
  22.  
  23. Private Sub Page_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  24.  
  25. lbl_Total.Text = CStr(value_GCS)
  26.  
  27. End Sub
  28.  
  29. Private Sub cbEyes4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbEyes4.MouseClick
  30.  
  31. cbEyes1.Checked = False
  32. .
  33. .
  34. .
  35. update_GCSTotal()
  36. value_Eyes = "4"
  37.  
  38. End Sub
  39.  
  40. .
  41. .
  42. .
  43.  
  44.  
  45. Private Sub cbVerbal4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbVerbal4.MouseClick
  46.  
  47. cbVerbal1.Checked = False
  48. .
  49. .
  50. .
  51. cbVerbal5.Checked = False
  52. update_GCSTotal()
  53. value_Verbal = "4"
  54.  
  55. Ebd Sub
  56.  
  57. Sub update_GCSTotal()
  58.  
  59. value_GCS = 0
  60.  
  61. If Me.cbEyes1.Checked Then
  62. value_GCS += CInt(Me.cbEyes1.Text)
  63. End If
  64.  
  65. .
  66. .
  67. .
  68. .
  69. .
  70. If Me.cbMotor5.Checked Then
  71. value_GCS += CInt(Me.cbMotor5.Text)
  72. End If
  73.  
  74. If Me.cbMotor6.Checked Then
  75. value_GCS += CInt(Me.cbMotor6.Text)
  76. End If
  77.  
  78. lbl_Total.Text = CStr(value_GCS)
  79.  
  80. End Sub
  81.  
  82.  
  83. Private Sub cbIntubationOral_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbIntubationOral.MouseClick
  84.  
  85. cbIntubationOral.Checked = True
  86. cbIntubationNasal.Checked = False
  87. value_Intubation = "Oral"
  88.  
  89. End Sub
  90.  
  91. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  92.  
  93. Dim connAdd As New SqlConnection _
  94. ("Data Source=152.226.152.99\SQLEXPRESS,1433;" + "Initial Catalog=TAR;" + "User ID=Remote;" + "Password=123;")
  95. 'Client's IP: 152.226.152.76 Host's PC: 152.226.152.99
  96.  
  97. .
  98. .
  99. .
  100.  
  101. Dim mySQL As String
  102.  
  103. Try
  104. connAdd.Open()
  105. Catch ex As Exception
  106. MessageBox.Show(ex.Message, "Error Open", MessageBoxButtons.OK, MessageBoxIcon.Error)
  107. End Try
  108.  
  109. mySQL = "insert into gcs (eyesOpen, verbal, motor, totalGcs) values ('" & Trim(value_Eyes) & "','" & Trim(value_Verbal) & "','" & Trim(value_Motor) & "','" & Trim(lbl_Total.Text) & "' ) "
  110.  
  111. Dim cmdAdd As New SqlCommand(mySQL, connAdd)
  112.  
  113. Try
  114. cmdAdd.ExecuteNonQuery()
  115. Catch ex As Exception
  116. MessageBox.Show(ex.Message, "Error Query", MessageBoxButtons.OK, MessageBoxIcon.Error)
  117. End Try
  118.  
  119. Dim Response As New Page_3()
  120. Page_3.Show()
  121. Me.Hide()
  122.  
  123. End Sub
  124.  
  125. Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
  126.  
  127. Dim Response As New Page_1()
  128. Page_1.Show()
  129. Me.Hide()
  130.  
  131. End Sub
  132.  
  133. Private Sub pbBody_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown
  134.  
  135. If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
  136.  
  137. mousePath.StartFigure() ' The L mouse is down so we need to start a new line in mousePath
  138.  
  139. End If
  140.  
  141. End Sub
  142.  
  143. Private Sub pbBody_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseMove
  144.  
  145. If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
  146.  
  147. Try
  148. mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath
  149.  
  150. Catch
  151. MsgBox("No way, Hose!")
  152. End Try
  153.  
  154. End If
  155.  
  156. pbBody.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
  157.  
  158. End Sub
  159.  
  160. Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint
  161. ' Here is where we do the actual painting
  162.  
  163. Try ' error trapping
  164.  
  165. myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
  166. 'change the value of myUserColor
  167.  
  168. myAlpha = 200 ' This will give the color a Alpha effect, you can set this to 255 if you want a full color
  169.  
  170. '*********************** NOTE ***********************************************
  171. 'The line below set the pen up with the ability to add user selected Alpha, Color and Penwidth
  172. ' A simpler, but less flexible solution would be to replace the line with the following code:
  173. 'Dim CurrentPen = New Pen(System.Drawing.Color.Black, myPenWidth)
  174. '************ End Note ***************************
  175.  
  176. Dim CurrentPen As New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
  177.  
  178. e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path! :)
  179.  
  180. Catch
  181. ' MsgBox("Not happening!")
  182. End Try
  183. End Sub
  184.  
  185. End Class

how do I do my saving and what is the datatype i used in my sql database for the images?

Thks
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
  #2
Jul 7th, 2009
what is the datatype i used in my sql database for the images?
I suggest using BLOB data type.

how do I do my saving
convert image to an array of bytes first. See Save binary data to SQL Server with VB.NET how to save byte array to SQL Server.

I assume you're using picture box control. See Convert image to byte array and vice versa how to get a byte array from an image.

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
  #3
Jul 7th, 2009
I suggest using BLOB data type.
I don't have BLOB datatype in my sql database choices. the available choices are bigInt, binary, bit, char, dateTime, decimal(18,0), float, image, int, money, nchar(10), ntext, numeric(18,0), nvarchar(50), nvarchar(MAX), real, smalldatetime, smallInt, smallMoney, sql_variant, text, timestamp, tinyInt, uniqueIdentifier, varBinary(50), varBinary(MAX), varchar(50), varchar(MAX), xml. OR u mean
  1. Dim image As BLOB = ""
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
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: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #4
Jul 7th, 2009
Tem, OP has Microsoft Sql Server database. OP cannot find a name of installed database.

DAWNIE,
You are using MS - SQL server and you have to use Image field type to store file content.

Image Field represents array of bytes.

To write file content, Use parameterized query.
Failure is not fatal, but failure to change might be. - John Wooden
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
  #5
Jul 7th, 2009
adatapost > OP cannot find a name of installed database.
The connection string seems ok to me

SQL server and you have to use Image field type to store file content
Sorry, my mistake. It's called Image in SQL Server, not BLOB
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
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: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #6
Jul 7th, 2009
No sir, it's OP's mistake. He/She mentioned mysql in his/her post.
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
  #7
Jul 7th, 2009
To write file content, Use parameterized query.
adatapost > Sorry, I don't understand the meaning of above statement.

it's OP's mistake. He/She mentioned mysql in his/her post
adatapost > Do you mean my mistake? Sry, whr did I posted wrong or something wrong with my codes?

convert image to an array of bytes first. See Save binary data to SQL Server with VB.NET how to save byte array to SQL Server.

I assume you're using picture box control. See Convert image to byte array and vice versa how to get a byte array from an image.
Teme64 > Does this website still can help me on saving my drew image to MS - SQL server?

Thks =)
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
  #8
Jul 8th, 2009
Teme64 > Does this website still can help me on saving my drew image to MS - SQL server?
Actually I'm not 100% sure. I haven't used the snippet in that kind of scenario, but I suggest you copy/paste the code(s) and try it out.

If I remember right, you were using picture box control and you drew user selections on it. And now you want to save it all to database, right?

Here's the picture box to byte array procedure
  1. ''' <summary>
  2. ''' Convert an image to array of bytes
  3. ''' </summary>
  4. ''' <param name="NewImage">Image to be converted</param>
  5. ''' <param name="ByteArr">Returns bytes</param>
  6. ''' <remarks></remarks>
  7. Public Sub Image2Byte(ByRef NewImage As Image, ByRef ByteArr() As Byte)
  8. '
  9. Dim ImageStream As System.IO.MemoryStream
  10.  
  11. Try
  12. ReDim ByteArr(0)
  13. If NewImage IsNot Nothing Then
  14. ImageStream = New System.IO.MemoryStream
  15. NewImage.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Jpeg)
  16. ReDim ByteArr(CInt(ImageStream.Length - 1))
  17. ImageStream.Position = 0
  18. ImageStream.Read(ByteArr, 0, CInt(ImageStream.Length))
  19. End If
  20. Catch ex As Exception
  21.  
  22. End Try
  23.  
  24. End Sub
You can test it with
  1. Dim ImageByteArr() As Byte = {0}
  2. Image2Byte(PictureBox1.Image, ImageByteArr)
and after that, check with the second picture box
  1. Byte2Image(PictureBox2.Image, ImageByteArr)
that you do get the same image (Byte2Image code not copied in here, see the link in my first post).

If you do get the image converted correctly back and forth to/from a byte array, use that saving binary data to SQL Server code (link to code was in my first post). And the correct data type with SQL Server is Image type like adatapost corrected. It's called BLOB in some other DB.

However, if you don't manage to do image and byte array conversions, post your code and possible error messages. I'll have to then test it myself why it wouldn't work.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
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: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Saving Drawing on PictureBox to SQL Database

 
0
  #9
Jul 8th, 2009
A sample demonstrate : create, draw, and save an image.
  1. Public Class Form2
  2. 'Graphics & Bitmap object variables
  3. Dim grp As Graphics
  4. Dim bmp As Bitmap
  5.  
  6. 'Save an image to disk file or database.
  7. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  8. 'Memory stream
  9. Dim ms As New System.IO.MemoryStream
  10.  
  11. 'Write an image data into memory stream
  12. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
  13.  
  14. 'To save an image into disk file
  15. 'bmp.Save("p1.png", System.Drawing.Imaging.ImageFormat.Png)
  16.  
  17. 'Byte array
  18. Dim b() As Byte = ms.ToArray
  19.  
  20. Dim cn As New System.Data.SqlClient.SqlConnection("CnStr")
  21.  
  22. 'Parameterized Query
  23. Dim cmd As New SqlClient.SqlCommand("insert into mytable (myid,myimage) values (@p1,@p2)", cn)
  24.  
  25. 'Create/add parameters
  26. '@p1 - name of parameter, datatype,size,"columnname"
  27. cmd.Parameters.Add("@p1", SqlDbType.Int, 4, "myid")
  28. cmd.Parameters.Add("@p2", SqlDbType.Image, b.Length, "myimage")
  29.  
  30. 'Assign value to the parameters
  31. cmd.Parameters("@p1").Value = 1
  32. cmd.Parameters("@p2").Value = b
  33.  
  34. 'Execute command
  35. cn.Open()
  36. cmd.ExecuteNonQuery()
  37. cn.Close()
  38. End Sub
  39.  
  40.  
  41. Private Sub Form2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  42. 'Create an image (bitmap) object of size(200,200)
  43.  
  44. If IsNothing(bmp) Then
  45. bmp = New Bitmap(200, 200)
  46. End If
  47.  
  48. grp = Graphics.FromImage(bmp)
  49. grp.DrawArc(Pens.Aqua, 10, 10, 200, 200, 100, 100)
  50. grp.DrawImage(bmp, 0, 0)
  51. e.Graphics.DrawImage(bmp, 0, 0)
  52. End Sub
  53. End Class
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
  #10
Jul 8th, 2009
 
'Write an image data into memory stream        
        pbBody.CreateGraphics.Save(ms, System.Drawing.Imaging.ImageFormat.Png)

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?

  1. 'Parameterized Query
  2. Dim cmd As New SqlClient.SqlCommand("insert into image (myid,myimage) values ('" & Trim(lblId.text) & "','" & Trim(?) & "')", cn)

How do I get the p2? isit the picturebox's name?
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