Yesterday I gave a presentation on some new features in Silverlight 4. During this presentation I mentioned the newly available ability to interoperate with Office applications such as Outlook and Excel though the new ComAutomationFactory that is in Silverlight 4. Someone in the audience asked the question, “Can I access my scanner”, and of course I said yes; then he said, “how?”, and I said, “I will get back to you”. Well here I am, with code!
A couple of things to note; this feature requires your application to run as an Out Of Browser (OOB) with elevated permissions. Also, there is no IntelliSense for your COM objects. So make sure you have the documentation to the API you are trying to use. So lets get started.
Creating an OOB Application
First thing you need to do is crack open VS 2010 Beta 2 and create a new Silverlight project. Make sure you are targeting the .NET Framework 4 it is a Silverlight 4 application.
For this test project we don’t need to host this in a website.
The next thing you need to do is right-click on the project and choose “Properties”. Check the “Enable Running Application out of browser”
Now click the Out of Browser Settings button and set “Require Elevated Trust when running outside the browser.”
Next add a button to the application to install our OOB application.
Now run the application and install it by clicking the install button you just created. When you start the installation, you will be prompted to install the app, and whether or not you want to create some shortcuts. Just say yes, we trust ourselves, sort of.
The next thing we want to do is enable debugging our OOB application. So right click your project and chose properties –> Debug –> Installed out of browser application –> YourApplication
The final step is to add a reference to Microsoft.CSharp.dll so we can use the dynamic keyword. Look for it in C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\. Now on to fun stuff.
Send an Email with Outlook
First create a form that will take your user input for the “To” and “Message” data, and a button to send the message.
Then handle the button click event as follows.
And you done. Of course this will only display the message, but just call mail.Send() to actually send it; one thing to mention is to make sure you have Outlook open when you hit send or bad things will happen.
Send Data to Excel, edit it, and update Silverlight
Now this little trick is cool. We will have a data source, send it to excel for display and editing, then send the updated data back to our Silverlight application and update the UI. Now this is a poor mans implementation for demo reasons.
First lets create our UI and populate it with data. This is what mine looks like.
Lets code up the Launch Excel button.
As you can see we are hooking into the Sheet changed event so we can respond to when the data is update in excel. So here is the code for that.
Now, I will click the Launch Excel button, and edit my data.
Now if I look back in my Silverlight application, I will now see that all of my data in the grid has been updated.
Lets Open a Program
Using the WScript.Shell API we can execute any command and open any program. In this example lets open Notepad and write some text to it. So first I am going to create a simple UI to allow a user to enter some text, then click a button to send it to Notepad.
Now lets enter some text and click that button.
Now that’s pretty cool. I am sure your mind is thinking of all the cool stuff you can do with this.
Well, this stuff is cool and all, but where is the really cool stuff? Wait no more!
Text to Speech
That’s right, I said it. I am going to use the SAPI.SpVoice API to tap into the power of text to speech. So lets build a UI that will allow a user to enter some text. Heck lets let them control the rate and pitch of the speech.
This should look something like this.
Lets hook up our button’s click event and make some noise.
If you wanted to you could create a volume control for it as well.
What? This still isn’t cool enough for you? Well I think I can satisfy your need for coolness. Enter:
Acquire an Image from your Scanner/Camera
That’s right I said it. You can access your scanner, scan an image, and then save it to your hard drive. Here we are using the WIA (Windows Image Acquisition). “WIA is a full-featured image manipulation component that provides end-to-end image processing capabilities. The WIA Automation Layer makes it easy to acquire images on digital cameras, scanners, or Web cameras, and to rotate, scale, and annotate your image files.” So, enough with the talking, lets get coding.
First all I need to do it create a button that will initiate the process.
Now we need to handle the button’s click event and do all the complicated code.
So what does this actually do? Well lets push the button and find out:
Well the first thing it does is asks me which device I want to get my images from. Lets pick my scanner.
Well, holly crap, that’s my scanner. And I can preview and crop my image before I even get the image. Yes, that is my baby picture, it was the only thing I could find OKAY! All my current pictures are digital. Anyways, so once I like my settings I click scan, the scanner will do its thing and my image will be saved.
And there it is right where I told it to save. How fun is that? I did you a favor and already coded all this up and packed it up in a nice little zip file. So download the code, play with it, and write some kick ass applications!
I almost forgot to mention, when you open the source code, go into Properties –> Debug –> and select “Dynamically create the page”, then run the application, install it, and go back and change the option back to “Installed out of browser application ”.