×

Quick Start for Windows Phone 8.0

Create a new project

To create a new project, follow these steps:

  1. In the Visual Studio, select File > New > Project...
  2. In the New Project window, select Visual C# > Windows Phone > Windows Phone App.
  3. Press OK.
  4. Choose "Windows Phone OS 8.0" when asked. (Please note that the SDK can only be used in Windows Phone 8 projects.)

A project has now been created for you.

Include libraries to your project

Before starting to use the functionality provided by the SDK, the Nokia Imaging SDK libraries must be added to the project. For detailed instructions, please see the chapter Adding libraries to the project.

Define your XAML UI

The UI we will build for this tutorial is very simple. There will be two XAML image controls and two buttons. One XAML image control will display the original image, and the other will display the filtered image. Similarly, one button is used for selecting the image, and the other one is used for saving the filtered image to the phone library.

Here are the steps to accomplish this:

  1. In the Visual Studio's Solution Explorer, open the MainPage.xaml.
  2. Our UI is in the landscape orientation; in the XAML view, search for the SupportedOrientations and shell:SystemTray.IsVisible definitions and replace it with the following:
    SupportedOrientations="Landscape" Orientation="Landscape"
    shell:SystemTray.IsVisible="False"
  3. Let's now add all the controls that make our UI. In the XAML view, search for the LayoutRoot grid that was generated when creating the project:
    <!--ContentPanel - place additional content here-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    
    </Grid>

    Replace the grid and all its content by this grid:

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    
        <Image x:Name="CartoonImage" Width="800" Height="480" Stretch="UniformToFill" Grid.RowSpan="3" />
        <Image x:Name="OriginalImage" Width="167" Height="100" Stretch="UniformToFill" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="12,12,0,0" />
    
        <TextBlock Text="Nokia Imaging SDK" Style="{StaticResource PhoneTextNormalStyle}" />
    
        <Button Content="Pick an image" Click="PickImage_Click" HorizontalAlignment="Left" Grid.Row="2" />
        <Button Content="Save the image" Click="SaveImage_Click" x:Name="SaveButton" HorizontalAlignment="Right" Grid.Row="2" IsEnabled="False" />
    
    </Grid>
    The only noteworthy thing in the code above is that we define the two preview images, CartoonImage and OriginalImage, without specifying any source for the image to display. We will bind the image source later; code examples will also be given in the chapters below.

Pick an image from the camera roll

Next, open the MainPage.xaml.cs in Visual Studio Solution Explorer. For picking the image, we will use the PhotoChooserTask. The photo chooser is part of the Windows Phone 8 SDK.

private void PickImage_Click(object sender, RoutedEventArgs e)
{    
    SaveButton.IsEnabled = false;

    PhotoChooserTask chooser = new PhotoChooserTask();
    chooser.Completed += PickImageCallback;
    chooser.Show();
}

private async void PickImageCallback(object sender, PhotoResult e)
 {
    if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
    { 
        return;
    }
}

When the user has selected the image she wants to use, the PickImageCallback is called, providing us a System.IO.Stream that we can use to access the content of the file. This is where we will start using the Nokia Imaging SDK.

Use FilterEffect to decode an image with an effect

