Using Flickr in .NET applications
Currently I’m working on a cool project, where I need to show some nice photos, just for visual effects. And the first thing I thought, where could I get cool photos? Easy, somewhere on the internet, right? Well, that can be tricky, since finding images on web is not that simple, there are license problems, images can be in poor quality, etc.
Since I’m also using Flickr for my own photos, it was obvious to choose this database. And it’s supposed to be the largest (public) database of images, photographs and even world famous artist are posting images on Flickr. OK, so we need to somehow access this database with our code. Sure, we can parse HTML, but that would be pretty stupid, since Flickr has great APIs exposed into the wild.
Flick APIs are exposed in various formats (JSON, SOAP, REST, etc.) but using that service in .NET applications means a little bit more hacking, if you want to do it by yourself. But there is one library, called FlickrNET, which was developed by Sam Judson. So I’ve used this library, which is super easy to use.
And as today is very popular for web services, you need to acquire API key, which you can get here for free.
For my case, I need to access just one random photo from Flickr Explore, since there are pretty cool photos from public photo stream. The only “hack” that I needed to do is to check, if there is a large image. In Flikcr, you (as author) can set to which sizes of photos others can access and if you upload smaller image than the “large” – which is defined by 1024 pixels width – Flickr will not create bigger image. So when requesting images, we need to take that into account – and here’s the code to do that:
FlickrNet.Flickr f = new FlickrNet.Flickr("your_api_key"); FlickrNet.Photos photos =
f.InterestingnessGetList(FlickrNet.PhotoSearchExtras.All, 100, 1); bool stop = false; int stop2 = 0; FlickrNet.Photo photo = null; while (!stop) { Random r = new Random((int)DateTime.Now.Ticks); photo = photos.PhotoCollection[r.Next(0, 99)]; FlickrNet.Size[] size = f.PhotosGetSizes(photo.PhotoId).SizeCollection; if (size.Length == 5 || stop2 == 100) if (size[4].Label.ToLower() == "large") stop = true; stop2++; } pictureBox1.ImageUrl = photo.LargeUrl;
So what we are doing here is just get a list of 100 popular images, and then checking for the first picture, that has large format. We will get a URL pointing to that image in photo.LargeUrl property and since this is a ASP.NET application, that is enough. For Windows applications, you would need to do a simple WebRequest for getting the image and than showing it as Bitmap in picture box.
That’s it! Very simple and extremely useful for creating cool UIs. Leave a comment, if you have any question.
| Tweet |
author: Aleš Rosina | Comments: 2 | Tags: c-sharp, flickr, dotnet
January
2010
This is some useful code with Flickr! I have recently taken up photography, so this will help me out a lot. <a href="http://www.angieslist.com/">Angie's List</a> offers options for photography services. Thanks again!
This post is really fantastic and helpful. Anyone who is interested in using .net application in flickr can go through this post which will solve his each and every problem. Since many people use flickr for own photos because photos from other sources cannot be used without examining since there will rights reserved on few of them so this post will really help those people
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=820de37d-9889-4474-b371-42700412348f)
