Hello everyone,
I am currently programming in my sparetime C# and was trying to fiddle with my scanner. I downloaded from the Microsoft Download centre the WIA Automation Kit 2.0 and registered the .DLL and also referenced it to my project.
I then try to type the following code:
CommonDialogClass wiadiag = new CommonDialogClass();
WIA.ImageFile wijnand = null;
wijnand = wiadiag.ShowAcquireImage(WiaDeviceType.UnspecifiedDeviceType, WiaImageIntent.GrayscaleIntent, WiaImageBias.MaximizeQuality,"", true, true, false);
When I start my application and hit the scan button I get to see the Select scanner option, I select the proper scanner and then have the choice between colors and the resolution. I select the settings and hit previes. This works fine and I see in my preview window the Picture I want. Then I hit the scan button and I am presented with the following Error :
Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))
I have tried it with 3 different machines (XP, VIsta) and 2 different scanners yet all of them portay the same error message.
But when I do the following code :
String deviceid = "";
const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
CommonDialogClass class1 = new CommonDialogClass();
WIA.CommonDialog WiaCommonDialog = new CommonDialogClass();
Device d = class1.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true,false);
Device Wiadev = null;
if (d != null)
{
ddeviceid = d.DeviceID;
}
DeviceManager manager = new DeviceManagerClass();
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == deviceid)
{
WIA.Properties infoprop = null;
Wiadev = info.Connect();
break;
}
}
WIA.ImageFile img = null;
WIA.Item item = (WIA.Item)Wiadev.Items[1];
try
{
img = (ImageFile)WiaCommonDialog.ShowTransfer(Wiadev.Items[1],wiaFormatJPEG,false);
Vector vector = img.FileData;
byte[] bin = (byte[])vector.get_BinaryData();
using (MemoryStream mstrm = new MemoryStream(bin))
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromStream(mstrm);
}
}
catch(COMException f)
{
}
It works and the picture gets loaded into the Picturebox albeit with the default settings. Does anyone know why my above code doesnt work? I found a lot of VS2005 projects who used the above method, am I missing something?
Thank you