Imports System.IO
Imports System.Linq
Imports WIA
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
' التحقق من توفر الأجهزة
Dim deviceManager As New DeviceManager()
Dim scannerDevice = deviceManager.DeviceInfos.Cast(Of DeviceInfo)().
FirstOrDefault(Function(device) device.Type = WiaDeviceType.ScannerDeviceType)
If scannerDevice Is Nothing Then
MessageBox.Show(
"لا يوجد جهاز ماسح ضوئي متصل. الرجاء توصيل الجهاز والمحاولة مرة أخرى.",
"خطأ",
MessageBoxButtons.OK,
MessageBoxIcon.Warning)
Return
End If
' إنشاء واجهة الماسح الضوئي
Dim dialog As New WIA.CommonDialog()
Dim image As ImageFile = dialog.ShowAcquireImage(AlwaysSelectDevice:=True)
If image IsNot Nothing Then
' بناء المسار لحفظ الصورة
Dim folderPath As String = "C:\scanner"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Dim fileName As String = "My Files Scanned" & DateTime.Now.ToString("hhmmss") & ".jpg"
Dim fullPath As String = Path.Combine(folderPath, fileName)
' حفظ الصورة في المسار المحدد
image.SaveFile(fullPath)
' عرض الصورة في PictureBox
PictureBox1.Image = image.FromFile(fullPath)
End If
Catch ex As COMException
MessageBox.Show(
"حدث خطأ أثناء محاولة الوصول إلى جهاز الماسح الضوئي. الرجاء التحقق من الجهاز والمحاولة مرة أخرى.",
"خطأ",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Catch ex As Exception
MessageBox.Show(
"حدث خطأ غير متوقع: " & ex.Message,
"خطأ",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub
End Class