LEADTOOLS ナレッジベース

WPFアプリケーションでTWAINスキャンするには?

TwainSessionクラスを使用して以下の手順でスキャンできます。

  1. Startupメソッドで初期化を実施
  2. SelectSourceメソッドでTWAINソースを選択
    (この引数が「String.Empty」の場合は選択ダイアログボックスを表示します。TWAINソース名を指定した場合は選択ダイアログボックスを表示させずにTWAINソースが選択されます。)
  3. Acquireメソッドで画像の取得

サンプルコード(VB.NET)

Private twainSession As TwainSession

Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    Try
        twainSession = New TwainSession()

        Dim strFriendlyName As String = AppDomain.CurrentDomain.FriendlyName
        Dim pro As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName(strFriendlyName.Substring(0, strFriendlyName.LastIndexOf("."c)))

        twainSession.Startup(pro(0).MainWindowHandle, "", "", "", "", TwainStartupFlags.None)

        AddHandler twainSession.AcquirePage, AddressOf twainSession_AcquirePage
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

Private Sub twainSession_AcquirePage(ByVal sender As Object, ByVal e As TwainAcquirePageEventArgs)
    RasterImageViewer1.Image = e.Image.Clone()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Try
        twainSession.SelectSource("")
        twainSession.Acquire(TwainUserInterfaceFlags.Show)

    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

サンプルコード(C#)

private TwainSession twainSession;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    try
    {
        twainSession = new TwainSession();

        string strFriendlyName = AppDomain.CurrentDomain.FriendlyName;
        System.Diagnostics.Process[] pro = System.Diagnostics.Process.GetProcessesByName(strFriendlyName.Substring(0, strFriendlyName.LastIndexOf('.')));

        twainSession.Startup(pro[0].MainWindowHandle, "", "", "", "", TwainStartupFlags.None);

        twainSession.AcquirePage += twainSession_AcquirePage;
    }
    catch (Exception ex)
    {
        System.Windows.MessageBox.Show(ex.ToString());
    }
}

private void twainSession_AcquirePage(object sender, TwainAcquirePageEventArgs e)
{
    RasterImageViewer1.Image = e.Image.Clone();
}

private void Button1_Click(object sender, RoutedEventArgs e)
{
    try
    {
        twainSession.SelectSource("");
        twainSession.Acquire(TwainUserInterfaceFlags.Show);
    }
    catch (Exception ex)
    {
        System.Windows.MessageBox.Show(ex.ToString());
    }
}

実際の動作はWPFコントロール チュートリアルの「WpfTwainScan」をご覧ください。

0 コメント

記事コメントは受け付けていません。