- Open MainPage.xaml.cs and add the following using statements to the top:
C# Copy Code
using ImageGear.Core; using ImageGear.Display; using ImageGear.Formats; using ImageGear.Windows.Controls; using ImageGear.TWAIN;
- Next we need to add initialization code for ImageGear licensing and components. Add the following code to the constructor for the Page class after InitializeComponent:
C# Copy Code
if (App.Current.Host.Source.DnsSafeHost == string.Empty) { statusText.Text = "You must run your Silverlight application from the http protocol."; return; } // Build the service URL using the current host name and set it. string webServiceUrl = string.Format("http://{0}/{1}", App.Current.Host.Source.DnsSafeHost, "ImageGearSilverlight20_WebService/SilverlightWebService.svc"); ImGearLicense.SetService(webServiceUrl); // Set delegate to handle results of license request. ImGearLicense.LicenseRequest = new ImGearLicense.DelegateLicenseRequest(delegate(Exception error) { if (error != null) { statusText.Text = "Failed to retrieve license from licensing web service. Exception: " + error.ToString(); } else { // Initialize format components ImGearCommonFormats.Initialize(); UpdateUI(); } }); // Set solution name, triggers request from licensing service for license. ImGearLicense.SetSolutionName("AccuSoft 5-44-20");
- If you are evaluating ImageGear for Silverlight, you have to set the application's RootVisual to the application's main Page element and initialize the evaluation manager prior to the other licensing calls. Please see the Evaluation License section for more details.
- Add the following code to the constructor for handling the click of the button:
C# Copy Code
buttonScan.Click += new RoutedEventHandler(buttonScan_Click);
- Add the following method to the class for updating UI text:
C# Copy Code
void UpdateUI() { if (!System.Runtime.InteropServices.Automation.AutomationFactory.IsAvailable) { statusText.Text = "Right click to install the application onto this computer."; buttonScan.Content = "Install OOB"; } else if (!ImGearTWAIN.IsAvailable) { statusText.Text = "Click 'Install' to install TWAIN onto this computer."; buttonScan.Content = "Install TWAIN"; } else { statusText.Text = "Click 'Scan' to acquire an image."; buttonScan.Content = "Scan"; } }
- Add the following method to the class. This verifies that the app is running out of browser and then that the required TWAIN technology is installed on the system. If both verification tests are passed, this opens the TWAIN Data Source Manager's UI, and then acquires one image to a page, and displays the image in the PageView Class:
C# Copy Code
void buttonScan_Click(object sender, RoutedEventArgs e) { // Verify if TWAIN technology is available in the current environment if (!ImGearTWAIN.IsAvailable) { // Check whether the COM Automation feature in Silverlight // is available to the application. This will check if the // app is running out of browser and with elevated permissions. if (!System.Runtime.InteropServices.Automation.AutomationFactory.IsAvailable) { MessageBox.Show( String.Format("The application must be running out of browser in order to acquire images through TWAIN.{0}{0}To install the application to run out of browser, right click anywhere in the application and select \"Install SilverlightTwainTutorial Application onto this computer\"", Environment.NewLine), "The application must be running out of browser.", MessageBoxButton.OK); } // App is running OOB with elevated permissions. // The TWAIN technology is not installed // on this machine. else { // See the "Using the ImageGear for Silverlight TWAIN Installer" help topic to discover options // for installing required TWAIN components at runtime. MessageBox.Show( String.Format("The required TWAIN components are not available on the client computer."), "TWAIN Components Required", MessageBoxButton.OK); } return; } // The TWAIN technology is available in the current environment. else { using (ImGearTWAIN twain = new ImGearTWAIN()) { try { // Create a TWAIN identity for the application ImGearApplicationIdentity identity = new ImGearApplicationIdentity(); identity.ProductName = "Silverlight Tutorial"; // indicate that we want to use the User Interface // of the TWAIN Data Source Manager and of the // TWAIN Data Source. twain.UseUI = true; // Open a connection with a Data Source. Passing in an // empty string for the sourceName will open the Data // Source Manager's UI, allowing the user to select the // source. twain.OpenSource("", identity); // Acquire an image to an ImGearPage. ImGearPage igPage = twain.AcquireToPage(); pageView.Display = new ImGearPresentationPageDisplay(igPage); pageView.Update(); } catch (ImGearException igex) { MessageBox.Show(igex.Message, "An exception occurred during scanning.", MessageBoxButton.OK); } } } }
- The sample is now complete. Right-click SilverlightTwainTutorialTestPage.html in the Solution Explorer and set it as the start up page. Set parent project as start up project, then press F5 to launch the sample.