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

我正在使用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’的定义,也没有接受第一个类型’WIA.CommonDialogClass’的扩展方法’ShowAcquireImage’(你是否缺少using指令或汇编引用?

我猜第二个错误是由于第一个错误而上升,对吧?

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

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

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

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

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代码模块吗?

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