Saturday, May 19, 2012

How to delete image from Picasa Web Album with Google Data API?

This post is next in series of our learning of Google Data API library. We have learnt using Google Data API to externally access Picasa Web Album.

In this post we will learn how we can delete photo from a Picasa web album. We will use Google Data API and .NET code to delete photo from our recently created Picasa web album "Google is the Best".

To extend our example in our previous post; I have added a button control in our .NET interface "Delete Photo". The objective is that the list view will show the photo list from the web album and you can select any image from the list and click on Delete Photo button to remove it from the web album. We are using the same web album that we created in this series

I am importing following Google Data API namespace in this program under each post.

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


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

private void button1_Click(object sender, EventArgs e)
{
        listView1.Columns.Add("Photo Title");
        listView1.Columns.Add("Photo Summary");
        listView1.Columns.Add("Published Date");
        listView1.Columns.Add("Last Updated");
        listView1.FullRowSelect = true;

//Create new Picasa service
        PicasaService myPicasa = new PicasaService("Vikash-Test-Picasa");

//Passing user credentials to Picasa service
        myPicasa.setUserCredentials("abc@gmail.com", "abcpassword");

//Creating a new photo query by passing userid and albumid
        PhotoQuery myPhotoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri("abc", "5737335314773942337"));

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

The .NET code snippet on "Delete Photo" button is following:

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

//Create a photoquery by passing userid and albumid
        PhotoQuery myPhotoQuery = new PhotoQuery(PicasaQuery.CreatePicasaUri("abc", "5737335314773942337"));
        PicasaFeed myPicasaFeed = myPicasa.Query(myPhotoQuery);
        foreach (PicasaEntry p in myPicasaFeed.Entries)
        {
        if(p.Title.Text.Equals(listView1.SelectedItems[0].Text))
        {
            p.Delete();
            System.Windows.Forms.MessageBox.Show("Image Deleted Successfully");
        }
}
}

After deleting the image a message box will prompt stating that "Image Deleted Successfully".

I run the form and clicked on "Get Photo List" button and code runs successfully and it fetched all the images from Picasa web album "Google is the Best"

I selected one image "PicasaTest4.jpg" and clicked on "Delete Photo" button. The code runs successfully and deleted the image from Picasa web album.

I clicked on "Get Photo List" again and this time it shows only four images as it has deleted one image. If you look at the second image in this post we had earlier five images in the web album.

I went to the Picasa web account to verify this and sure enough "PicasTest4.jpg" image was deleted successfully.

So we have learnt how to delete images from a particular web album.

Thanks for reading till this point.

Popular Posts

Real Time Web Analytics