Sunday, October 28, 2012

Google Picasa Web API
How to pull thumbnail of all photos from Picasa web Album?

Picasa Album is a great service provided by Google to store and share online images. There are many plug-in and interfaces available to access images stored in Picasa web album. These images can be accessed from many devices such as cellphone, iPhone, tablets, web applications, desktop applications and many more.

Google has provided Google Data API which can be used by developer community to develop applications and integrate it with Picasa web album. Developers need to first download the Google Data API library to interact with Google services such as Picasa web albums, Google Docs, Google BigQuery, YouTube and other Google provided services.

On this site, I have published articles on using Google Data API with .NET and accessing Google services such as Picasa web Albums, Google Analytics, Google Spreadsheet, Google BigQuery. This article is another extension to the same set of articles.

In this post we will learn to pull thumbnails of all photos stored in a particular Picasa web album using .NET. First we will interact with Google services by providing our Google credentials and then pull the albums stored with the specific Google account. We will then pull the thumbnails of all images stored in a specific web album

To start with, I have designed following form in Visual Studio 2008. The form has two text boxes to enter Google credentials i.e. Google account and password. The login button has code logic to connect to Google Picasa service by supplying the Google credentials entered into the two text boxes.

Get Album List button has code logic to pull all albums name and id stored with the Google account. The lists of Albums are shown in ListView1 control

ListView1 control has an event SelectedIndexChanged which contains the code logic to access all the images stored in the selected web album. Each of the image is than loaded into ImageList1 control. Once all the images are read and added into ImageList1, the thumbnail of all images are shown in ListView2 control.

Showing thumbnail of all images stored web album "Harshita".

Showing thumbnail of all images stored in web album "Canada.Halifax.2010"

The code snippet on this form is following:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;

using Google.GData.Photos;
using Google.GData.Extensions;
using Google.GData.Extensions.Location;
using Google.Picasa;
using System.IO;

namespace TestDOTNET
{
    public partial class Form1 : Form
    {
        String myAlbumID;
        PicasaService myPicasa;
        AlbumQuery myAlbumQuery;
        PicasaFeed myPicasaFeed;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Code logic to connect with Google Picasaservice with the supplied credentials
            myPicasa = new PicasaService("Vikash-Test-Picasa");
            myPicasa.setUserCredentials(textBox1.Text, textBox2.Text);
            label3.Text = "Click on Get Album List";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Code logic to pull Album names and ID
            listView1.Columns.Add("Album Title");
            listView1.Columns.Add("Album ID");            
            listView1.FullRowSelect = true;

            myAlbumQuery = new AlbumQuery(PicasaQuery.CreatePicasaUri(textBox1.Text));
            myAlbumQuery.KindParameter = "album";

            myPicasaFeed = myPicasa.Query(myAlbumQuery);
            foreach (PicasaEntry p in myPicasaFeed.Entries)
            {

                AlbumAccessor myAlbum = new AlbumAccessor(p);
                listView1.Items.Add((new ListViewItem(new string[] { myAlbum.AlbumTitle.ToString(), myAlbum.Id.ToString() })));
            }


        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Code logic to pull all thumbnails of selected web album
            myAlbumID = "";
            imageList1.Images.Clear();
            listView2.Clear();
            ListView.SelectedListViewItemCollection AlbumList = listView1.SelectedItems;

            foreach (ListViewItem item in AlbumList)
            {
                myAlbumID = item.SubItems[1].Text.ToString();

            }

            if (myAlbumID.Length > 0)
            {
                PhotoQuery myPhotoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri(textBox1.Text, myAlbumID));

                PicasaFeed myPicasaFeed = myPicasa.Query(myPhotoQuery);
                foreach (PicasaEntry p in myPicasaFeed.Entries)
                {
                    string firstThumbUrl = p.Media.Thumbnails[0].Attributes["url"] as string;
                    imageList1.Images.Add(LoadMyImage(firstThumbUrl));               
                }
                listView2.SmallImageList = imageList1;                

                listView2.View = View.LargeIcon;
                this.imageList1.ImageSize = new Size(32, 32);
                listView2.LargeImageList = this.imageList1;
                
                for (int j = 0; j < this.imageList1.Images.Count; j++)
                {
                    ListViewItem item = new ListViewItem();
                    item.ImageIndex = j;
                    listView2.Items.Add(item);
                }
            }
        }

        private Image LoadMyImage(string url)
        {
            //Code logic to download all thumbnails of images
            System.Net.WebRequest request = System.Net.WebRequest.Create(url);

            System.Net.WebResponse response = request.GetResponse();
            System.IO.Stream responseStream = response.GetResponseStream();

            Bitmap bmp = new Bitmap(responseStream);

            responseStream.Dispose();

            return bmp;
        }

    }
}

So this is how you can download the thumbnails of all images stored in a Picasa web album. Just in case you want to download the source code please visit the Download Zone.

Popular Posts

Real Time Web Analytics