Twitter <%=Resources.labels.subscribe %>

Silverlight 4 OOB Get Image From Scanner

  • 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:

    Permalink |

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Intro

My name is Brent Lamborn. I´m 35 years old and I live in Nebraska. I've been coding with the .NET Framework since 2003. I'm a former Marine and I have 5 boys.


View Brent Lamborn's LinkedIn profileView Brent Lamborn's profile

Sponsored By

Categories

Tag Cloud

Blog Roll