类无法嵌入。请改用适用的界面

时间:2010-11-18 21:09:47

标签: c# wia

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

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

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

  

Interop类型'WIA.CommonDialogClass'   无法嵌入。使用适用的   接口改为。

而且:

  

'WIA.CommonDialogClass'没有   包含的定义   'ShowAcquireImage'并没有扩展名   方法'ShowAcquireImage'接受a   类型的第一个参数   可以找到'WIA.CommonDialogClass'   (你错过了使用指令或   装配参考?

我猜第二个错误正在上升,因为第一个错误,对吗?

有关如何解决此问题的任何建议吗?

3 个答案:

答案 0 :(得分:26)

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

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

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

答案 1 :(得分:9)

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

发生此错误是因为新项目中引用的TestStand API Interop程序集的Embed Interop Types属性的默认值为true。要解决此错误,请按照以下步骤将“嵌入互操作类型”属性的值更改为“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

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

答案 2 :(得分:4)

简单地说,您只需在解决方案面板/参考中选择错误组件。然后,按Alt-Enter(属性),找到“嵌入互操作类型”并将其值设置为“False”(如果为True) 布鲁斯!

相关问题