so do I stil put in the Byte2Image, Image2Byte, SaveByteArray, LoadByteArray or do I just leave it as it was now?
) And like I posted before, Adatapost's code is pretty much same as mine.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?
the picture display doesnt have the pts that i had circle

Option Explicit On Option Strict On Public Class Form2 Public BitmapCanvas As Bitmap Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' ' Saveing graphics(drawline,drawellipse,ect.) to a file ' http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/0184b1c0-90d0-4660-ba78-a615a8231fe9/ ' ' Drawing in VB .Net Win Forms ' http://channel9.msdn.com/forums/TechOff/229119-Drawing-in-VB-Net-Win-Forms/ BitmapCanvas = New Bitmap("D:\image.jpg") PictureBox1.Image = BitmapCanvas PictureBox1.Invalidate() ' Force redraw End Sub ' "Drawing" arrays. ARRAYS ARE OBSOLETE NOW, YOU CAN USE "NORMAL" VARIABLES Private sStartX() As Integer Private sStartY() As Integer Private sEndX() As Integer Private sEndY() As Integer ' Remove New and (Color.White), they're just for debugging Private pPen As New System.Drawing.Pen(Color.White) ' DECLARE THIS AS AN ARRAY IF THE PEN CAN BE CHANGED ' Counter for the arrays, -1 = nothing drawed yet Private m_DrawIndex As Integer = -1 Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown 'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down ' Start a new drawing: increase the counter and redim the arrays m_DrawIndex += 1 ReDim Preserve sStartX(m_DrawIndex) ReDim Preserve sStartY(m_DrawIndex) ReDim Preserve sEndX(m_DrawIndex) ReDim Preserve sEndY(m_DrawIndex) 'ReDim Preserve pPen(m_DrawIndex) ' Only if pPen is an array too ' sStartX(m_DrawIndex) = e.X sStartY(m_DrawIndex) = e.Y End Sub Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp Dim Gr As Graphics 'Initialise Ending Points Of Shape, Once Mouse Button Is Released sEndX(m_DrawIndex) = e.X sEndY(m_DrawIndex) = e.Y 'Draw The Circle With The Current Starting, And Ending Values. 'We must subtract the Starting values from the Ending values, 'to make sure the shape's Starting and ending values are 'precisely those where you started drawing, and where you 'ended drawing. ' Get graphics object from the bitmap to draw on Gr = Graphics.FromImage(BitmapCanvas) ' Draw Gr.DrawEllipse(pPen, sStartX(m_DrawIndex), sStartY(m_DrawIndex), _ sEndX(m_DrawIndex) - sStartX(m_DrawIndex), sEndY(m_DrawIndex) - sStartY(m_DrawIndex)) ' Assign bitmap to picture box control PictureBox1.Image = BitmapCanvas PictureBox1.Invalidate() ' Force redraw ' Dispose graphics! Gr.Dispose() End Sub Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click ' Me.Hide() End Sub End Class
Option Strict On Option Explicit On Imports System.Drawing.Imaging ' Image related methods etc. Imports System.IO ' MemoryStream Imports System.Data.SqlClient ' SQl Server related classes Public Class Form1 ' Class global variable Private m_CnStr As String = "Data Source=.\SQLEXPRESS; INITIAL CATALOG=<dbname>; UID=xxx; Password=xxx;" ' Connection string to DB ' Table structure: ' ImageTable: ' ' ImageID varchar(50) ' BlobField image ''' <summary> ''' Convert a byte array to an Image ''' </summary> ''' <param name="NewImage">Image to be returned</param> ''' <param name="ByteArr">Contains bytes to be converted</param> ''' <remarks></remarks> Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte) ' Dim ImageStream As MemoryStream Try If ByteArr.GetUpperBound(0) > 0 Then ImageStream = New MemoryStream(ByteArr) NewImage = Image.FromStream(ImageStream) Else NewImage = Nothing End If Catch ex As Exception NewImage = Nothing End Try End Sub ''' <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 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 ''' <summary> ''' Save a byte array to database ''' </summary> ''' <param name="ByteArr">Contains bytes to be saved</param> ''' <param name="ImageID">ID for the image</param> ''' <remarks></remarks> Public Sub SaveByteArray(ByVal ByteArr() As Byte, ByVal ImageID As String) ' Dim strSQL As String Dim oConn As SqlConnection Dim oCmd As SqlCommand Dim oVarCharParam As SqlParameter Dim oBLOBParam As SqlParameter Try ' Create and open connection object oConn = New SqlConnection(m_CnStr) oConn.Open() ' Insert statement ' Notice that @BLOBValue is a placeholder for the actual data strSQL = "INSERT INTO ImageTable (ImageID, BlobField) VALUES (@IDValue, @BLOBValue)" ' Create a command object oCmd = oConn.CreateCommand() ' Set SQL statement oCmd.CommandText = strSQL ' Create a command parameter oVarCharParam = New SqlParameter("@IDValue", SqlDbType.VarChar, _ 50, ParameterDirection.Input.ToString) ' Set the actual data oVarCharParam.Value = ImageID ' Add this parameter to the command oCmd.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 oCmd.Parameters.Add(oBLOBParam) ' Execute SQL statement oCmd.ExecuteNonQuery() ' Close the connection oConn.Close() Catch ex As Exception End Try End Sub ''' <summary> ''' Load a byte array from database ''' </summary> ''' <param name="ByteArr">Contains bytes from the database</param> ''' <param name="ImageID">ID for the image</param> ''' <remarks></remarks> Public Sub LoadByteArray(ByRef ByteArr() As Byte, ByVal ImageID As String) ' Dim strSQL As String Dim oConn As SqlConnection Dim oCmd As SqlCommand Dim ValueFromDB As Object ' ExecuteScalar method returns an object Try ' Create and open connection object oConn = New SqlConnection(m_CnStr) oConn.Open() ' Select statement strSQL = "SELECT BlobField FROM ImageTable WHERE ImageID='" & ImageID & "'" ' Create a command object oCmd = oConn.CreateCommand() ' Set SQL statement oCmd.CommandText = strSQL ' Execute SQL statement ValueFromDB = oCmd.ExecuteScalar() ' Close the connection oConn.Close() ' Convert returned object to an array of bytes If ValueFromDB IsNot DBNull.Value Then ByteArr = CType(ValueFromDB, Byte()) End If Catch ex As Exception End Try End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' ' DEBUG: Initial ID TextBox1.Text = "patient101" End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ' Saves the image from the bitmap buffer to SQL Server Dim ImageByteArr(0) As Byte ' An array to hold image (bytes) Dim PatientID As String ' ID for the image Form2.ShowDialog() ' Draw the image first ' Get ID from a text box control and remove spaces PatientID = TextBox1.Text.Trim ' Convert image to an array of bytes ' USE FORM2 BITMAP, NOT PICTURE BOX CONTROL Image2Byte(Form2.BitmapCanvas, ImageByteArr) ' Save an array of bytes to SQL Server's field of type Image SaveByteArray(ImageByteArr, PatientID) End Sub Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click ' Loads the image to the picture box control from SQL Server Dim ImageByteArr(0) As Byte ' An array to hold image (bytes) Dim PatientID As String ' ID for the image ' Get ID from a text box control and remove spaces PatientID = TextBox1.Text.Trim ' Load an array of bytes from SQL Server's field of type Image LoadByteArray(ImageByteArr, PatientID) ' Convert an array of bytes to image Byte2Image(PictureBox1.Image, ImageByteArr) End Sub End Class
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?
which is the part of the codes that link form2's drew image into main form and retrieval form?
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.I didnt manage to get the drawing over to my main form
Public Class DrawingForm Public BitmapCanvas As Bitmap Private Sub Drawing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load BitmapCanvas = New Bitmap("D:\Image\Human Anatomy.JPG") pbBody.Image = BitmapCanvas pbBody.Invalidate() ' Force redraw End Sub Private sStartX As Integer Private sStartY As Integer Private sEndX As Integer Private sEndY As Integer 'Create And Initialise Pens To Draw The Particular Outline Shapes With. Color : Black, Width : 1 Dim pPen As New Pen(Color.Black, 1) Private Sub pbBody_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown 'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down sStartX = e.X sStartY = e.Y End Sub Private Sub pbBody_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseUp Dim Gr As Graphics 'Initialise Ending Points Of Shape, Once Mouse Button Is Released sEndX = e.X sEndY = e.Y 'Draw The Circle With The Current Starting, And Ending Values. 'We must subtract the Starting values from the Ending values, 'to make sure the shape's Starting and ending values are 'precisely those where you started drawing, and where you 'ended drawing. ' Get graphics object from the bitmap to draw on Gr = Graphics.FromImage(BitmapCanvas) ' Draw Gr.DrawEllipse(pPen, sStartX, sStartY, _ sEndX - sStartX, sEndY - sStartY) ' Assign bitmap to picture box control pbBody.Image = BitmapCanvas pbBody.Invalidate() ' Force redraw ' Dispose graphics! Gr.Dispose() End Sub Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click Me.Hide() End Sub End Class
Imports System.Data Imports System.Data.SqlClient Imports System.IO Imports System.Drawing.Imaging Private Sub Page_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' DEBUG: Initial ID txtId.Text = "" End Sub ''' <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 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 ''' <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 ' Create and open connection object Dim connAdd4 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "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 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
Imports System.Drawing.Imaging ' Image related methods etc. Imports System.IO ' MemoryStream Imports System.Data.SqlClient ' SQl Server related classes Imports System.Data Public Class Page2 ''' <summary> ''' Convert a byte array to an Image ''' </summary> ''' <param name="NewImage">Image to be returned</param> ''' <param name="ByteArr">Contains bytes to be converted</param> ''' <remarks></remarks> Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte) ' Dim ImageStream As MemoryStream Try If ByteArr.GetUpperBound(0) > 0 Then ImageStream = New MemoryStream(ByteArr) NewImage = Image.FromStream(ImageStream) Else NewImage = Nothing End If Catch ex As Exception NewImage = Nothing End Try End Sub ''' <summary> ''' Load a byte array from database ''' </summary> ''' <param name="ByteArr">Contains bytes from the database</param> ''' <param name="patientIC">Patient's IC for Image</param> ''' <remarks></remarks> Public Sub LoadByteArray(ByRef ByteArr() As Byte, ByVal patientIC As String) ' Dim mySQL As String Dim cmdGet4 As SqlCommand Dim ValueFromDB As Object ' ExecuteScalar method returns an object Try ' Create and open connection object Dim connGet4 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;") connGet4.Open() ' Select statement mySQL = "SELECT picture FROM image WHERE patientIC='" & Trim(txtId.Text) & "'" ' Create a command object cmdGet4 = connGet4.CreateCommand() ' Set SQL statement cmdGet4.CommandText = mySQL ' Execute SQL statement ValueFromDB = cmdGet4.ExecuteScalar() ' Close the connection connGet4.Close() ' Convert returned object to an array of bytes If ValueFromDB IsNot DBNull.Value Then ByteArr = CType(ValueFromDB, Byte()) End If Catch ex As Exception End Try End Sub Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click Dim connGet As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;") Dim connGet1 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;") Dim connGet2 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;") Dim connGet3 As New SqlConnection _ ("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;") connGet.Open() Dim cmdGet As New SqlCommand("select * from gcs where patientIC = '" & Trim(txtId.Text) & "'", connGet) Dim dr As SqlDataReader dr = cmdGet.ExecuteReader() If dr.Read() Then lblEyes.Text = dr("eyesOpen") lblVerbal.Text = dr("verbal") lblMotor.Text = dr("motor") lblTotalGCS.Text = dr("totalGcs") End If dr.Close() connGet.Close() connGet1.Open() Dim cmdGet1 As New SqlCommand("select * from vitalSign where patientIC = '" & Trim(txtId.Text) & "'", connGet1) Dim dr1 As SqlDataReader dr1 = cmdGet1.ExecuteReader() If dr1.Read() Then lblPulse.Text = dr1("pulse") lblRespiration.Text = dr1("respiration") lblSystolic.Text = dr1("systolic") lblDiastolic.Text = dr1("diastolic") lblMean.Text = dr1("mean") lblSpo2.Text = dr1("spo2") lblTemperature.Text = dr1("temperature") End If dr1.Close() connGet1.Close() connGet2.Open() Dim cmdGet2 As New SqlCommand("select * from respiratoryAssist where patientIC = '" & Trim(txtId.Text) & "'", connGet2) Dim dr2 As SqlDataReader dr2 = cmdGet2.ExecuteReader() If dr2.Read() Then lblIntubationSize.Text = dr2("iSize") lblIntubationMarking.Text = dr2("marking") lblIntubation.Text = dr2("intubation") lblChestTubeSize.Text = dr2("cSize") lblChestTube.Text = dr2("chestTube") End If dr2.Close() connGet2.Close() connGet3.Open() Dim cmdGet3 As New SqlCommand("select * from drain where patientIC = '" & Trim(txtId.Text) & "'", connGet3) Dim dr3 As SqlDataReader dr3 = cmdGet3.ExecuteReader() If dr3.Read() Then lblNasogastricTube.Text = dr3("nasogastricTube") lblUrinaryCatheter.Text = dr3("urinaryCatheter") lblLtArm.Text = dr3("ivPlugLeft") lblRtArm.Text = dr3("ivPlugRight") lblOthers.Text = dr3("others") End If dr3.Close() connGet3.Close() ' 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) ' Convert an array of bytes to image Byte2Image(pbBody.Image, ImageByteArr) End Sub Private Sub Page2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' DEBUG: Initial ID txtId.Text = "" End Sub End Class
i duno wat went wrong whereby ur example works but not on my codes.
Dim connGet4 As New SqlConnection _
("Data Source=DATASOURCE;" + "Initial Catalog=TABLENAME;" + "User ID=UID;" + "Password=PW;")
' 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
i also try putting the drawing part and main form into 1 form but i didnt manage to get it worked
| DaniWeb Message | |
| Cancel Changes | |