0

I have this little function in my Silverlight 4 OOB app that gets an image from a scanner:

public static BitmapImage GetImageFromScanner()
        {
            try
            {
                using (dynamic CommonDialog = AutomationFactory.CreateObject("WIA.CommonDialog"))
                {
                    //Param meanings: (scanner, black and white, maximize quality)
                    dynamic imageFile = CommonDialog.ShowAcquireImage(1, 2, 131072);
                    if (imageFile != null)
                    {
                        return (BitmapImage)imageFile;
                    }
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {

                if (ex.ErrorCode == -2145320939)
                {
                    MessageBox.Show("Could not find an attached scanner.", "Scanner Error", MessageBoxButton.OK);
                }
                else if (ex.ErrorCode == -2145320957)
                {
                    MessageBox.Show("There is no paper in the scanner.", "Scanner Error", MessageBoxButton.OK);
                }


            }

            return null;
        }

I'd like the function to return a BitmapImage but I'm not sure how to cast the dynamic type. I'm not even sure what type imageFile would be if it weren't dynamic. The above method returns the following exception:

Unable to cast object of type 'System.Runtime.InteropServices.Automation.AutomationMetaObjectProvider' to type 'System.Windows.Media.Imaging.BitmapImage'.

Can someone provide guidance? I'm not sure if this is a question about the dynamic keyword or AutomationFactory since both are new to me. :/

EDIT:

I know it is an image because if I do this:

 string filePath = string.Format("c:\\{0}.jpg", Guid.NewGuid());
                        imageFile.SaveFile(filePath);
                        MessageBox.Show(string.Format("Saved {0}", filePath));

It saves the document scanned as a jpg. I tried to figure out what object in the .NET framework have a SaveFile() method and there are seemingly many.

Brent Lamborn
  • 1,370
  • 3
  • 17
  • 37
  • Why dont you try GetType() on the dynamic object while runnig the debugger. See if that gives you a clue of what object you are getting back and consequently what kind of image if any. – InBetween May 26 '11 at 20:59
  • GetType() tells me it is a 'System.Runtime.InteropServices.Automation.AutomationMetaObjectProvider' – Brent Lamborn May 26 '11 at 21:02

2 Answers2

1

See if this helps: Scanning an Image from Silverlight 4 using WIA Automation

It doesn't seem to be straightforward to get an image...

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • Here is a link to a post that shows the code I ended up using: http://www.brentlamborn.com/post/Silverlight-4-OOB-Get-Image-From-Scanner.aspx – Brent Lamborn May 31 '11 at 15:03
0

ShowAcquireImage returns ImageFile, it have methods to save content to disk or stream

gdbdable
  • 4,445
  • 3
  • 30
  • 46