| | |
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:
Solved Threads: 0
Hi guy,
I currently having problem saving my drawing into the sql database.
Can I get some help here pls.
my current codes:
how do I do my saving and what is the datatype i used in my sql database for the images?
Thks
I currently having problem saving my drawing into the sql database.
Can I get some help here pls.
my current codes:
VB.NET Syntax (Toggle Plain Text)
Imports System.Data Imports System.Data.SqlClient Imports System.IO Imports System.Drawing Imports System.Drawing.Drawing2D Public Class Page_2 ' for GCS TABLE Private value_GCS As Integer = 0 Dim value_Eyes As String = "" . . . Dim value_UrinaryCatheter As String = "" ' for freehand drawing Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement Dim myAlpha As Integer = 100 ' declare a Alpha variable Dim myUserColor As New Color() 'this is a color the user selects Dim myPenWidth As Single = 5 'set pen width variable Private Sub Page_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lbl_Total.Text = CStr(value_GCS) End Sub Private Sub cbEyes4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbEyes4.MouseClick cbEyes1.Checked = False . . . update_GCSTotal() value_Eyes = "4" End Sub . . . Private Sub cbVerbal4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbVerbal4.MouseClick cbVerbal1.Checked = False . . . cbVerbal5.Checked = False update_GCSTotal() value_Verbal = "4" Ebd Sub Sub update_GCSTotal() value_GCS = 0 If Me.cbEyes1.Checked Then value_GCS += CInt(Me.cbEyes1.Text) End If . . . . . If Me.cbMotor5.Checked Then value_GCS += CInt(Me.cbMotor5.Text) End If If Me.cbMotor6.Checked Then value_GCS += CInt(Me.cbMotor6.Text) End If lbl_Total.Text = CStr(value_GCS) End Sub Private Sub cbIntubationOral_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cbIntubationOral.MouseClick cbIntubationOral.Checked = True cbIntubationNasal.Checked = False value_Intubation = "Oral" End Sub Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click Dim connAdd As New SqlConnection _ ("Data Source=152.226.152.99\SQLEXPRESS,1433;" + "Initial Catalog=TAR;" + "User ID=Remote;" + "Password=123;") 'Client's IP: 152.226.152.76 Host's PC: 152.226.152.99 . . . Dim mySQL As String Try connAdd.Open() Catch ex As Exception MessageBox.Show(ex.Message, "Error Open", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try mySQL = "insert into gcs (eyesOpen, verbal, motor, totalGcs) values ('" & Trim(value_Eyes) & "','" & Trim(value_Verbal) & "','" & Trim(value_Motor) & "','" & Trim(lbl_Total.Text) & "' ) " Dim cmdAdd As New SqlCommand(mySQL, connAdd) Try cmdAdd.ExecuteNonQuery() Catch ex As Exception MessageBox.Show(ex.Message, "Error Query", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try Dim Response As New Page_3() Page_3.Show() Me.Hide() End Sub Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click Dim Response As New Page_1() Page_1.Show() Me.Hide() End Sub Private Sub pbBody_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down mousePath.StartFigure() ' The L mouse is down so we need to start a new line in mousePath End If End Sub Private Sub pbBody_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseMove If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down Try mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath Catch MsgBox("No way, Hose!") End Try End If pbBody.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event End Sub Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint ' Here is where we do the actual painting Try ' error trapping myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to 'change the value of myUserColor myAlpha = 200 ' This will give the color a Alpha effect, you can set this to 255 if you want a full color '*********************** NOTE *********************************************** 'The line below set the pen up with the ability to add user selected Alpha, Color and Penwidth ' A simpler, but less flexible solution would be to replace the line with the following code: 'Dim CurrentPen = New Pen(System.Drawing.Color.Black, myPenWidth) '************ End Note *************************** Dim CurrentPen As New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path! :) Catch ' MsgBox("Not happening!") End Try End Sub End Class
how do I do my saving and what is the datatype i used in my sql database for the images?
Thks
•
•
•
•
what is the datatype i used in my sql database for the images?
•
•
•
•
how do I do my saving
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
Teme64 @ Windows Developer Blog
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
•
•
•
•
I suggest using BLOB data type.
VB.NET Syntax (Toggle Plain Text)
Dim image As BLOB = ""
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.
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
adatapost > OP cannot find a name of installed database.
The connection string seems ok to me
Sorry, my mistake. It's called Image in SQL Server, not BLOB
The connection string seems ok to me
•
•
•
•
SQL server and you have to use Image field type to store file content
Teme64 @ Windows Developer Blog
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
•
•
•
•
To write file content, Use parameterized query.
•
•
•
•
it's OP's mistake. He/She mentioned mysql in his/her post
•
•
•
•
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.
Thks =)
•
•
•
•
Teme64 > Does this website still can help me on saving my drew image to MS - SQL server?
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
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(ByRef NewImage As Image, ByRef ByteArr() As Byte) ' Dim ImageStream As System.IO.MemoryStream Try ReDim ByteArr(0) If NewImage IsNot Nothing Then ImageStream = New System.IO.MemoryStream NewImage.Save(ImageStream, System.Drawing.Imaging.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
VB.NET Syntax (Toggle Plain Text)
Dim ImageByteArr() As Byte = {0} Image2Byte(PictureBox1.Image, ImageByteArr)
VB.NET Syntax (Toggle Plain Text)
Byte2Image(PictureBox2.Image, ImageByteArr)
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.
Teme64 @ Windows Developer Blog
A sample demonstrate : create, draw, and save an image.
VB.NET Syntax (Toggle Plain Text)
Public Class Form2 'Graphics & Bitmap object variables Dim grp As Graphics Dim bmp As Bitmap 'Save an image to disk file or database. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Memory stream Dim ms As New System.IO.MemoryStream 'Write an image data into memory stream bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png) 'To save an image into disk file 'bmp.Save("p1.png", 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 mytable (myid,myimage) values (@p1,@p2)", cn) 'Create/add parameters '@p1 - name of parameter, datatype,size,"columnname" cmd.Parameters.Add("@p1", SqlDbType.Int, 4, "myid") cmd.Parameters.Add("@p2", SqlDbType.Image, b.Length, "myimage") 'Assign value to the parameters cmd.Parameters("@p1").Value = 1 cmd.Parameters("@p2").Value = b 'Execute command cn.Open() cmd.ExecuteNonQuery() cn.Close() End Sub Private Sub Form2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint 'Create an image (bitmap) object of size(200,200) If IsNothing(bmp) Then bmp = New Bitmap(200, 200) End If grp = Graphics.FromImage(bmp) grp.DrawArc(Pens.Aqua, 10, 10, 200, 200, 100, 100) grp.DrawImage(bmp, 0, 0) e.Graphics.DrawImage(bmp, 0, 0) End Sub End Class
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Apr 2009
Posts: 48
Reputation:
Solved Threads: 0
'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?
VB.NET Syntax (Toggle Plain Text)
'Parameterized Query 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?
![]() |
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 |
"crystal .net .net2008 2008 access add advanced application array assignment basic beginner box browser button buttons center click code combo convert cpu cuesent data database datagrid datagridview datetimepicker designer dissertation dissertations dissertationtopic dosconsolevb.net editvb.net employees excel exists firewall forms html images isnumericfuntioncall listview map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity open pan pdf picturebox picturebox2 port position print printpreview record regex reuse right-to-left save search serial settings socket sorting sqldatbase sqlserver storedprocedure temp textbox timer timespan transparency txttoxmlconverter useraccounts usercontol vb vb.net vb.nettoolboxvisualbasic2008sidebar vba vbnet vista visual visualbasic visualbasic.net visualstudio.net web wpf wrapingcode xml year






