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, m开发者_运维知识库aximize 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.
See if this helps: Scanning an Image from Silverlight 4 using WIA Automation
It doesn't seem to be straightforward to get an image...
ShowAcquireImage returns ImageFile, it have methods to save content to disk or stream
精彩评论