Silverlight 4 OOB アプリには、スキャナーから画像を取得する次の小さな関数があります。
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;
}
関数が BitmapImage を返すようにしたいのですが、動的型をキャストする方法がわかりません。動的でない場合、 imageFile がどのタイプになるかさえわかりません。上記のメソッドは、次の例外を返します。
Unable to cast object of type 'System.Runtime.InteropServices.Automation.AutomationMetaObjectProvider' to type 'System.Windows.Media.Imaging.BitmapImage'.
誰かがガイダンスを提供できますか?これが dynamic キーワードに関する質問なのか、AutomationFactory に関する質問なのかはわかりません。どちらも私にとっては初めてのことです。:/
編集:
私がこれを行うと、それが画像であることを知っています:
string filePath = string.Format("c:\\{0}.jpg", Guid.NewGuid());
imageFile.SaveFile(filePath);
MessageBox.Show(string.Format("Saved {0}", filePath));
スキャンした文書をjpgとして保存します。.NET フレームワークのどのオブジェクトが SaveFile() メソッドを持っているかを調べようとしましたが、一見たくさんあります。