首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TwainDotNet System.TypeInitializationException错误

TwainDotNet System.TypeInitializationException错误
EN

Stack Overflow用户
提问于 2016-03-17 03:58:02
回答 1查看 763关注 0票数 0

我正在尝试将TWAIN支持添加到我已经编写了一段时间的程序中。我正在使用来自https://github.com/tmyroadctfig/twaindotnet的TwainDotNet

我已经能够毫无问题地将vb.net示例应用到新的测试项目中。但是,当我将它放入当前项目的表单中时,我得到了一个错误。

代码:

代码语言:javascript
复制
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

错误:

代码语言:javascript
复制
'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

它终止的代码如下:

代码语言:javascript
复制
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 =新DataSourceManager(DataSourceManager.DefaultApplicationId,messageHook);

我不知道它为什么会死,调试消息也很模糊。你有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-03-18 01:34:12

问题是由于我正在为哪种CPU类型编译。我为x86重新编译了TwainDotNet的代码。然后,我将我的主程序设置为为x86编译。它现在起作用了。

我一直在为AnyCPU编译。log4net.dll是为x86编译的,TwainDotNet需要它。我也认为Twain是为x86编写的,但我不知道。

不用说,我现在正在为x86架构进行编译。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36045758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档