ImageShack is a great image host. The best thing about it is the ImageShack XML API which makes it possible to programmatically upload images. This works just the same as uploading manually. Registration is not required, unless you intend to change or remove the images afterwards. The following code is based on something I found on MSDN. I added some improvements, such as a more flexible mime type finder, a XML parser, etc. Furthermore I decreased the average amount of lines of code per function, because large functions are really a pain to deal with.
To use the ImageShack API. it only requires to send one variable (the image file) via POST to http://www.imageshack.us/index.php:
There are also couple of optional parameters, which are worth considering:
- xml = "yes" (returns XML code)
- cookie (registration code of a user to whom the image will belong)
- optimage = {"0", "1"} (specifies that image will be resized)
- optsize = "[axis1]x[axist2]" (will resize the image to fit any intended resolution, for more information see here)
You can find more information about how to upload files via POST at an earlier post in my blog. The following listing demonstrates the XML code which is returned by the service, if xml = "yes" is specified.
<?xml version="1.0" encoding="iso-8859-1"?>
<links>
<image_link>http://img33.imageshack.us/img33/7729/microsoftnet2008logo.png</image_link>
<thumb_link>http://img33.imageshack.us/img33/7729/microsoftnet2008logo.th.png</thumb_link>
<ad_link>http://img33.imageshack.us/my.php?image=microsoftnet2008logo.png</ad_link>
<thumb_exists>no</thumb_exists>
<total_raters>0</total_raters>
<ave_rating>0.0</ave_rating>
<image_location>img33/7729/microsoftnet2008logo.png</image_location>
<thumb_location>img33/7729/microsoftnet2008logo.th.png</thumb_location>
<server>img33</server>
<image_name>microsoftnet2008logo.png</image_name>
<done_page>http://img33.imageshack.us/content.php?page=done&l=img33/7729/microsoftnet2008logo.png</done_page>
<resolution>117x111</resolution>
<filesize>4986</filesize>
<image_class>r</image_class>
</links>
In order to upload a file via POST a mime type is required. The following listing shows two different ways to determine the mime type of a given file. The first one is definitely the more elegant way to accomplish this task, but you might want to use the upload from a mobile device where the registry is not as advanced.
Public Function GetMimeType(ByVal Filename As String) As String
Dim result As String
Dim extension As String = System.IO.Path.GetExtension(Filename).ToLower()
Dim registryKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension)
If registryKey IsNot Nothing AndAlso registryKey.GetValue("Content Type") IsNot Nothing Then
result = registryKey.GetValue("Content Type").ToString
Else
result = "image/unknown"
End If
Return result
End Function
Public Function GetMimeType2(ByVal Filename As String) As String
Dim extension As String = System.IO.Path.GetExtension(Filename).ToLower()
Dim result As String
Select Case extension
Case ".jpg"
result = "image/jpeg"
Case ".jpeg"
result = "image/jpeg"
Case ".gif"
result = "image/gif"
Case ".png"
result = "image/png"
Case ".bmp"
result = "image/bmp"
Case ".tif"
result = "image/tiff"
Case ".tiff"
result = "image/tiff"
Case Else
result = "image/unknown"
End Select
Return result
End Function
As usual, at the end follows the complete source code.