我正在尝试为我已经写了一段时间的程序添加TWAIN支持。我正在使用https://github.com/tmyroadctfig/twaindotnet
中的TwainDotNet我已经能够将vb.net示例适应新的测试项目而没有问题。但是,当我在当前项目中将其放入表单时,我收到错误。
代码:
Imports System.Windows.Forms
Imports TwainDotNet
Imports TwainDotNet.TwainNative
Imports TwainDotNet.WinFroms
Public Class frmDebug
Private Property areaSettings As New AreaSettings(Units.Centimeters, 0.1F, 5.7F, 0.1F + 2.6F, 5.7F + 2.6F)
Private Property twain As Twain
Private Property settings As ScanSettings
Private Property images As List(Of System.Drawing.Bitmap)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Twain needs a hook into this Form's message loop to work:
twain = New Twain(New WinFormsWindowMessageHook(Me))
' Add a handler to grab each image as it comes off the scanner
AddHandler twain.TransferImage,
Sub(sender As Object, args As TwainDotNet.TransferImageEventArgs)
If (Not (args.Image Is Nothing)) Then
imgScanner.Image = args.Image
images.Add(args.Image)
'widthLabel.Text = String.Format("Width: {0}", pictureBox1.Image.Width)
'heightLabel.Text = String.Format("Height: {0}", pictureBox1.Image.Height)
End If
End Sub
' Re-enable the form after scanning completes
AddHandler twain.ScanningComplete,
Sub(sender As Object, e As TwainDotNet.ScanningCompleteEventArgs)
Enabled = True
End Sub
End Sub
Private Sub cmdSource_Click(sender As Object, e As EventArgs) Handles cmdSource.Click
' Show the "select scanning source" dialog
twain.SelectSource()
End Sub
End Class
错误:
'Program.vshost.exe' (CLR v4.0.30319: Program.vshost.exe): Loaded 'D:\Programming\Program\Program\bin\x86\Debug\TwainDotNet.dll'. Symbols loaded.
'Program.vshost.exe' (CLR v4.0.30319: Program.vshost.exe): Loaded 'D:\Programming\Program\Program\bin\x86\Debug\TwainDotNet.WinFroms.dll'. Symbols loaded.
'Program.vshost.exe' (CLR v4.0.30319: Program.vshost.exe): Loaded 'D:\Programming\Program\Program\bin\x86\Debug\Program-CustomDlls.dll'. Symbols loaded.
Exception thrown: 'System.TypeInitializationException' in TwainDotNet.dll
它死掉的代码在这里:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using TwainDotNet.TwainNative;
using TwainDotNet.Win32;
using System.Drawing;
namespace TwainDotNet
{
public class Twain
{
DataSourceManager _dataSourceManager;
public Twain(IWindowsMessageHook messageHook)
{
ScanningComplete += delegate { };
TransferImage += delegate { };
_dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook);
_dataSourceManager.ScanningComplete += delegate(object sender, ScanningCompleteEventArgs args)
{
ScanningComplete(this, args);
};
_dataSourceManager.TransferImage += delegate(object sender, TransferImageEventArgs args)
{
TransferImage(this, args);
};
}
/// <summary>
/// Notification that the scanning has completed.
/// </summary>
public event EventHandler<ScanningCompleteEventArgs> ScanningComplete;
public event EventHandler<TransferImageEventArgs> TransferImage;
/// <summary>
/// Starts scanning.
/// </summary>
public void StartScanning(ScanSettings settings)
{
_dataSourceManager.StartScan(settings);
}
/// <summary>
/// Shows a dialog prompting the use to select the source to scan from.
/// </summary>
public void SelectSource()
{
_dataSourceManager.SelectSource();
}
/// <summary>
/// Selects a source based on the product name string.
/// </summary>
/// <param name="sourceName">The source product name.</param>
public void SelectSource(string sourceName)
{
var source = DataSource.GetSource(
sourceName,
_dataSourceManager.ApplicationId,
_dataSourceManager.MessageHook);
_dataSourceManager.SelectSource(source);
}
/// <summary>
/// Gets the product name for the default source.
/// </summary>
public string DefaultSourceName
{
get
{
using (var source = DataSource.GetDefault(_dataSourceManager.ApplicationId, _dataSourceManager.MessageHook))
{
return source.SourceId.ProductName;
}
}
}
/// <summary>
/// Gets a list of source product names.
/// </summary>
public IList<string> SourceNames
{
get
{
var result = new List<string>();
var sources = DataSource.GetAllSources(
_dataSourceManager.ApplicationId,
_dataSourceManager.MessageHook);
foreach (var source in sources)
{
result.Add(source.SourceId.ProductName);
source.Dispose();
}
return result;
}
}
}
}
第21行:_dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId,messageHook);
我不知道它为什么会死,调试信息也很模糊。有什么想法吗?
答案 0 :(得分:0)
问题是由于我正在编译的CPU类型。我拿了TwainDotNet的代码并重新编译为x86。然后我设置我的主程序来编译x86。它现在有效。
我一直在编译AnyCPU。 log4net.dll是为x86编译的,是TwainDotNet所需要的。我也认为吐温是为x86编译的,但我无从谈论。
毋庸置疑,我现在正在为x86架构进行编译。