.NET C# Wrapper for the ImageShack XML API

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.

  1. volkan says:

    thank you very much. I have some difficulties about the c# code. have any vb.net samples?
    Thank you.

  2. Bryce Thomas says:

    Hi volkan,

    My knowledge of vb.net is pretty dismal, so I can’t translate it directly for you. A quick Google search for “C# to VB.NET Converter” turned up this page though, which looks like it’s able to convert C# code to VB code. I’ve never used it before though, so I can’t vouch for how good of a job it does…

  3. DavMay says:

    Thx for sharing this nice piece of software!

    For image hosting, i use mainly http://imagehosting.gr/ which work quite the same as imageShack.
    How could i adapt your source code to make it working with imagehosting.gr ?

    Thx for any help.

  4. Bryce Thomas says:

    Hi DavMay,

    I checked out imagehosting.gr – doesn’t look like they provide any sort of public API like ImageShack do to their image hosting service, so you’re a bit out of luck. No doubt it would still be possible, but given that their service isn’t really set up with this kind of usage in mind it would likely be a lot more difficult and messy.

    You might be able to use Fiddler to analyse what gets sent out across the wire when you upload an image through their website and then try and modify the C# code to send out something similar. Apart from being more difficult without a public API though, any code you write would also be more likely to break if they changed their site too much.

  5. Thanks for sharing, I managed to implement this using Visual Foxpro and a libcurl wrapper class.

  6. Bryce Thomas says:

    Hi Carlos,

    Good stuff. I’ve never done any work with Visual Foxpro, but hope the C# example here was of some assistance.

  7. Of course it was! I used Fiddler until I got the exact same output as your demo app. Just one thing, there is no need to check the image file extension and then set the content type, just use ‘Content-Type: application/octet-stream’ for all image types.

  8. Bryce Thomas says:

    Carlos,

    Thanks again for sharing!

  9. Yann ALET says:

    Nice work, I’m going to use your lib for one of my magnets ;-)

    Thanks a lot !

  10. Bryce Thomas says:

    Hey Yann,

    Sweet, great to hear you’re using the library!

  11. mcnamara says:

    Hello,

    First of all thank you for the library.

    I have one question about it. Imageshack allows to log in with your username and password but it seems that this library does not support it. Any plans to implement it?

    Thank you.

  12. Bryce Thomas says:

    Hi mcnamara,

    I don’t intend on adding support for username and password unfortunately. At least not at this point in time. If it becomes a popular request I’ll consider it, but until then I’d say your best bet would be to try and modify the source code to fit your purposes.

    It’s been a while since I’ve looked at the ImageShack API in detail, so it’s hard for me to gauge whether or not this is likely to be a difficult feature to add. If you’re able to come up with something that works with a user’s login and password though, please do share!

  13. mario says:

    Hi Thomas

    I’m trying to make a small command line application in groovy to auto-upload my galleries to imageshack, and I have some problem with upload, because every time I get the message:

    HTTP/1.1 200 OK

    Wrong file type detected for file myPicture.jpg:

    File type is “jpg” content-type is set to “image/jpg”, do you know what could be the main cause of this problem? I don’t know where I can found any help in imageshack api. :(

    best regards
    mario

    Do you know any idea ?

  14. Bryce Thomas says:

    Hi Mario,

    I’m really not familiar with groovy, so can’t help to that end. Have you had any success with uploading images using the demo app I’ve provided? If the demo app is working, but the stuff you’re working on in Groovy isn’t, then the best advice I can offer would be to use Fiddler and modify your program to try and replicate the output as shown in the Fiddler print screen in this article. So make up a .gif with exactly the same name (test_image_other.gif) and see if you can get it working as demonstrated above. Then, if you can get this working, try it out with a .jpg instead of a .gif and see if that is for some reason causing it to break.

    Good luck.

  15. Sathya says:

    Thanks for sharing the library Bryce.I wanted a simple, stupid, double-click and upload to Imageshack tool, couldn’t find any so I set about building one for myself. Was struggling with how to send the requests, a bit of Googling led me here. Thanks to your library, I was successfully able to create the program. ( Refer : http://www.chip.in/forums/viewtopic.php?f=1&t=53184 )

    Again, thanks a lot!
    Cheers.
    /SJ

  16. Bryce Thomas says:

    Hi Sathya,

    Fantastic! Glad to hear you were able to use the library.

  17. Jacob says:

    It’s broken as of a few days ago. Change to ImageShack. Could we get a fix please? :)

  18. Bryce Thomas says:

    Hi Jacob,

    Won’t have time to look at it for at least a few days, though I’ll see what I can work out.

  19. Jacob says:

    Thanks a lot. I really need this script for my program (but I’m awful at doing the real programming. :P )

  20. Jacob says:

    Thanks a lot.
    If it turns out they have fully blocked the API for non-registered developers, waffleimages looks like it would work in much the same way, but it’s open. Not really sure. I was never great at programming.

  21. Sathya says:

    Ah! I was wondering how come my app using library stopped working all of a sudden! This explains it.

  22. Jacob says:

    @Sathya
    It does seem like an ImageShack sided thing. Many others broke too.

  23. Carlos Alloatti says:

    Yes, it is broken now, but I got it working again, I changed the following:

    Added headers:

    “Accept: text/*”
    “User-Agent: Shockwave Flash”
    “Pragma: no-cache”
    “Connection: Keep-Alive”

    Changed:
    name=”fileupload”;
    to:
    name=”Filedata”;

    And the most important thing, changed the URL to:

    “http://iload2.imageshack.us/upload_api.php?swfupload=1&cookie=&optsize=resample&ulevel=&nocache=29632″

    I just made one change at a time until it worked again, no idea if just changing the URL is all you need, it just works again and I will leave it like that, maybe someone else can do the changes one at a time in the reverse order and see what happens.

    The response is a much bigger xml packet, around 2000 bytes, but the image link is still there just like before.

    Carlos

  24. Carlos Alloatti says:

    To expand to the above post, this is exactly what goes out as reported by Fiddler:

    POST http://iload2.imageshack.us/upload_api.php?swfupload=1&cookie=&optsize=resample&ulevel=&nocache=29632 HTTP/1.1
    Host: iload2.imageshack.us
    Connection: Keep-Alive
    Accept: text/*
    Content-Type: multipart/form-data; boundary=——–gL6cH2ae0ei4Ij5ei4KM7Ij5cH2cH2
    User-Agent: Shockwave Flash
    Pragma: no-cache
    Connection: Keep-Alive
    Content-Length: 3960
    Expect: 100-continue

    ———-gL6cH2ae0ei4Ij5ei4KM7Ij5cH2cH2
    Content-Disposition: form-data; name=”xml”

    yes
    ———-gL6cH2ae0ei4Ij5ei4KM7Ij5cH2cH2
    Content-Disposition: form-data; name=”Filedata”; filename=”xxxxxxx.jpg”
    Content-Type: application/octet-stream

    <>
    ———-gL6cH2ae0ei4Ij5ei4KM7Ij5cH2cH2

  25. Jacob says:

    Yes, the new address works. It takes a lot longer though.

  26. Jacob says:

    Just to elaborate, I just changed the address.

  27. Jacob says:

    I take it back. It works fine now. Seems to be just the address.

  28. Sathya says:

    Well, I’ve moved away from imageshack, and I’m uploading to imgur. Thanks for library Bryce!

  29. Bryce Thomas says:

    Jacob/Sathya/Carlos – thanks for working through some of the problems in the comments and sharing with everyone.

  30. Jacob says:

    Hey,
    I noticed you forgot to close the file when uploaded. You should also put it in a using statement.
    http://pastebin.com/YwtDqa6c
    There is an optimised edit.

  31. Jacob says:

    Sorry,
    The last post, you’re going to have to move the last using(…) back.

  32. darkdog says:

    well actually i get an exception in this line:
    HttpWebResponse imageShackWebResponse = imageShackWebRequest.GetResponse() as HttpWebResponse;

    it tells me that the Remoteserver has got an error: (400)

  33. Chris says:

    I got it to work after reading Carlos Alloatti’s changes. Actually, only the URL change was needed. Many of the names searched for in the switch statement are changed though – so I had to search for what I needed, e.g. it doesn’t return “image_location” now, so I had to parse “image_html”

    btw, Jacob’s idea is good, but won’t quite work the way as posted – an exception gets thrown
    “This property cannot be set after writing has started.”
    So just nest the using statements.

  34. Chris says:

    Nevermind – it does still work!!

    I had some spyware that messed with my proxy settings – internet explorer burning me even though I use firefox! grrrrr

  35. Carlos Alloatti says:

    As of March 2011 this stopped working for me. The error was: “You must provide a valid auth token or dev key”

    I just downloaded the ImageShack Uploader app from ImageShack, and checked how they did it. I added the following to the uploaded info:

    –UPLOADERBOUNDARY
    Content-Disposition: form-data; name=”public”

    yes
    –UPLOADERBOUNDARY
    Content-Disposition: form-data; name=”key”

    BXT1Z35V8f6ee0522939d8d7852dbe67b1eb9595
    –UPLOADERBOUNDARY

    Now it works again.

  1. [...] Based on Omkar’s ScreenUp and uses Bryce’s C# Wrapper for ImageShack XML API. Also, thanks to Omkar for the selection screenshot library. [...]

Leave a Reply