ImageGear for Silverlight
Developing the Application
Send Feedback
ImageGear for Silverlight User Guide > Getting Started > ImageGear for Silverlight C# Tutorial > Developing the Application

Glossary Item Box

  1. Open MainPage.xaml.cs and add the following using statements to the top:
    C# Copy Code
    using System.IO;
    using ImageGear.Core;
    using ImageGear.Display;
    using ImageGear.Formats;
    using ImageGear.Windows.Controls;
  2. 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) 
    {
        loadText.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)
        {
            loadText.Text = "Failed to retrieve license from licensing web service. Exception: " + error.ToString();
        }
        else
        {
            // Initialize format components
            ImGearCameraRawFormats.Initialize();
            ImGearCommonFormats.Initialize();
            ImGearAdvancedFormats.Initialize();
            loadText.Text = "Click to load image file.";
        }
    });
    // 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.

  3. Define a handler that will control what happens when the button is clicked. Add the following code to the constructor for the Page class:
    C# Copy Code
    buttonOpen.Click += new RoutedEventHandler(buttonOpen_Click);
  4. Add the following function to the class. This creates an open file dialog, opens the selected file, and displays it in the PageView Class:
    C# Copy Code
    void buttonOpen_Click(object sender, RoutedEventArgs e)
    {
        // Prompt the user to select an image file
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Image files (*.jpg;*.png;*.tif;*.bmp)|*.jpg;*.png;*.tif;*.bmp|All files (*.*)|*.*";
        ofd.FilterIndex = 1;
        bool? result = ofd.ShowDialog();
        if (!result.GetValueOrDefault(false))
            return;
        // Open a stream to the contents of the file  
        using (Stream f = ofd.File.OpenRead())
        {
            // Open it with ImageGear  
            ImGearPage igPage = ImGearFileFormats.LoadPage(f, 0);
            pageView.Display = new ImGearPresentationPageDisplay(igPage);
            pageView.Update();
        }
    }
  5. The sample is now complete. Right-click SilverlighttutorialTestPage.html in the Solution Explorer and set it as the start up page. Press CTRL+F5 to launch the sample. 
©2013. Accusoft Corporation. All Rights Reserved.