Click here to Skip to main content
16,016,773 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to convert this C# code to WPF vb

i am also used the website like converter.telerik.com its converts but some code is wrong.

so please Help me to convert this code i am new in WPF VB and C# also.


Thanks in Advance .

What I have tried:

Here is the xaml code.
HTML
<Window x:Class="TestAppWpf.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestAppWpf" Height="450" Width="500">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <StackPanel>
      <GroupBox Header="Source">
        <StackPanel>
          <RadioButton 
            Name="SourceFromTwainUI" 
            IsChecked="True">
            Use TWAIN UI
          </RadioButton>
          <Button 
            Margin="5"             
            IsEnabled="{Binding ElementName=SourceFromTwainUI, Path=IsChecked}"                         
            Click="OnSelectSourceButtonClick">
            Select Source
          </Button>
          <RadioButton Name="SourceUserSelected">Manual source</RadioButton>
          <ComboBox Name="ManualSource" IsEnabled="{Binding ElementName=SourceUserSelected, Path=IsChecked}" />
        </StackPanel>
      </GroupBox>
 
      <Button Margin="5" Name="ScanButton" Click="scanButton_Click">Scan</Button>
      
      <Separator />
 
      <CheckBox Margin="5" Name="UseAdfCheckBox">Use ADF</CheckBox>
      <CheckBox Margin="5" Name="UseDuplexCheckBox">Use Duplex</CheckBox>
 
      <Separator />
 
      <CheckBox Margin="5" Name="UseUICheckBox">Use UI</CheckBox>
      <CheckBox Margin="5" Name="ShowProgressCheckBox">Show Progress</CheckBox>
 
      <Separator />
 
      <CheckBox Margin="5" Name="BlackAndWhiteCheckBox">B & W</CheckBox>
      <CheckBox Margin="5" Name="GrabAreaCheckBox">Grab Area</CheckBox>
 
      <Separator />
 
      <CheckBox Margin="5" Name="AutoDetectBorderCheckBox">Auto Detect Border</CheckBox>
      <CheckBox Margin="5" Name="AutoRotateCheckBox">Auto Rotate</CheckBox>
 
      <Separator />
 
      <Button Margin="5" Click="OnSaveButtonClick">Save</Button>
    </StackPanel>
    <Border Grid.Column="1" BorderThickness="3, 0, 0, 0" BorderBrush="Gray">
      <Image Margin="5" Name="MainImage" Stretch="UniformToFill" />
    </Border>
  </Grid>
</Window>

and code behind C# code is : =

C#
using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Interop;
using Microsoft.Win32;
using TwainDotNet;
using TwainDotNet.TwainNative;
using TwainDotNet.Wpf;
using TwainDotNet.Win32;

namespace TestAppWpf
{
public partial class Window1 : Window
{
private static AreaSettings AreaSettings = new AreaSettings(Units.Centimeters, 0.1f, 5.7f, 0.1F + 2.6f, 5.7f + 2.6f);

private Twain _twain;
private ScanSettings _settings;

private Bitmap resultImage;

public Window1()
{
InitializeComponent();

Loaded += delegate
{
_twain = new Twain(new WpfWindowMessageHook(this));
_twain.TransferImage += delegate(Object sender, TransferImageEventArgs args)
{
if (args.Image != null)
{
resultImage = args.Image;
IntPtr hbitmap = new Bitmap(args.Image).GetHbitmap();
MainImage.Source = Imaging.CreateBitmapSourceFromHBitmap(
hbitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
Gdi32Native.DeleteObject(hbitmap);
}
};
_twain.ScanningComplete += delegate
{
IsEnabled = true;
};

var sourceList = _twain.SourceNames;
ManualSource.ItemsSource = sourceList;

if (sourceList != null && sourceList.Count > 0)
ManualSource.SelectedItem = sourceList[0];
};
}

private void OnSelectSourceButtonClick(object sender, RoutedEventArgs e)
{
_twain.SelectSource();
}

private void scanButton_Click(object sender, RoutedEventArgs e)
{
IsEnabled = false;

_settings = new ScanSettings
{
UseDocumentFeeder = UseAdfCheckBox.IsChecked,
ShowTwainUI = UseUICheckBox.IsChecked ?? false,
ShowProgressIndicatorUI = ShowProgressCheckBox.IsChecked,
UseDuplex = UseDuplexCheckBox.IsChecked,
Resolution = (BlackAndWhiteCheckBox.IsChecked ?? false)
? ResolutionSettings.Fax
: ResolutionSettings.ColourPhotocopier,
Area = !(GrabAreaCheckBox.IsChecked ?? false) ? null : AreaSettings,
ShouldTransferAllPages = true,
Rotation = new RotationSettings
{
AutomaticRotate = AutoRotateCheckBox.IsChecked ?? false,
AutomaticBorderDetection = AutoDetectBorderCheckBox.IsChecked ?? false
}
};

try
{
if (SourceUserSelected.IsChecked == true)
_twain.SelectSource(ManualSource.SelectedItem.ToString());
_twain.StartScanning(_settings);
}
catch (TwainException ex)
{
MessageBox.Show(ex.Message);
}

IsEnabled = true;
}

private void OnSaveButtonClick(object sender, RoutedEventArgs e)
{
if (resultImage != null)
{
var saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == true)
resultImage.Save(saveFileDialog.FileName);
}
}
}
}
Posted
Updated 2-Nov-17 21:45pm
v2
Comments
Graeme_Grant 3-Nov-17 2:03am    
Please reformat your code correctly.

1 solution

I copy and pasted the C# code above into Telerik Code Converter[^] and it converted fine:
VB
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):
VB
_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...
 
Share this answer
 
Comments
ketan Ram Patil 3-Nov-17 4:43am    
thanks for your valuable replay.. :)

Now the problem is i dont know how to convert this C# code


public Window1()
{
InitializeComponent();

Loaded += delegate
{
_twain = new Twain(new WpfWindowMessageHook(this));
_twain.TransferImage += delegate(Object sender, TransferImageEventArgs args)
{
if (args.Image != null)
{
resultImage = args.Image;
IntPtr hbitmap = new Bitmap(args.Image).GetHbitmap();
MainImage.Source = Imaging.CreateBitmapSourceFromHBitmap(
hbitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
Gdi32Native.DeleteObject(hbitmap);
}
};
_twain.ScanningComplete += delegate
{
IsEnabled = true;
};

var sourceList = _twain.SourceNames;
ManualSource.ItemsSource = sourceList;

if (sourceList != null && sourceList.Count > 0)
ManualSource.SelectedItem = sourceList[0];
};
}
to VB
ketan Ram Patil 4-Nov-17 1:50am    
Please Help i tried so many times... but i cant convert this c# code to vb :(
ketan Ram Patil 8-Nov-17 0:24am    
Thank you For your Valuable Replay.. It works Perfectly.
:)
after debugging i got the Error and also solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900