Here is a simple method you can use to retrieve a JPEG image from a scanner in your Silverlight 4 Out-Of-Browser application:
public static WriteableBitmap GetImageFromScanner()
{
try
{
using (dynamic CommonDialog = AutomationFactory.CreateObject("WIA.CommonDialog"))
{
const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
dynamic imageFile = CommonDialog.ShowAcquireImage(1, 2, 131072, wiaFormatJPEG, false, false, false);
if (imageFile != null)
{
if (imageFile.FormatID != wiaFormatJPEG)
{
dynamic ip = AutomationFactory.CreateObject("WIA.ImageProcess");
ip.Filters.Add(ip.FilterInfos("Convert").FilterID);
ip.Filters(1).Properties("FormatID").Value = wiaFormatJPEG;
ip.Filters(1).Properties("Quality").Value = 20;
imageFile = ip.Apply(imageFile);
}
BitmapImage bitmapBase = new BitmapImage();
dynamic fileData = imageFile.FileData;
byte[] imageData = fileData.BinaryData;
MemoryStream ms = new MemoryStream(imageData);
bitmapBase.SetSource(ms);
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapBase);
return writeableBitmap;
}
}
}
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;
}
Tags: silverlight
Permalink |