Class cannot be embedded. Use the applicable interface instead

delete picture delete · Nov 18, 2010 · Viewed 36.5k times · Source

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?

Answer

Hans Passant picture Hans Passant · Nov 18, 2010

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.