C# 类不能被嵌入。改用适用的接口

C# 类不能被嵌入。改用适用的接口,c#,wia,C#,Wia,我正在使用WIA从扫描仪到windows窗体捕获图像。以下是我正在使用的代码: private void button2_Click(object sender, EventArgs e) { const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"; CommonDialogClass wiaDiag = new CommonDialogClass(); WIA.ImageFile wia

我正在使用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”和无扩展名 方法“ShowAcquireImage”接受 类型的第一个参数 找不到“WIA.CommonDialogClass” (您是否缺少using指令或 程序集引用

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


有关如何修复此问题的任何建议?

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

 WIA.CommonDialog wiaDiag = new WIA.CommonDialog();
不直观,但允许使用新运算符创建COM接口。您需要在名称空间名称前加前缀,因为CommonDialog与Winforms CommonDialog类不明确。

发生此错误是因为新项目中引用的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
相关链接:
KnowledgeBase 595FQJPI:我可以将Visual Studio 2010与TestStand一起使用并调用.NET Framework 4.0代码模块吗?

简单地说,您只需在解决方案面板/引用中选择错误程序集。然后,按Alt-Enter(属性),查找“嵌入互操作类型”,如果为True,则将其值设置为“False”
Brgs!

Unreal!它几乎像一个
dynamic
类一样工作,因为任何操作都无法获得intellisense,但它实际上按预期执行。感谢朋友!我发现CommonDialogClass在.NET 3.5中工作,您遇到的问题在以后的版本中引入。