C#

.NET C# Wrapper for the ImageShack XML API

Posted in C# on June 14th, 2009 by Bryce Thomas – 36 Comments

Quick Downloads:
Image Shack API Wrapper Library
ImageShack Wrapper and Demo App Source
Compiled ImageShack Demo App

So after recently starting up a local online classifieds forum I was looking for a cheap and effective way to host pictures of the stuff users are selling. I thought using an external image host like ImageShack was a good idea, but wanted users to be able to upload their images without leaving the classifieds site. Unfortunately, I wasn’t able to find any ready made plugins for the VBulletin forum software that could do this. The best I could find was one which let a user upload an image to ImageShack but took the user to a new browser tab from which they’d manually have to copy and paste the thumbnail link back into their forum post. Sure – not that much effort, but if the link address was returned through the ImageShack XML API instead and automatically placed into the user’s post it’d make their experience a little nicer.

Anyway, my knowledge of PHP approaches zero, so instead of attempting to code up the PHP VBulletin plugin straight off the mark, I thought I’d first try and get the whole image submission/return of XML thing worked out in something I was familiar with – C# and .NET. Which brings me to what this post is really all about. I’ve created a reusable .NET code library wrapper around the ImageShack XML API that allows you to upload images to ImageShack with a simple method call and have the uploaded image details returned. The method is overloaded to give the option of resizing the image on upload if desired. I’ve also written a small demonstration Windows Forms application that uses the code library to upload an image to ImageShack and display the details of the hosted image.

Here’s what the code library’s IImageShackUploader interface looks like:

using System;
using System.Collections.Generic;
using System.Text;

namespace Brycestrosoft.ImageShackAPIWrapper
{
    public interface IImageShackUploader
    {
        UploadedImageDetails UploadImage(string fileName);

        UploadedImageDetails UploadImage(string fileName, int resizeWidth, int resizeLength);
    }
}

And here’s what the returned UploadedImageDetails data object looks like (the private field’s respective public properties have been omitted for brevity):

using System;
using System.Collections.Generic;
using System.Text;

namespace Brycestrosoft.ImageShackAPIWrapper
{
    public class UploadedImageDetails
    {
        private string _imageLink;
        private string _thumbLink;
        private string _adLink;
        private bool _thumbExists;
        private int _totalRaters;
        private double _averageRating;
        private string _imageLocation;
        private string _thumbLocation;
        private string _server;
        private string _imageName;
        private string _donePage;
        private string _resolution;
        private int _fileSize;
        private string _imageClass;
    }
}

And here’s what the example Windows Forms application looks like:

imageshackapiwrapperdemoapp

The class which actually does the work of sending the data off to ImageShack and returning the image details is the StandardImageShackUploader which implements the IImageShackUploader interface. To keep the length of this post down I haven’t shown the StandardImageShackUploader class code here, but it is available in the download.

I thought that while writing the code to send off the information to ImageShack and accept the returned XML data it would be useful to be able to see exactly what was being sent and received on the wire. So I downloaded a copy of Fiddler which gave me a pretty good idea of what was being sent out in my requests and what I was receiving back in ImageShack’s responses. This proved to be indispensable when troubleshooting and for anyone thinking of modifying the request/response code I’d highly recommend it. Here’s a quick screen shot of what’s getting sent out across the wire and what’s getting sent back:

fiddlerimageshack1

Some things I noted about the information that ImageShack returns:

  • thumb_link is only a valid link if thumb_exists is true. If the image you upload is smaller than the size of ImageShack thumbnails, then it looks as though ImageShack doesn’t actually create a thumbnail, but they return you an imaginary thumbnail link regardless.
  • ImageShack returns a total_raters and ave_rating value, even though you’ve only just uploaded the image and it’s not going to have any ratings. I’m guessing this is ImageShack provisioning for other image querying features in their XML API in future.
  • If you opt to resize your image, the resolution value that ImageShack returns may not match the dimensions you specified if your dimensions don’t scale the image up/down proportionately. In other words, it appears that they’ll let you resize an image, providing that you keep its width and height in the same proportion as the original image. If you don’t keep it in proportion, they’ll force the resize to be in proportion and one of the dimensions may not come out the way you’d expected. Either way, I’ve provisioned for both the resize height and width to be sent off to ImageShack because this is what their API takes and in future they might even change it so that images don’t have to be resized proportionately.
  • Generally speaking, there’s probably a fair few details returned by the ImageShack XML API that there is no real use for at this point. Things like image_location, thumb_location and server probably aren’t particularly useful pieces of information, especially considering that it appears as though this information could be derived with some string manipulation on some of the other data that ImageShack sends back. However, for the sake of completeness, I’ve made all of the information returned by the ImageShack API available through the wrapper so that if someone does have a use for it then it’s there.

Props go out to the msdn thread here which I borrowed some code/concepts from.