Class cannot be embedded. Use the applicable interface instead
Asked Answered
V

3

23

I'm using WIA to capture an image fron the scanner to the windows form. Here is the code I'm using:

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

When trying to run this little test, I get this error:

Interop type 'WIA.CommonDialogClass' cannot be embedded. Use the applicable interface instead.

And this:

'WIA.CommonDialogClass' does not contain a definition for 'ShowAcquireImage' and no extension method 'ShowAcquireImage' accepting a first argument of type 'WIA.CommonDialogClass' could be found (are you missing a using directive or an assembly reference?

I'm guessing the second error is being risen because of the first error, right?

Any suggestions on how to fix this?

Vivl answered 18/11, 2010 at 21:9 Comment(1)
I've found that CommonDialogClass works in .NET 3.5 and the problem you're experiencing was introduced in later versions.Proscribe
C
26

The 2nd error is caused by the first one. The Embed Interop Types feature only supports embedding interfaces, not classes. Other than just setting that option on the WIA reference to False and deploy the interop library, you could also fix it like this:

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

Unintuitive but creating COM interfaces with the new operator is allowed. You need to prefix the namespace name because CommonDialog is ambiguous with the Winforms CommonDialog class.

Catt answered 18/11, 2010 at 21:37 Comment(1)
Unreal! It almost works like a dynamic class, in that you don't get intellisense for any of the operations but it actually performs as expected. Thanks friend!Coycoyle
T
9

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

This error occurs because the default value is true for the Embed Interop Types property of the TestStand API Interop assembly referenced in the new project. To resolve this error, change the value of the Embed Interop Types property to False by following these steps:

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

Related Links: KnowledgeBase 595FQJPI: Can I Use Visual Studio 2010 with TestStand and Call .NET Framework 4.0 Code Modules?

Turoff answered 10/6, 2013 at 7:48 Comment(0)
S
5

Simply, you just choose the error assembly into Solution Panel/References. Then, press Alt-Enter (Properties), find "Embed Interop Type" and set its value to "False" if it True Brgs !

Schriever answered 2/8, 2014 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.