For one of my recent projects I had to use Flickr as an image repository, so this code just shows how I was able to do that with the help of the fantastic Flickrnet.dll library. You can download the latest Flickrnet library here: http://flickrnet.codeplex.com/. You can also download the article’s full working Flickr demo code.
Getting Started
Before we dive into the MVC portion, you have to make sure you have installed the flickrnet.dll file, referenced it, and added the appropriate web.config elements.
configSections element
Append the following element after the default sectionGroup:
<section name="flickrNet" type="FlickrNet.FlickrConfigurationManager,FlickrNet"/>
And setup your database connection strings
Flickrnet element
Add the following element before the appSettings element:
<flickrNet apiKey="YOUR_API_KEY" secret="YOUR_SECRET" cacheDisabled="true"/>
appSettings element
Add the following keys to the appSettings element:
<add key="FlickrAuthtoken" value="YOUR_FROB/TOKEN" /> <add key="FlickrUserId" value="YOUR_USERID"/>
That is all you need to do in web.config. You get your apikey and secret through flickr. An awesome article off the flickrnet.codeplex site covers how to get your FROB/Token: http://blogs.msdn.com/coding4fun/archive/2006/11/22/1126978.aspx.
Database Setup
The images are going to be uploaded to Flickr, but we are going to keep references to the images in the database for later retrieval. Here is the table script if you are using MySQL:
CREATE TABLE `sampledb`.`Image` ( `ImageID` int(11) NOT NULL AUTO_INCREMENT, `ImageURL` varchar(250) DEFAULT NULL, `Tags` varchar(150) DEFAULT NULL, `FlickrID` varchar(50) DEFAULT NULL, `ImageURLThumbnail` varchar(250) DEFAULT NULL, PRIMARY KEY (`ImageID`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
or if you are using SQL Server:
CREATE TABLE [SampleDB].[dbo].[Image] ( ImageID int NOT NULL IDENTITY, ImageURL varchar(250) DEFAULT NULL, Tags varchar(150) DEFAULT NULL, FlickrID varchar(50) DEFAULT NULL, ImageURLThumbnail varchar(250) DEFAULT NULL, PRIMARY KEY (ImageID) );
Once the database is setup, all we have left is the code for uploading, storing, and retrieving the images.
Uploading Images into Flickr
This part is easier than you think. We start out with a typical upload form. For this sample we are going to only have a file input control and a textbox to store our tags:
<form method="post" enctype="multipart/form-data" id="form1" action="/Home/Index">
<b>Image</b><br />
<input type="file" name="NewImageName" id="NewImageName" />
<br /><br />
<b>Tags</b><br />
<input type="text" name="NewImgTags" id="NewImgTags" />
<br />
<input type="submit" value="Upload" />
</form>
Nothing controversial there, moving on to the controller is where we find the meat. The first thing the controller needs is a reference to the Flirckrnet.dll and to System.Configuration, so you add your using statements:
using FlickrNet;
using System.Configuration;
Next up is creating the Index post method. Here, we reference the Flickrnet object and upload the photo. At this point, Flickr takes the image, and depending upon the size and dimensions, will resize it into any of five different sizes: square, thumbnail, small, medium, and large.
We then loop through the different sizes Flickr sends back, and store the thumbnail and large image urls into our table. Below is part of the controller method that uploads the image, and then loops through the sizes. You can download a full working sample here.
var flickr = new Flickr
{
AuthToken = ConfigurationManager.AppSettings["FlickrAuthtoken"]
};
//
// Upload the image, title, tags, security settings
//
var flickr_id = flickr.UploadPicture(file.InputStream, ImageTitle, ImageDescription, ImageTags, IsPublic,IsFamily,IsFriends);
//
// Loop through links to resized images up on flickr
// and store in the database
//
var flickrPhotos = flickr.PhotosGetSizes(flickr_id);
Image newImage = new Image();
newImage.FlickrID = flickr_id.ToString();
//
// I only care about the thumbnail and the large image, but
// you can store all of them (when applicable)
//
for (int i = 0; i < flickrPhotos.SizeCollection.Length; i++)
{
switch (flickrPhotos.SizeCollection[i].Label)
{
case "Square":
break;
case "Thumbnail":
newImage.ImageURLThumbnail = flickrPhotos.SizeCollection[i].Source;
break;
case "Small":
break;
case "Medium":
break;
case "Large":
newImage.ImageURL = flickrPhotos.SizeCollection[i].Source;
ViewData["NewUrl"] = flickrPhotos.SizeCollection[i].Source;
break;
}
}
newImage.Tags = ImageTags;
newImage.CreateAndFlush();
The code sample then sends the large image url from Flickr back down to the browser to preview. Below is a video of the code in action:

