 |
|
สอบถามปัญหาเขียนโค๊ด VB ดึงรูปภาพจากบัตรประชาชนลง Picture Box แต่เมื่อบันทึกรูปภาพไม่ได้ |
|
 |
|
|
 |
 |
|
สอบถามผู้รู้ [VB.net] เนื่องจากดึงรูปภาพจากบัตรประชาชนมาแสดงใน Picture Box แล้วเกิดปัญหาไม่สามารถบันทึกลง Microsoft SQL 2014 ได้ครับ
Code แสดงรูปภาพ
Code (VB.NET)
Sub ShowPicIDCard()
Dim Block_ID As Integer = 0
Dim Offsets As Integer = 377
Dim Sizes As Integer = 5120
Dim data_img As String
data_img = New String(CChar(" "), Sizes)
rc = Read_Data(card_type, Block_ID, Offsets, Sizes, data_img, status)
If (rc <> SCAPI_SUCCESS) Then
TB_StatusText.Text += error_text("Read_Data", rc, status) & Environment.NewLine
Return
End If
Dim byte_img() As Byte = System.Text.Encoding.Default.GetBytes(data_img, 2, data_img.Length - 2)
Dim mstream As New System.IO.MemoryStream(byte_img)
Me.PictureIDCard.Image = Image.FromStream(mstream)
End Sub
Source Code บันทึกพอกดบันทึกแล้วขึ้น Error
Code (VB.NET)
Sub SaveCustomer()
Dim sb As New StringBuilder
Try
Me.ConnectDB()
Dim sqlsave As String
sb = New StringBuilder
sb.Remove(0, sb.Length)
'\\ คำสั่ง SQL ที่จัดการการเพิ่มข้อมูลใน Database \\
sb.Append("INSERT INTO CUSTOMER(ImageName, Image, CU_CardID, CU_NameT, CU_NameE, CU_SEX, CU_Address, CU_TEL)")
sb.Append(" VALUES (@ImgName, @Img, '" & Me.LB_IDCard.Text & "','" & Me.LB_NameTH.Text & "','" & Me.LB_NameEN.Text & "','" & Me.LB_SexTH.Text & "','" & Me.LB_Address.Text & "','" & Me.TB_Telephone.Text & "')")
'//////////////////////////////////
Dim ImgName As SqlParameter = New SqlParameter("ImgName", System.Data.SqlDbType.VarChar, 100)
ImgName.Value = Me.LB_NumberPicture.Text.ToString()
com.Parameters.Add(ImgName)
'/////////////////////////////////
Dim Img As SqlParameter = New SqlParameter("@Img", System.Data.SqlDbType.Image)
Img.Value = Me.PictureIDCard.Image
com.Parameters.Add(Img)
'/////////////////////////////////
sqlsave = sb.ToString()
With com
.CommandType = CommandType.Text
.Connection = ConnectSQL
.Transaction = tr
.CommandText = sqlsave
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox("พบข้อผิดพลาดระหว่างบันทึกข้อมูล !!!", MsgBoxStyle.Exclamation, "พบข้อผิดพลาดระหว่างบันทึก")
End Try
End Sub

ขอบคุณครับ
Tag : .NET, VB.NET
|
ประวัติการแก้ไข 2022-09-28 12:56:00 2022-09-28 13:00:23 2022-09-28 13:00:53
|
 |
 |
 |
 |
Date :
2022-09-28 10:37:38 |
By :
vishnu |
View :
824 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error ว่าอะไรนำมาแปะด้วยครับ
|
 |
 |
 |
 |
Date :
2022-09-28 11:11:42 |
By :
009 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แปลงรูปภาพเป็น byte[] ก่อน ผ่านฟังก์ชันนี้
Code (VB.NET)
Public Function ImageToByteArray(ByVal imageIn As System.Drawing.Image) As Byte()
Using ms = New MemoryStream()
imageIn.Save(ms, imageIn.RawFormat)
Return ms.ToArray()
End Using
End Function
แล้วค่อย add parameter
แต่ถ้าไม่จำเป็นจริงๆ ควรบันทึกเป็น path ของรูปภาพน่าจะดีกว่า ลดขนาด DB ด้วย
|
 |
 |
 |
 |
Date :
2022-09-28 13:48:32 |
By :
009 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
return signature ต้องเป็น byte array ครับ ไม่ใช่ byte
...As Byte()
|
 |
 |
 |
 |
Date :
2022-09-29 10:16:14 |
By :
009 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|