As promised yesterday here is a little bit of code I use to display photos from Picasa Web Albums. I use the Google Data API and their .NET client library. You can find it here, get the file named “Google Data API Setup (1.6.0.0).msi“. You might also want to check out the Developers Guide.
Anyway this will be a very brief and very focused little guide. The purpose? to display semi-random photos from a public Picasa Web album. First we need some references, and for this guide we will need the following.
- Google.GData.Client.dll
- Google.GData.Extensions.dll
- Google.GData.Photos.dll
The files are probably located in: c:\Program Files (x86)\Google\Google Data API SDK\Redist\
We will be using four classes from the API, these are; PicasaService, PhotoQuery, PicasaFeed and PicasaEntry. They all inherit from some base Google Data API classes, but no need to worry about that now
Now for some code, we need to use the PicasaService class to query for photos, for this we need a PhotoQuery object to pass to the PicasaService‘s Query method. This results in a PicasaFeed object containing entries that represent the photos that we queried for, these are PicasaEntry objects that we can work with. We can get thumbnail- and photo URL’s among other things.
1: PicasaService service = new PicasaService("slyngelstat.dk");
2: PhotoQuery pq = new PhotoQuery(PicasaQuery.CreatePicasaUri("username", "5496727133364271337"));
3: PicasaFeed feed = (PicasaFeed)service.Query(pq);
Now we can start using our feed. I created a small wrapper class, Photo, to make it easier to use in a small User Control.
1: public class Photo
2: {
3: public string Url { get; set; }
4: public string ThumbnailUrl { get; set; }
5: }
The PicasaEntry object contains a large hieracy of data, the best way to learn what you need and not, is just to navigate through it using the Visual Studio debugger. But anyway we need to create this very advanced Photo object
, and fill it with a random photo from the resulting Picasa feed.
1: Random random = new Random();
2: PicasaEntry entry = (PicasaEntry)feed.Entries[random.Next(0, feed.Entries.Count())];
3:
4: Photo photo = new Photo
5: {
6: Url = entry.Links[1].AbsoluteUri,
7: ThumbnailUrl = entry.Media.Thumbnails[2].Url
8: };
That was easy enough. Now to display it in a UserControl you could wrap the above code in a simple method and use it in a simple get-property. Lets do that.
1: public Photo GetRandomPhoto()
2: {
3: // The code from above
4: return photo;
5: }
And the get-property.
1: private Photo randomPhoto;
2: public Photo RandomPhoto
3: {
4: get
5: {
6: if (randomPhoto == null)
7: {
8: randomPhoto = GetRandomPhoto();
9: }
10: return randomPhoto;
11: }
12: }
When this is placed in the code of a User Control the following can be used easily from the markup and the photo will change when ever you load the page.
1: <a href='<%=RandomPhoto.Url %>'>
2: <img src='<%=RandomPhoto.ThumbnailUrl %>' alt="photo" />
3: </a>
And there you have it. This could be extended to a slideshow changing the photos every few seconds, kind of like I have on the front page at the moment.
The Google Data API is very usefull, this guide showed how to display photos from a public album, but you can also authenticate using your Picasa account, then you can upload new photos, create albums etc.
Everyone have a nice weekend, and by the way, guess who just started a two week vacation…AWESOME!!