Imports WIA
Imports System.IO
Public Class Form1
Private Sub BtnScan_Click(sender As Object, e As EventArgs) Handles BtnScan.Click
Try
' إنشاء حاوية للأجهزة
Dim deviceManager As New DeviceManager()
' اختيار الجهاز (الماسح الضوئي)
Dim selectedDevice As Device = Nothing
For Each deviceInfo As DeviceInfo In deviceManager.DeviceInfos
If deviceInfo.Type = WiaDeviceType.ScannerDeviceType Then
selectedDevice = deviceInfo.Connect()
Exit For
End If
Next
If selectedDevice Is Nothing Then
MessageBox.Show("لم يتم العثور على أي ماسح ضوئي متصل.")
Return
End If
' تحديد خيارات المسح الضوئي
Dim commonDialog As New CommonDialog()
Dim imageFile As ImageFile = commonDialog.ShowAcquireImage(WiaDeviceType.ScannerDeviceType)
If imageFile IsNot Nothing Then
' حفظ الصورة الممسوحة
Dim outputPath As String = Path.Combine(Application.StartupPath, "ScannedImage.jpg")
SaveImageToFile(imageFile, outputPath)
MessageBox.Show("تم حفظ الصورة بنجاح في: " & outputPath)
Else
MessageBox.Show("تم إلغاء عملية المسح.")
End If
Catch ex As Exception
MessageBox.Show("حدث خطأ أثناء المسح الضوئي: " & ex.Message)
End Try
End Sub
Private Sub SaveImageToFile(image As ImageFile, filePath As String)
' تحويل الصورة إلى ملف وحفظها
Dim imageData As Byte() = DirectCast(image.FileData.BinaryData, Byte())
File.WriteAllBytes(filePath, imageData)
End Sub
End Class