[.net] C# and the WIA Automation SDK

Started by
4 comments, last by wijnand 15 years, 8 months ago
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
Advertisement
Here's a wild guess but I notice in the first code you don't specify the format. Perhaps you need to do that?

const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";  wijnand = wiadiag.ShowAcquireImage(WiaDeviceType.UnspecifiedDeviceType, WiaImageIntent.GrayscaleIntent, WiaImageBias.MaximizeQuality, wiaFormatJPEG, true, true, false);
I tried giving it a JPEG GUID but sadly that did not seem to be the problem either :( according to the SDK it can be left on blank :S
Looking up the error code says it's "either the component has not been registered on the server or the ProgID passed to the Server.CreateObject method is misspelled"

But you have registered the component with regsvr32?
Yes I have done a manual REGSVR32 just to make sure, even ruled out it was Windows Vista X64 by trying it on a Windows XP machine.

Before I tried running the XP one i ran my application without registering the .DLL and it gave me an error immediatly, when I registered it it showed the WIA things and then crapped out immediatly when I pressed the scan button again.

Maybe I need to do something in my project settings for it to put the .DLL in the assembly or something? And why does my more expanded code work (without the GUI) I am starting to suspect the Epson drivers themselves are at fault.

But maybe I am overlooking something :(
I think I found the problem

to my amusement when i used the string that I gave in my previous application it worked perfectly. Thank you headkaze you are my official hero :D

This topic is closed to new replies.

Advertisement