Finally, we are ready to use the Nokia Imaging SDK!

  1. Add the following using directives -- these are typically needed when using the SDK:
    using System.IO;
    using System.Windows.Media.Imaging;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Nokia.Graphics.Imaging;
    using Nokia.InteropServices.WindowsRuntime; 
    
  2. In addition to the above using directives, following directives are used to choose image from the photo library and to save image in media library.
    using Microsoft.Phone.Tasks;
    using Windows.Storage.Streams;
    using Microsoft.Xna.Framework.Media; 
  3. In the MainPage.xaml.cs class, declare the following member variables.
    public partial class MainPage : PhoneApplicationPage
    
    {
    
    	// FilterEffect instance is used to apply different
            // filters to an image.
    	// Here we will apply Cartoon filter to an image.
    	private FilterEffect _cartoonEffect  		 =	null;
    
    	// The following  WriteableBitmap contains 
    	// The filtered and thumbnail image.
    	private WriteableBitmap _cartoonImageBitmap 	 =	null;
    	private WriteableBitmap _thumbnailImageBitmap 	 =	null;
    	...
    	...
    }	
    
    
  4. Next we have to initialize the private members that we have just declared. We will initialize them in the constructor.
    public Mainpage()
    {
    	InitializeComponent();
    
    	// Initialize WriteableBitmaps to render the
            // filtered and original image.
            _cartoonImageBitmap = new WriteableBitmap((int)CartoonImage.Width, (int)CartoonImage.Height);
            _thumbnailImageBitmap = new WriteableBitmap((int)OriginalImage.Width, (int)OriginalImage.Height);
    }
    	 
  5. Once the image is selected we can apply filters on the image. In this tutorial we apply Cartoon filter and display the result in XAML image control (CartoonImage). The complete code of the PickImageCallback becomes:
      private async void PickImageCallback(object sender, PhotoResult e) 
      { 
    	if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
    	{
    		return;
    	}
    
    	try
    	{
    		// Show thumbnail of original image.
    		_thumbnailImageBitmap.SetSource(e.ChosenPhoto);
    		OriginalImage.Source = _thumbnailImageBitmap;
    
    		// Rewind stream to start.                     
    		e.ChosenPhoto.Position = 0;
    						
    		// A cartoon effect is initialized with selected image stream as source.
    		var imageStream = new StreamImageSource(e.ChosenPhoto);
    		_cartoonEffect = new FilterEffect(imageStream);
    
    		// Add the cartoon filter as the only filter for the effect.
    		var cartoonFilter = new CartoonFilter();
    		_cartoonEffect.Filters = new[] { cartoonFilter };
    
    		// Render the image to a WriteableBitmap.
    		var renderer = new WriteableBitmapRenderer(_cartoonEffect, _cartoonImageBitmap);
    		_cartoonImageBitmap = await renderer.RenderAsync();
    
    		// Set the rendered image as source for the cartoon image control.
    		CartoonImage.Source = _cartoonImageBitmap;
    	}
    	catch (Exception exception)
    	{
    		MessageBox.Show(exception.Message);
    		return;
    	}
    
    	SaveButton.IsEnabled = true;
    }

    We first set image stream, from the selected image, to _thumbnailImageBitmap without applying any filters to get the thumbnail of the original image.

    Next line is setting the position of the stream to the beginning. This step is necessary, as the current reading position of the stream will be at the end as a result of the previous step.

    Next we initialize the _cartoonEffect with the image stream (e.ChosenPhoto) of the selected image, the data coming from the JPEG file. Cartoon filter is then added to the filter list of the _cartoonEffect.

    Filtered image is then rendered to the _cartoonImageBitmap using WriteableBitmapRenderer. The rendering to the _cartoonImageBitmap is asynchronous; RenderAsync replaces the image source with the current image in the _cartoonEffect.

    We then enable the SaveButton, so that the filtered image could be saved.

Add capabilities

The application reads data from the Pictures folder, and therefore it needs certain capabilities.

  1. In the Visual Studio's Solution Explorer, go to the Properties folder and open the WMAppManifest.xml in "View Code" mode (this can be done by right-clicking the filename).
  2. Add the capability ID_CAP_MEDIALIB_PHOTO.

    The capabilities then look something like this:

    <Capabilities>
        <Capability Name="ID_CAP_NETWORKING" />
        <Capability Name="ID_CAP_MEDIALIB_AUDIO" />
        <Capability Name="ID_CAP_MEDIALIB_PLAYBACK" />
        <Capability Name="ID_CAP_SENSORS" />
        <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
        <Capability Name="ID_CAP_MEDIALIB_PHOTO" />
    </Capabilities>

Rendering and encoding to full resolution JPEG

Rendering to full resolution output JPEG is simple once we have an instance of FilterEffect. Every pixel of the original image will be processed by the library and then saved as a JPEG. The Media library is used to save the data buffer created by the SDK as a JPEG file.

This code example demonstrates how to add the functionality to save the filtered image as a full resolution JPEG:

private async void SaveImage_Click(object sender, RoutedEventArgs e)
{
	SaveButton.IsEnabled = false;

	if (_cartoonEffect == null) {
		return;
	}

	var jpegRenderer = new JpegRenderer(_cartoonEffect);
	
	// Jpeg renderer gives the raw buffer for the filtered image.
	IBuffer jpegOutput = await jpegRenderer.RenderAsync();

	// Save the image as a jpeg to the saved pictures album.
	MediaLibrary library = new MediaLibrary();
	string fileName = string.Format("CartoonImage_{0:G}", DateTime.Now);
	var picture = library.SavePicture(fileName, jpegOutput.AsStream());

	MessageBox.Show("Image saved!");

	SaveButton.IsEnabled = true;
}

Running the application

To run the application:

  1. Select either "Device" or "Emulator", "Debug" with the selectors in the Visual Studio's standard toolbar.
  2. Build the application.
  3. Connect a device (if you are deploying it on device) and run.
  4. Select an image using "Pick an image" button.
  5. You should see the main page containing the decoded image with a Cartoon style filter effect added.
Tip:

If you are testing an app on an emulator, you may experience an emulator bug. If your app can't see any content in the pictures library, it is likely a result of a bug in the emulator.

If you need to access the in-built images, you need to open the photos app at least once before you begin testing your application. Once you browse through the photos, the content will become available to your app.

Get the complete source

The complete code of the tutorial:

Quick Start for Windows Phone 8.0 project v1.2 quick-start_wp_v1_2.zip


Last updated 18 June 2014

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×