Integrating Google Analytics in WP/WinRT

How we had wished there was a Google Analytics SDK for windows phone and windows 8! The ease of a Google dashboard, the amount of info that it gives, the integration,  is way superior as compared to the other analytics tools available in the market.

and here is a GoogleAnalyticsSDK available in Codeplex. Works perfect and well documented! (An unusual combination :P)

Integration is a piece of cake!

Google Analytics SDK

May the code be with you! 😀

How to add multiple versions of the assembly without causing conflicts! -WP

An unusual sticky situation it is!

You have a external class, say Class Saviour , in your project. Class Saviour  uses another class, Class MostSoughtAfter from codeplex. Class Saviour has implemented Class MostSoughtAfter to suit its needs. So where ever you create an instance of Class MostSoughtAfter,  it will refer the Class MostSoughtAfter defined by Class Saviour . Suppose you need to use a function in the original Class MostSoughtAfter, and you add the dll . Visual Studio will throw a warning saying there is an ambiguity and that Class MostSoughtAfter implemented in Class Saviour is used to instantiate.

Bingo! You have multiple versions of the same class in your project and you need to reference them separately. I found a similar question in StackOverflow

Now in C# we have an “extern alias” keyword which will allow us to do the same. MSDN says

By using an external assembly alias, the namespaces from each assembly can be wrapped inside root-level namespaces named by the alias, which enables them to be used in the same file.

  1. In the solution explorer of your project, click the properties of the dll you want to reference,
  2. The Aliases field is set to ‘global’ by default. Change the global to name of your choice, say MyDLL.
  3. In the class you want to use the dll, first declare the alias right above the using statements
    extern alias MyDLL;
  4. To use a method in the assembly we make use of :: qualifier
    MyDLL:: System.Windows.Media.Imaging.MyClass.Resize();
  5. Thus a simple creation of an instance of MyClass will use the class implemented in the project. And if you want to use the original dll reference using the alias .

‘extern’  and an MSDN blog saves my day!!


Happy coding !! 😀

Saving a Canvas into a PNG file – WP8

This is a familiar situation that most developers run into. The need to save a canvas element to a png file.


  • You have few strokes drawn on a canvas and you want to save it as a png .
  • You want to save the just the strokes on a transparent background.
  • You want to save the strokes along with the canvas background.
  • You want to save it with your specified sizes

I too ran into this ditch lately. And had a tough time finding a solution. A random google search will give you numerous methods, most of which is applicable only to traditional C#/.NET application.

Then I found this MSDN link. I still have’nt figured out how to use a PNGEncoder and DeCoder. If anyone finds a good blog about it, feel free to post it here. Many of the solutions I found never worked. The searches how ever led me to the WriteableBitmapEx hosted at codeplex. It had several useful functions but none to save a canvas to an image.

A week’s search, frustration, irritation,  trial & error of several broken classes finally led me to ToolStack. It is mentioned that it works for Windows Phone 7.1 but I have implemented in WP8 project. There 3 classes which will do the job neatly, although compression is not yet implemented.

  1. ToolStackPNGWriterWBext.cs – Implementation of WriteableBitmapExtensions
  2. ToolStackPNGWriterLib.cs – Core Class and implementation of saving to PNG
  3. ToolStackCRCLib.cs – Additional classes needed for ToolStackPNGWriterLib.

Getting it working on your project.

  1.  Download all necessary files from ToolStack. Download the example too and run it first so that you can understand how it works.
  2.  Add the Classes mentioned to your project.
  3. Create a Writeablebitmap of your canvas and then use the WritePNG function to save
    WriteableBitmap wb = new WriteableBitmap(MyCanvas, null);

And there your image lies in the specified location, in the size of your canvas! 🙂  Remember to add the necessary using statements in your file. Any canvas element can be captured into a WritableBitmap, even an InkPresenter can be captured.


If you try to implement the same functionality in a metro app, you’re in for trouble mate. Because the very simple function which allows to capture a canvas onto a WriteableBitmap does not exist! There is a method to save the inkstrokes though, but only the portion containing the stroke will be saved. You cannot save it along with its transparent/colored background. I consulted few developers and they seem to have no answer too. It seems this is possible in JavaScript.

Lets see if Windows 8.1 contains something for us developers in this direction.

Happy Coding!! 😀