With Windows Phone 7 (WP7) all set to hit the retail markets, so are the various application building up in the Windows Marketplace. However the developers creating the applications for WP7 are bound to come across one problem while working with graphics for the application – GIFs.
The applications being built for Windows Phone 7 is based on Silverlight as the delivery platform and like it or not Silverlight doesn’t support .GIF image formats. Well some may say so what? we can always use JPEG or PNG. Well if you are in a situation where you can alternate with JPEG or PNG then your really wont have a roadblock, but if you are having a GIF image that changes it’s graphic state based on data coming from the application into the graphic. For example a weather application where the nature of the graphic needs to change depending on the forecast of the day (sunny would have only the sun in the graphic, partly cloudy would have a cloud and sun in the graphic and so on).
In such cases if your desired base graphic is in a .gif format then your application will not render the graphics as desired.
There are couple of ways to work around this problem. One is by converting the .gif image into another format directly inside the application. So basically when you need to display that image you write a code to change the format. The only drawback with this approach is that it consumes a lot of hardware resources and in a device like the phone every bit of resource conserved is useful.
The other option is to use a web service to take care of the this dynamic conversion of the gif images. This can be done by creating a WCF service application. Here’s how to do it.
- First off, create a WCF service application from Visual Studio
- Next you need to add functionality to the existing classes (by modifying the service interface) that appears a result of creating WCF application from Visual Studio.
- The above piece of code will get the byte array for the downloaded GIF image and convert it to JPEG. It does this by accepting a byte array as a parameter and returning a byte array.
- Some additional modifications are need to make sure that the helper method takes advantage of LINQ rather than having a separate foreach loop. After the modifications the service interface resembles the code below:
Service1 : IConverter
Bitmap bitmap = (Bitmap)Bitmap.FromStream(
MemoryStream stream =
ImageCodecInfo info = GetCodecInfo(
EncoderParameters parameters =
bitmap.Save(stream, info, parameters);
ImageCodecInfo codecInfo = (from c
ImageCodecInfo.GetImageEncoders() where c.MimeType == MIME select c).First();
- Now you can “Run” the service to launch the WCF Test Client (Press F5 in the main service code file).
- You will notice that the service has been registered and also has a dedicated service URL. Now add a service reference to the WP7 application. Here’s how:
- In Solution Explorer >> Right click References >> Select Add Service Reference.
- In the Add Service Reference dialog box you need to add the service URL to the service list and once it is accessible the service will be listed as one of the available reference points:
- Launch the service and create an instance of the current service class in the WP7 application.
ServiceReference1.ConverterClient client =
- Now you need to add a reference to the ConvertGifToJpegCompleted event handler because the method will be invoked asynchronously:
- By doing this what you can do is to read the content while the main thread is responsive.
sender, ServiceReference1.ConvertGifToJpegCompletedEventArgs e)
(MemoryStream stream =
BitmapImage image =
- Finally you need to call ConvertGifToJpegAsync and it will trigger the event handler:
- In the above line of code, imageContents is the byte array for the GIF image. The result is a clean conversion from GIF to JPEG.