如何将此扫描程序代码C#代码转换为WPF VB [英] How do I convert this scanner code C# code to the WPF VB

查看:66
本文介绍了如何将此扫描程序代码C#代码转换为WPF VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将这个C#代码转换为WPF vb



i我也使用像converter.telerik.com这样的网站转换它但有些代码是错误的。 />


所以请帮我转换这段代码我是WPF VB和C#的新手。





先谢谢。



我有什么试过:



这是xaml代码。

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.

<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 : =




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);
}
}
}
}

推荐答案

我复制并粘贴了C#上面的代码进入 Telerik Code Converter [ ^ ]并且转换得很好:

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):


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...


I'll leave the rest of the debugging for you...


这篇关于如何将此扫描程序代码C#代码转换为WPF VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