|
สำหรับรายละเอียดของเรื่อง Touchless SDK สามารถไปดูได้ที่ [VB.NET] การจับภาพและบันทึกไฟล์ภาพจากกล้องเว็บแคมด้วยการใช้งาน Touchless SDK ... ส่วนในบทความนี้จะเป็นการนำภาพที่แคปเจอร์มาใส่ PictureBox แล้วแปลงเป็นไฟล์ PDF ซึ่งต้อง Add References Syncfusion.Pdf.Base ของฟรีจากค่าย Syncfusion เข้ามาใช้งานก่อนด้วยครับ สามารถดูรายละเอียดได้จาก [VB.NET] โค้ดการใช้งาน XlsIO ของ Syncfusion เพื่อทำการแปลงไฟล์ Excel ให้เป็น PDF และ PNG ...
มาดูโค้ดกันเถอะ ...
- Imports TouchlessLib
- Imports System.IO
- Imports Syncfusion.Pdf
- Imports Syncfusion.Pdf.Graphics
- Public Class frmCamera
- Dim WebCamMgr As TouchlessLib.TouchlessMgr
- Dim strPDFPath As String
- Private Sub frmCamera_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- Try
- Timer1.Enabled = False
- WebCamMgr.CurrentCamera.Dispose()
- WebCamMgr.Cameras.Item(cmbCamera.SelectedIndex).Dispose()
- WebCamMgr.Dispose()
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- Private Sub frmCamera_Load(sender As Object, e As System.EventArgs) Handles Me.Load
- WebCamMgr = New TouchlessLib.TouchlessMgr
- Label2.Text = ""
- For i As Integer = 0 To WebCamMgr.Cameras.Count - 1
- cmbCamera.Items.Add(WebCamMgr.Cameras(i).ToString)
- Next
- If cmbCamera.Items.Count >= 0 Then
- cmbCamera.SelectedIndex = 0
- Timer1.Enabled = True
- btnSave.Enabled = False
- '// Create a folder in VB if it doesn't exist.
- strPDFPath = MyPath(Application.StartupPath) & "PDF"
- If (Not System.IO.Directory.Exists(strPDFPath)) Then System.IO.Directory.CreateDirectory(strPDFPath)
- '// Initialized and load images into DataGridView.
- Call InitDataGridView()
- Call LoadPDF2DatagridView()
- Else
- MessageBox.Show("No Web Camera, This application needs a webcam.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
- Me.Close()
- End If
- End Sub
- ' / --------------------------------------------------------------------------
- Private Sub LoadPDF2DatagridView()
- Dim directory As New IO.DirectoryInfo(strPDFPath)
- If directory.Exists Then
- Dim pdfFiles() As IO.FileInfo = directory.GetFiles("*.pdf")
- Dim row As String()
- Dim iArr() As String
- For Each pdfFile As IO.FileInfo In pdfFiles
- iArr = Split(pdfFile.FullName, "")
- row = New String() {iArr(UBound(iArr))}
- dgvData.Rows.Add(row)
- Next
- End If
- Label2.Text = "Total PDF : " & Me.dgvData.Rows.Count
- End Sub
- ' / --------------------------------------------------------------------------
- Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
- If picPreview Is Nothing Then Exit Sub
- Try
- '/ Create file name
- Dim sPicName As String = strPDFPath & Format(Now, "ddMMyy-hhmmss") & ".png"
- Dim b As Bitmap = picPreview.Image
- b.Save(sPicName, System.Drawing.Imaging.ImageFormat.Png)
- '// Copy filename and change to PDF extension.
- Dim sPDF As String = Mid(sPicName, 1, Len(sPicName) - 3) & "pdf"
- '/ Create a new PDF document.
- Dim doc As New PdfDocument()
- '/ Add a page to the document
- Dim page As PdfPage = doc.Pages.Add()
- '/ Getting page size to draw the image which fits the page.
- Dim pageSize As SizeF = page.GetClientSize()
- '/ Create PDF graphics for the page.
- Dim graphics As PdfGraphics = page.Graphics
- '/ Load the image from the disk.
- Dim image As New PdfBitmap(sPicName)
- '/ Draw the image
- graphics.DrawImage(image, New RectangleF(0, 0, pageSize.Width, pageSize.Height))
- '/ Save the document
- doc.Save(sPDF)
- '/ Close the document
- doc.Close(True)
- '// Show File name in the DataGridView.
- Dim iArr() As String
- iArr = Split(sPDF, "")
- Dim row As String()
- row = New String() {iArr(UBound(iArr))}
- dgvData.Rows.Add(row)
- '//
- Label2.Text = "Total PDF : " & Me.dgvData.Rows.Count
- Me.dgvData.Focus()
- SendKeys.Send("^{HOME}")
- SendKeys.Send("^{DOWN}")
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- ' / --------------------------------------------------------------------------
- Private Sub dgvData_DoubleClick(sender As Object, e As System.EventArgs) Handles dgvData.DoubleClick
- If Me.dgvData.Rows.Count = 0 Then Return
- Process.Start(strPDFPath & dgvData.CurrentRow.Cells(0).Value.ToString)
- End Sub
- ' / --------------------------------------------------------------------------
- Private Sub InitDataGridView()
- Me.dgvData.Columns.Clear()
- '// Initialize DataGridView Control
- With dgvData
- .AllowUserToAddRows = False
- .AllowUserToDeleteRows = False
- .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
- .AutoResizeColumns()
- .AllowUserToResizeColumns = True
- .AllowUserToResizeRows = True
- .SelectionMode = DataGridViewSelectionMode.FullRowSelect
- End With
- '// Declare columns type.
- '// Add 1th column (Index = 0), Show image file name.
- Dim FName As New DataGridViewTextBoxColumn()
- dgvData.Columns.Add(FName)
- With FName
- .HeaderText = "File Name"
- .ReadOnly = True
- .Visible = True
- End With
- '//
- Me.dgvData.Focus()
- End Sub
- Private Sub cmbCamera_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbCamera.SelectedIndexChanged
- WebCamMgr.CurrentCamera = WebCamMgr.Cameras.ElementAt(cmbCamera.SelectedIndex)
- '//
- 'WebCamMgr.CurrentCamera.CaptureHeight = 480
- 'WebCamMgr.CurrentCamera.CaptureWidth = 640
- End Sub
- Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
- picFeed.Image = WebCamMgr.CurrentCamera.GetCurrentImage()
- End Sub
- Private Sub btnCapture_Click(sender As System.Object, e As System.EventArgs) Handles btnCapture.Click
- picPreview.Image = WebCamMgr.CurrentCamera.GetCurrentImage()
- btnSave.Enabled = True
- End Sub
- '// Delete PDF & PNG File.
- Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
- If Me.dgvData.Rows.Count = 0 Then Return
- Try
- Dim sPic As String = dgvData.CurrentRow.Cells(0).Value.ToString
- sPic = Mid(sPic, 1, Len(sPic) - 3) & "png"
- '// Delete files in folder.
- FileSystem.Kill(strPDFPath & dgvData.CurrentRow.Cells(0).Value.ToString)
- FileSystem.Kill(strPDFPath & sPic)
- '// Delete current row from dgvData
- dgvData.Rows.Remove(dgvData.CurrentRow)
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- End Class
คัดลอกไปที่คลิปบอร์ด
ฟังค์ชั่นการกำหนดตำแหน่งพาธ ... โมดูลหากิน 5555+
- Module modFunction
- ' / Get my project path
- ' / Ex.
- ' / AppPath = C:\My Project\bin\debug
- ' / Replace "\bin\debug" with ""
- ' / Return : C:\My Project\
- Public Function MyPath(AppPath As String) As String
- '/ MessageBox.Show(AppPath);
- AppPath = AppPath.ToLower()
- MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "")
- '// Check the backslash symbol (ASCII Code = 92) on the far right. If not, add one at the end.
- If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
- End Function
- End Module
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|