HOME > .NET Framework > Forum > สอบถามพอดีต้องการบันทึกภาพที่อ่านมาจากบัตรประชาชน เเละไม่สามารถบันทึกได้ครับ ฟ้อง error A generic error occurred in GDI+
สอบถามพอดีต้องการบันทึกภาพที่อ่านมาจากบัตรประชาชน เเละไม่สามารถบันทึกได้ครับ ฟ้อง error A generic error occurred in GDI+
สอบถามพอดีต้องการบันทึกภาพที่อ่านมาจากบัตรประชาชน เเละไม่สามารถบันทึกได้ครับ ฟ้อง error A generic error occurred in GDI+
โค้ด ทั้งหมด ครับ
Code (VB.NET)
Imports System.IO
Imports ThaiNationalIDCard
Imports System.ComponentModel
Imports System.Data.SqlClient
Imports System.Drawing.Imaging
Public Class frmAddCustomer
Dim bgWorker As New BackgroundWorker
Dim con As SqlConnection = New SqlConnection("Data Source=localhost;uid=sa;pwd=;database=User")
Dim cmd As SqlCommand
Dim adapt As SqlDataAdapter
Private Sub frmSmartCard_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.F7
Call btnRead_Click(sender, e)
Case Keys.F10
Me.Close()
End Select
End Sub
Private Sub frmSmartCard_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Call SetupScreen()
'// Detect Smart card reader.
If Not GetReader() Then Return
ProgressBar1.Visible = False
'// Initialized BackGroundWorker
With bgWorker
.WorkerReportsProgress = True
.WorkerSupportsCancellation = True
End With
'// *********** IMPORTANT ***********
Control.CheckForIllegalCrossThreadCalls = False
'// Add Event Handler.
AddHandler bgWorker.DoWork, AddressOf bgWorker_DoWork
AddHandler bgWorker.RunWorkerCompleted, AddressOf bgWorker_RunWorkerCompleted
End Sub
'// ตรวจสอบเครื่องอ่านบัตรว่ามีอยู่หรือไม่
Function GetReader() As Boolean
Try
Dim ID As New ThaiIDCard
Dim readers = ID.GetReaders
If readers Is Nothing Then
Return False
Else
Return True
End If
Catch ex As Exception
MessageBox.Show("ไม่พบเครื่องอ่านบัตร โปรดตรวจสอบการเชื่อมต่อ.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Me.Close()
End Try
End Function
Private Sub btnRead_Click(sender As System.Object, e As System.EventArgs) Handles btnRead.Click
Call SetupScreen()
If Not GetReader() Then Return
btnRead.Enabled = False
'/
ProgressBar1.Style = ProgressBarStyle.Marquee
ProgressBar1.Visible = True
'// BackGroundWorker
bgWorker.RunWorkerAsync()
End Sub
Private Sub bgWorker_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs)
Try
'// -------------------------------------------
'// โค้ดส่วนของการอ่านข้อมูลบัตรประชาชน
Dim ID As New ThaiIDCard
Refresh()
Dim Personal As Personal = ID.readAllPhoto
If Not IsNothing(Personal) Then
With Personal
txtIDCard.Text = .Citizenid
txtPrefixThai.Text = .Th_Prefix
txtPrefixEng.Text = .En_Prefix
txtFirstNameThai.Text = .Th_Firstname
txtLastnameThai.Text = .Th_Lastname
txtFirstNameEng.Text = .En_Firstname
txtLastnameEng.Text = .En_Lastname
txtBirthDate.Text = Format(CDate(.Birthday.ToString), "dd/MM/yyyy")
'// คำนวณอายุ
'// If Not IsNothing(txtBirthDate.Text) Then lblAge.Text = CalcDate(txtBirthDate.Text, Now())
If Not IsNothing(txtBirthDate.Text) Then txtAge.Text = CalcDate(txtBirthDate.Text, Now())
'// 1 = ชาย, 2 = หญิง (นำไปแปลงค่าเพศในฟังค์ชั่นก่อน)
txtSex.Text = CheckSex(.Sex, txtSex).ToString
txtIssueDate.Text = Format(CDate(.Issue.ToString), "dd/MM/yyyy")
txtExpireDate.Text = Format(CDate(.Expire.ToString), "dd/MM/yyyy")
'// ที่อยู่
txtHouseNo.Text = .addrHouseNo
Dim nMoo As String = .addrVillageNo
Dim Mootxt As String = ""
Select Case (nMoo)
Case ""
Mootxt = ""
Case Else
Mootxt = nMoo.Substring(8)
End Select
txtVillageNo.Text = Mootxt
'// txtVillageNo.Text = .addrVillageNo
Dim nLane As String = .addrLane
Dim Lanetxt As String = ""
Select Case (nLane)
Case ""
Lanetxt = ""
Case Else
Lanetxt = nLane.Substring(3)
End Select
txtLane.Text = Lanetxt
'//txtLane.Text = .addrLane
Dim nRoad As String = .addrRoad
Dim Roadtxt As String = ""
Select Case (nLane)
Case ""
Roadtxt = ""
Case Else
Roadtxt = nRoad.Substring(3)
End Select
txtRoad.Text = Roadtxt
'//txtRoad.Text = .addrRoad
Dim nSubDistict As String = .addrTambol
Dim newstringSubDistict As String = nSubDistict.Substring(0, 4)
Dim stringSubDistict As String = ""
Select Case (newstringSubDistict)
Case "ตำบล"
stringSubDistict = nSubDistict.Substring(4)
'// stringSubDistict = "1"
Case Else
stringSubDistict = nSubDistict
'//stringSubDistict = "2"
End Select
txtTambol.Text = stringSubDistict
'//txtTambol.Text = .addrTambol
Dim nDistict As String = .addrAmphur
Dim newstringDistict As String = nDistict.Substring(0, 5)
Dim stringDistict As String = ""
Select Case (newstringDistict)
Case "อำเภอ"
stringDistict = nDistict.Substring(5)
Case Else
stringDistict = nDistict
End Select
txtAmphur.Text = stringDistict
'// txtAmphur.Text = .addrAmphur
Dim nProvince As String = .addrProvince
Dim newstringProvince As String = nProvince.Substring(0, 7)
Dim stringProvince As String = ""
Select Case (newstringProvince)
Case "จังหวัด"
stringProvince = nProvince.Substring(7)
Case Else
stringProvince = nProvince
End Select
txtProvince.Text = stringProvince
'// txtProvince.Text = .addrProvince
'// รูปภาพ
picData.Image = .PhotoBitmap
End With
'// -------------------------------------------
'//
ElseIf (ID.ErrorCode() > 0) Then
MessageBox.Show(ID.Error)
Else
MessageBox.Show("Catch All", "รายงานความผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
btnRead.Enabled = True
End Sub
'// Reading complete.
Private Sub bgWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)
ProgressBar1.Visible = False
'MessageBox.Show("Done Complete.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Function CheckSex(ByVal sex As Byte, ByRef txt As TextBox) As String
'// เก็บค่าตัวเลขไว้ใน Tag เผื่อไว้ตอนไปเก็บลงในฐานข้อมูล [กำหนดให้ ชาย=1 หญิง=2]
txt.Tag = sex
If sex = 1 Then
CheckSex = "ชาย"
Else
CheckSex = "หญิง"
End If
End Function
'// เคลียร์หน้าจอทั้งหมด
Sub SetupScreen()
'// หรือสั่งลูปเคลียร์ TextBox Control ทั้งหมดที่มีอยู่บนฟอร์ม
For Each tb As TextBox In Me.GroupBox1.Controls.OfType(Of TextBox)()
tb.Clear()
Next
picData.Image = Nothing
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
' / --------------------------------------------------------------------------------
' / ฟังค์ชั่นในการคำนวณหาความแตกต่างระหว่างวันเดือนปี (คำนวณหาอายุ)
' / คัดลอกโค้ดทั้งหมดมาจาก VB6
Public Function CalcDate(sDate As Date, eDate As Date) As String
Dim vDays As Integer
Dim vMonths As Integer
Dim vYears As Integer
'/ Parameters:
'/ sDate - ค่าวันเดือนปีเกิด (หรือวันเดือนปีที่ต้องการคำนวณหา)
'/ eDate - คำนวณเทียบกับวันเดือนปีปัจจุบัน (Now())
'/ Results:
'/ vYears - เก็บค่าความแตกต่างของจำนวนปี
'/ vMonths - เก็บค่าความแตกต่างของจำนวนเดือน
'/ vDays - เก็บค่าความแตกต่างของจำนวนวัน
'/ หาความแตกต่างของจำนวนเดือน
vMonths = DateDiff("m", sDate, eDate)
vDays = DateDiff("d", DateAdd("m", vMonths, sDate), eDate)
If vDays < 0 Then
vMonths = vMonths - 1
vDays = DateDiff("d", DateAdd("m", vMonths, sDate), eDate)
End If
vYears = vMonths \ 12 ' หารตัดเศษก็จะได้จำนวนปี
vMonths = vMonths Mod 12 ' การหารเอาเศษ โดยจะมีค่าระหว่าง 0, 1, 2, ... 11 ไม่มีทางเท่ากับ หรือ มากกว่า 12
'//CalcDate = "อายุ: " & vYears & " ปี " & vMonths & " เดือน " & vDays & " วัน."
CalcDate = vYears
End Function
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim Choose As String = ""
If rbtnSingle.Checked Then
Choose = rbtnSingle.Text
ElseIf rbtnMarry.Checked Then
Choose = rbtnMarry.Text
End If
If (txtIDCard.Text = "") Then
MessageBox.Show("กดอ่านค่าบัตร ก่อนบันทึก")
ElseIf (txtTel.Text = "") Then
MessageBox.Show("โปรดระบุเบอร์โทรศัพท์")
ElseIf ((txtTel.Text = "00") _
OrElse ((txtTel.Text = "000") _
OrElse ((txtTel.Text = "0000") _
OrElse ((txtTel.Text = "00000") _
OrElse ((txtTel.Text = "000000") _
OrElse ((txtTel.Text = "0000000") _
OrElse ((txtTel.Text = "00000000") _
OrElse ((txtTel.Text = "000000000") _
OrElse (txtTel.Text = "0000000000"))))))))) Then
MessageBox.Show("โปรดระบุเบอร์โทรศัพท์ที่ถูกต้อง หากไม่มีใส่ 0 ")
Else
Dim nimg As Image = picData.Image
Dim byteImage As Byte() = CType((New ImageConverter()).ConvertTo(nimg, GetType(Byte())), Byte())
Dim img As Image = Image.FromStream(New MemoryStream(byteImage))
Dim MyImage As Bitmap = New Bitmap(img, picData.Width, picData.Height)
picData.Image = CType(MyImage, Image)
cmd = New SqlCommand("insert into TblNewCustomer(IDCard, PrefixThai, PrefixEng, FirstNameThai, LastnameThai, FirstNameEng, LastnameEng," &
"BirthDate, Sex, AgeCustomer, TelCustomer, IssueDate, ExpireDate, HouseNo," &
"VillageNo, Lane, Road, SubDistict, Distict, Province,pic, CenterSrs) values( @IDCARD, @PREFIXTHAI, @PREFIXENG, @FIRSTNAMETHAI, @LASTNAMETHAI," & '//pic,
" @FIRSTNAMEENG, @LASTNAMEENG , @BIRTHDATE, @SEX, @AGECUSTOMER, @TELCUSTOMER, @ISSUEDATE, @EXPIREDATE, @HOUSENO," &
"@VILLAGENO, @LANE, @ROAD, @SUBDISTICT, @DISTICT, @PROVINCE,@PIC, @CENTERSRS)", con) '//@PIC,
con.Open()
cmd.Parameters.AddWithValue("@IDCARD", txtIDCard.Text)
cmd.Parameters.AddWithValue("@PREFIXTHAI", txtPrefixThai.Text)
cmd.Parameters.AddWithValue("@FIRSTNAMETHAI", txtFirstNameThai.Text)
cmd.Parameters.AddWithValue("@LASTNAMETHAI", txtLastnameThai.Text)
cmd.Parameters.AddWithValue("@PREFIXENG", txtPrefixEng.Text)
cmd.Parameters.AddWithValue("@FIRSTNAMEENG", txtFirstNameEng.Text)
cmd.Parameters.AddWithValue("@LASTNAMEENG", txtLastnameEng.Text)
cmd.Parameters.AddWithValue("@SEX", txtSex.Text)
cmd.Parameters.AddWithValue("@BIRTHDATE", txtBirthDate.Text)
cmd.Parameters.AddWithValue("@HOUSENO", txtHouseNo.Text)
cmd.Parameters.AddWithValue("@ISSUEDATE", txtIssueDate.Text)
cmd.Parameters.AddWithValue("@EXPIREDATE", txtExpireDate.Text)
cmd.Parameters.AddWithValue("@TELCUSTOMER", txtTel.Text)
cmd.Parameters.AddWithValue("@AGECUSTOMER", txtAge.Text)
'// cmd.Parameters.AddWithValue("@Stutus", Choose)
cmd.Parameters.AddWithValue("@VILLAGENO", txtVillageNo.Text)
cmd.Parameters.AddWithValue("@LANE", txtProvince.Text)
cmd.Parameters.AddWithValue("@ROAD", txtProvince.Text)
cmd.Parameters.AddWithValue("@SUBDISTICT", txtTambol.Text)
cmd.Parameters.AddWithValue("@DISTICT", txtAmphur.Text)
cmd.Parameters.AddWithValue("@PROVINCE", txtProvince.Text)
cmd.Parameters.AddWithValue("@CENTERSRS", cmbCenter.Text)
cmd.Parameters.AddWithValue("@PIC", byteImage)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("เพิ่มข้อมูลบัตรเรียบร้อย")
End If
'// ElseIf (Choose = "") Then
'// MessageBox.Show("B##08*2 2", 2, Microsoft.VisualBasic.ChrW(68))
'// Else
'// Dim cmd As SqlCommand = New SqlCommand("Select * FROM IDCARD_Custumer WHERE CusNum =@Name ")
'// cmd.Parameters.AddWithValue("@Name", m_txtbxtSCusnum.Text.Trim)
'// cmd.CommandType = CommandType.Text
'// cmd.Connection = con
'// con.Open
'// Dim rdr As SqlDataReader
'// rdr = cmd.ExecuteReader
'// If rdr.HasRows Then
'// MessageBox.Show("B'2I3 B##08B'2C+!H")
'// con.Close
'// Else
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
GroupBox1.Enabled = True
btnSave.Enabled = True
btnCancel.Enabled = True
btnExit.Enabled = False
End Sub
Private Sub GroupBox2_Enter(sender As Object, e As EventArgs) Handles GroupBox2.Enter
End Sub
Private Sub rbtnSingle_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnSingle.CheckedChanged
GroupBox2.Enabled = False
End Sub
Private Sub rbtnMarry_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnMarry.CheckedChanged
GroupBox2.Enabled = True
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
GroupBox1.Enabled = False
btnSave.Enabled = False
btnCancel.Enabled = False
btnExit.Enabled = True
End Sub
End Class
ส่วนที่ error
Code (VB.NET)
Dim nimg As Image = picData.Image
Dim byteImage As Byte() = CType((New ImageConverter()).ConvertTo(nimg, GetType(Byte())), Byte())
Dim img As Image = Image.FromStream(New MemoryStream(byteImage))
Dim MyImage As Bitmap = New Bitmap(img, picData.Width, picData.Height)
picData.Image = CType(MyImage, Image)
Tag : .NET, Ms SQL Server 2012, Win (Windows App), VB.NET, VS 2017 (.NET 4.x)
Dim bmp1 As Bitmap = New Bitmap(picData.Image)
'//Dim nimg As Image = picData.Image
Dim byteImage As Byte() = CType((New ImageConverter()).ConvertTo(bmp1, GetType(Byte())), Byte())
Dim img As Image = Image.FromStream(New MemoryStream(byteImage))
Dim MyImage As Bitmap = New Bitmap(img, picData.Width, picData.Height)
picData.Image = CType(MyImage, Image)