23-10-17, 04:30 PM
(23-10-17, 03:23 AM)khodor1985 كتب : تابع الفيديوهات :
https://www.youtube.com/watch?v=utheGCVkvs0
https://www.youtube.com/watch?v=r0pLHP7x4R8
اول شي صديقي تحتاج ان يكون العمود في قاعدة البيانات image
كود :
Dim imagepath As String
Dim myStream As IO.Stream = Nothing
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Dim openFileDialog1 As New OpenFileDialog()
'Set the Filter.
openFileDialog1.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
imagepath = openFileDialog1.FileName
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
Else
Return
End If
End SubPHP كود :
Private Function ImageToStream(ByVal fileName As String) As Byte()
Dim stream As New MemoryStream()
tryagain:
Try
Dim image As New Bitmap(fileName)
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
GoTo tryagain
End Try
Return stream.ToArray()
End Function
كود :
Private Sub btn_with_pic_Click(sender As Object, e As EventArgs) Handles btn_with_pic.Click
Try
Dim fName As String
fName = imagepath
If File.Exists(fName) Then
Dim content As Byte() = ImageToStream(fName)
'فحص الاتصال بقاعدة البيانات
If SQL.conn.State = ConnectionState.Open Then
SQL.conn.Close()
End If
SQL.conn.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "insert into customers (cust_id,name,image,date) values(@cust_id,@name,@image,@date)"
cmd.Parameters.AddWithValue("@cust_id", (id_number_txt.Text))
cmd.Parameters.AddWithValue("@name", (name_txt.Text))
cmd.Parameters.AddWithValue("@image", (content))
cmd.Parameters.AddWithValue("@date", (datetoday))
cmd.Connection = SQL.conn
cmd.ExecuteNonQuery()
SQL.conn.Close()
Else
MsgBox(fName & " الصورة المختارة ليست موجودة او غير صالحة ", vbCritical, "حصل خطأ")
End If
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub