Wednesday, May 23, 2012

How to download images from Picasa web Album with Google Data API?

To continue our learning towards Google Data API; this post will show us how we can download images from Google Data API.

To start with, I have designed following .NET GUI interface. We have two list view and and two button control on the form. List view 1 will fetch all album details stored in our Picasa web account. After selecting a particular web album from list view 1 control`; all the images stored in that particular web album will be shown in the list view 2.

I am importing following Google Data API namespace in the form.

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

We have define string variable `myAlbumID`at form level

public partial class Form4 : Form
{
String myAlbumID;

//Code to follow


}

The .NET code snippet on "Get Album List" button is following:

private void button1_Click(object sender, EventArgs e) 
 { 
 listView1.Columns.Add("Album ID"); 
 listView1.Columns.Add("Album Title"); 
 listView1.Columns.Add("No Of Photo"); 
 listView1.FullRowSelect = true; 
 
 PicasaService myPicasa = new PicasaService("Vikash-Test-Picasa"); 
 myPicasa.setUserCredentials("abc@gmail.com", "abcpassword"); 
 AlbumQuery myAlbumQuery = new AlbumQuery(PicasaQuery.CreatePicasaUri("abc")); 
 PicasaFeed myPicasaFeed=myPicasa.Query(myAlbumQuery); 
 foreach (PicasaEntry p in myPicasaFeed.Entries) 
 { 
 
 AlbumAccessor myAlbum = new AlbumAccessor(p); 
 listView1.Items.Add((new ListViewItem(new string[] { myAlbum.Id.ToString(),myAlbum.AlbumTitle.ToString(), myAlbum.NumPhotos.ToString()}))); 
 } 
 }

The .NET code snippet on "List view 1 select index change event" is following:

private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
 { 
 
 myAlbumID = ""; 
 listView2.Clear(); 
 ListView.SelectedListViewItemCollection AlbumList =listView1.SelectedItems; 

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

 } 

 if (myAlbumID.Length > 0) 
 { 
  listView2.Columns.Add("Photo Title"); 
  listView2.Columns.Add("Photo Summary"); 
  listView2.Columns.Add("Published Date"); 
  listView2.Columns.Add("Last Updated"); 
  listView2.FullRowSelect = true; 

  PicasaService myPicasa = new PicasaService("Vikash-Test-Picasa"); 
  myPicasa.setUserCredentials("abc@gmail.com", "abcpassword"); 

  PhotoQuery myPhotoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri("abc", myAlbumID)); 

  PicasaFeed myPicasaFeed = myPicasa.Query(myPhotoQuery); 
  foreach (PicasaEntry p in myPicasaFeed.Entries) 
  { 
  listView2.Items.Add(new ListViewItem(new string[] { p.Title.Text, p.Summary.Text, p.Published.ToShortDateString(), p.Updated.Date.ToShortDateString() })); 
  } 
 } 
 }

The .NET code snippet on "Dowload All Images" button is following:

private void button2_Click(object sender, EventArgs e)
  {
  PicasaService myPicasa = new PicasaService("Vikash-Test-Picasa");
  myPicasa.setUserCredentials("abc@gmail.com", "abcpassword");

  PhotoQuery myPhotoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri("abc", myAlbumID));

  PicasaFeed myPicasaFeed = myPicasa.Query(myPhotoQuery);
  foreach (PicasaEntry p in myPicasaFeed.Entries)
  {
   
  Stream mystream=myPicasa.Query(new Uri(p.Content.Src.ToString()));
  Bitmap m = new Bitmap(mystream);
  m.Save("C:\\006.  Temp\\PicasaImage\\" +p.Title.Text);

  }

  System.Windows.Forms.MessageBox.Show("All Images have been downloaded");
  }

After setting up the code; I run the form and click on Get Album List button. It fetches all the album details stored in my picasa web account. I clicked on album `Google is the Best`and all images stored in that album were shown in the list view 2.

The next thing I did was to click on button “Download All Images”; the code runs successfully and downloaded all the images to C:\006. Tem\PicasaImage folder in my local system. After all images were downloaded the code pop-up a messagebox stating that All Images have been downloaded.

I checked my folder C:\006. Tem\PicasaImage and all imags were successfully downloaded.

So our objective to download images from Picasa web album has been achieved.

Thanks for reading till this point.

If you want to explore more with the Source code of this post; please visit Download Zone.

Popular Posts

Real Time Web Analytics