23

我正在使用 WIA 将图像从扫描仪捕获到 windows 窗体。这是我正在使用的代码:

private void button2_Click(object sender, EventArgs e)
{
    const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
    CommonDialogClass wiaDiag = new CommonDialogClass();
    WIA.ImageFile wiaImage = null;

    wiaImage = wiaDiag.ShowAcquireImage(
            WiaDeviceType.UnspecifiedDeviceType,
            WiaImageIntent.GrayscaleIntent,
            WiaImageBias.MaximizeQuality,
            wiaFormatJPEG, true, true, false);

    WIA.Vector vector = wiaImage.FileData;

    Image i = Image.FromStream(new MemoryStream((byte[])vector.get_BinaryData()));
    i.Save(@"D:\prueba1.jpeg");
}

尝试运行这个小测试时,我收到此错误:

无法嵌入互操作类型“WIA.CommonDialogClass”。请改用适用的接口。

还有这个:

“WIA.CommonDialogClass”不包含“ShowAcquireImage”的定义,并且找不到接受“WIA.CommonDialogClass”类型的第一个参数的扩展方法“ShowAcquireImage”(您是否缺少 using 指令或程序集引用?

我猜第二个错误是因为第一个错误而上升的,对吧?

对于如何解决这个问题,有任何的建议吗?

4

3 回答 3

26

第二个错误是由第一个错误引起的。嵌入互操作类型功能仅支持嵌入接口,不支持类。除了将 WIA 引用上的该选项设置为 False 并部署互操作库之外,您还可以像这样修复它:

 WIA.CommonDialog wiaDiag = new WIA.CommonDialog();

不直观但允许使用new运算符创建 COM 接口。您需要在命名空间名称前加上前缀,因为CommonDialog与 Winforms CommonDialog 类不明确。

于 2010-11-18T21:37:15.123 回答
9

http://digital.ni.com/public.nsf/allkb/4EA929B78B5718238625789D0071F307

发生此错误的原因是新项目中引用的 TestStand API 互操作程序集的 Embed Interop Types 属性的默认值为 true。要解决此错误,请按照以下步骤将 Embed Interop Types 属性的值更改为 False:

Select the TestStand Interop Assembly reference in the references section of your project in the Solution Explorer.
Find the Embed Interop Types property in the Property Browser, and change the value to False

相关链接: 知识库 595FQJPI:我可以将 Visual Studio 2010 与 TestStand 一起使用并调用 .NET Framework 4.0 代码模块吗?

于 2013-06-10T07:48:41.680 回答
4

简单地说,您只需将错误程序集选择到解决方案面板/参考中。然后,按 Alt-Enter(属性),找到“Embed Interop Type”并将其值设置为“False”,如果它是 True Brgs !

于 2014-08-02T16:34:44.803 回答