I copy and pasted the C# code above into
Telerik Code Converter[
^] and it converted fine:
Imports System.Drawing
Imports System.IO
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Interop
Imports Microsoft.Win32
Imports TwainDotNet
Imports TwainDotNet.TwainNative
Imports TwainDotNet.Wpf
Imports TwainDotNet.Win32
Namespace TestAppWpf
Public Partial Class Window1
Inherits Window
Private Shared AreaSettings As New AreaSettings(Units.Centimeters, 0.1F, 5.7F, 0.1F + 2.6F, 5.7F + 2.6F)
Private _twain As Twain
Private _settings As ScanSettings
Private resultImage As Bitmap
Public Sub New()
InitializeComponent()
Loaded += Sub()
_twain = New Twain(New WpfWindowMessageHook(Me))
_twain.TransferImage += Sub(sender As [Object], args As TransferImageEventArgs) If args.Image IsNot Nothing Then
resultImage = args.Image
Dim hbitmap As IntPtr = New Bitmap(args.Image).GetHbitmap()
MainImage.Source = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
Gdi32Native.DeleteObject(hbitmap)
End If
_twain.ScanningComplete += Sub() IsEnabled = True
Dim sourceList = _twain.SourceNames
ManualSource.ItemsSource = sourceList
If sourceList IsNot Nothing AndAlso sourceList.Count > 0 Then
ManualSource.SelectedItem = sourceList(0)
End If
End Sub
End Sub
Private Sub OnSelectSourceButtonClick(sender As Object, e As RoutedEventArgs)
_twain.SelectSource()
End Sub
Private Sub scanButton_Click(sender As Object, e As RoutedEventArgs)
IsEnabled = False
_settings = New ScanSettings() With { _
Key .UseDocumentFeeder = UseAdfCheckBox.IsChecked, _
Key .ShowTwainUI = If(UseUICheckBox.IsChecked, False), _
Key .ShowProgressIndicatorUI = ShowProgressCheckBox.IsChecked, _
Key .UseDuplex = UseDuplexCheckBox.IsChecked, _
Key .Resolution = If((If(BlackAndWhiteCheckBox.IsChecked, False)), ResolutionSettings.Fax, ResolutionSettings.ColourPhotocopier), _
Key .Area = If(Not (If(GrabAreaCheckBox.IsChecked, False)), Nothing, AreaSettings), _
Key .ShouldTransferAllPages = True, _
Key .Rotation = New RotationSettings() With { _
Key .AutomaticRotate = If(AutoRotateCheckBox.IsChecked, False), _
Key .AutomaticBorderDetection = If(AutoDetectBorderCheckBox.IsChecked, False) _
} _
}
Try
If SourceUserSelected.IsChecked = True Then
_twain.SelectSource(ManualSource.SelectedItem.ToString())
End If
_twain.StartScanning(_settings)
Catch ex As TwainException
MessageBox.Show(ex.Message)
End Try
IsEnabled = True
End Sub
Private Sub OnSaveButtonClick(sender As Object, e As RoutedEventArgs)
If resultImage IsNot Nothing Then
Dim saveFileDialog = New SaveFileDialog()
If saveFileDialog.ShowDialog() = True Then
resultImage.Save(saveFileDialog.FileName)
End If
End If
End Sub
End Class
End Namespace
Now, the conversion is not perfect and will require tweaking (aka Debugging). Debugging is a normal programming task.
Looking at the outputted VB code, I can see one area that needs tweaking (not tested):
_settings = New ScanSettings() With { _
.UseDocumentFeeder = UseAdfCheckBox.IsChecked, _
.ShowTwainUI = If(UseUICheckBox.IsChecked, False), _
.ShowProgressIndicatorUI = ShowProgressCheckBox.IsChecked, _
.UseDuplex = UseDuplexCheckBox.IsChecked, _
.Resolution = If((If(BlackAndWhiteCheckBox.IsChecked, False)), ResolutionSettings.Fax, ResolutionSettings.ColourPhotocopier), _
.Area = If(Not (If(GrabAreaCheckBox.IsChecked, False)), Nothing, AreaSettings), _
.ShouldTransferAllPages = True, _
.Rotation = New RotationSettings() With { _
.AutomaticRotate = If(AutoRotateCheckBox.IsChecked, False), _
.AutomaticBorderDetection = If(AutoDetectBorderCheckBox.IsChecked, False) _
} _
}
I'll leave the rest of the debugging for you...