Retrieve Picasa Albums and View Photos in ASP.Net

Introduction

This article describes how to retrieve Picasa Albums with a photo in ASP.Net.  Here I will describe how to communicate with Picasa using the Google API.

Description

Since it is a third party software, for this we require a third party DLL to be referenced by our application that will communicate with the Gmail server.

For this we must download the Google API setup from the following link:

http://code.google.com/p/google-gdata/

Or you can directly download it from this link:

https://google-gdata.googlecode.com/files/Google_Data_API_Setup_2.1.0.0.msi

When you install this setup you will get these DLL files in your PC:

  1. Google.GData.Client.dll
  2. Google.GData.Photos.dll
  3. Google.GData.Extensions.dll

Collect these DLLs.

To view your retrieved photos in a gallery look we use the jQuery class libraries. So you must download these files and CSS:

  1. jquery-1.10.1.js
  2. jquery.galleriffic.js
  3. jquery.opacityrollover.js

You can also download it from the source code attached in the file.

Design

Now design your screen like the following screen.

Image1.jpg

Or you can copy the following source code:

  1. <form id="form1" runat="server">  
  2.     <table>  
  3.         <tr id="tdlogin" runat="server">  
  4.             <td colspan="2">  
  5.                 <table align="center">  
  6.                     <tr>  
  7.                         <td>  
  8.                             <asp:Label ID="Label2" runat="server" Text="User Name"></asp:Label>  
  9.                         </td>  
  10.                         <td>  
  11.                             <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>  
  12.                         </td>  
  13.                     </tr>  
  14.                     <tr>  
  15.                         <td>  
  16.                             <asp:Label ID="Label3" runat="server" Text="Password"></asp:Label>  
  17.                         </td>  
  18.                         <td>  
  19.                             <asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>  
  20.                         </td>  
  21.                     </tr>  
  22.                     <tr>  
  23.                         <td colspan="2" align="center">  
  24.                             <asp:Button ID="btnLogin" runat="server" Text="Login"  
  25.                                 onclick="btnLogin_Click" />  
  26.                             <asp:HiddenField ID="hfUsername" runat="server" />  
  27.                             <asp:HiddenField ID="hfPwd" runat="server" />  
  28.                         </td>  
  29.                     </tr>  
  30.                     <tr>  
  31.                         <td colspan="2" align="center">  
  32.                             <asp:Label ID="lblMsg" runat="server" ForeColor="Red"></asp:Label>  
  33.                         </td>  
  34.                     </tr>  
  35.                 </table>  
  36.             </td>  
  37.         </tr>  
  38.         <tr>  
  39.             <td style='width: 40%;'>  
  40.                 <div>  
  41.                     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" PageSize="7"  
  42.                         Width="100%" AllowPaging="true" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_SelectedIndexChanging">  
  43.                         <Columns>  
  44.                             <asp:TemplateField HeaderText="Album Name">  
  45.                                 <ItemTemplate>  
  46.                                     <asp:Label ID="Label1" runat="server" Text='<%#Eval("Album") +"-"+Eval("NO of photo") %>'></asp:Label>  
  47.                                 </ItemTemplate>  
  48.                             </asp:TemplateField>  
  49.                             <asp:TemplateField HeaderText="Click to View">  
  50.                                 <ItemTemplate>  
  51.                                     <asp:LinkButton ID="LnkViewphoto" CommandName="Download" CommandArgument='<%#Eval("id") %>'  
  52.                                         runat="server"> <img src='<%#Eval("url") %>' width="150px" height="80px"/></asp:LinkButton>  
  53.                                 </ItemTemplate>  
  54.                             </asp:TemplateField>  
  55.                         </Columns>  
  56.                     </asp:GridView>  
  57.                 </div>  
  58.             </td>  
  59.             <td style='width: 65%;'>  
  60.                 <asp:Panel ID="Panel1" runat="server" Visible="false">  
  61.                     <div id="page">  
  62.                         <div id="container">  
  63.                             <h2>  
  64.                                 Gallery by Sanjeeb</h2>  
  65.                             <div id="gallery" class="content">  
  66.                                 <div id="controls" class="controls">  
  67.                                 </div>  
  68.                                 <div class="slideshow-container">  
  69.                                     <div id="loading" class="loader">  
  70.                                     </div>  
  71.                                     <div id="slideshow" class="slideshow">  
  72.                                     </div>  
  73.                                 </div>  
  74.                                 <div id="caption" class="caption-container">  
  75.                                 </div>  
  76.                             </div>  
  77.                             <div id="thumbs" class="navigation">  
  78.                                 <div id="divSlider" runat="server">  
  79.                                 </div>  
  80.                             </div>  
  81.                             <div style="clear: both;">  
  82.                             </div>  
  83.                             <h4>  
  84.                                 CopyRight by Sanjeeb</h4>  
  85.                         </div>  
  86.                     </div>  
  87.                 </asp:Panel>  
  88.             </td>  
  89.         </tr>  
  90.     </table>  
  91. </form>  

Next add the following JavaScript code in the head tag (it's used to design the gallery):

 

  1. <head runat="server">  
  2.     <title></title>  
  3.     <link rel="stylesheet" href="css/basic.css" type="text/css" />  
  4.     <link rel="stylesheet" href="css/galleriffic-2.css" type="text/css" />  
  5.         <script type="text/javascript" src="js/jquery-1.10.1.js"></script>  
  6.     <script type="text/javascript" src="js/jquery.galleriffic.js"></script>  
  7.     <script type="text/javascript" src="js/jquery.opacityrollover.js"></script>  
  8.     <script type="text/javascript">  
  9.         document.write('');  
  10.     </script>  
  11.     <script type="text/javascript">  
  12.         jQuery(document).ready(function ($) {  
  13.             // We only want these styles applied when javascript is enabled  
  14.             $('div.navigation').css({ 'width''300px''float''left' });  
  15.             $('div.content').css('display''block');  
  16.   
  17.             // Initially set opacity on thumbs and add  
  18.             // additional styling for hover effect on thumbs  
  19.             var onMouseOutOpacity = 0.67;  
  20.             $('#thumbs ul.thumbs li').opacityrollover({  
  21.                 mouseOutOpacity: onMouseOutOpacity,  
  22.                 mouseOverOpacity: 1.0,  
  23.                 fadeSpeed: 'fast',  
  24.                 exemptionSelector: '.selected'  
  25.             });  
  26.             // Initialize Advanced Galleriffic Gallery  
  27.             var gallery = $('#thumbs').galleriffic({  
  28.                 delay: 2500,  
  29.                 numThumbs: 15,  
  30.                 preloadAhead: 10,  
  31.                 enableTopPager: true,  
  32.                 enableBottomPager: true,  
  33.                 maxPagesToShow: 7,  
  34.                 imageContainerSel: '#slideshow',  
  35.                 controlsContainerSel: '#controls',  
  36.                 captionContainerSel: '#caption',  
  37.                 loadingContainerSel: '#loading',  
  38.                 renderSSControls: true,  
  39.                 renderNavControls: true,  
  40.                 playLinkText: 'Play Slideshow',  
  41.                 pauseLinkText: 'Pause Slideshow',  
  42.                 prevLinkText: 'Previous Photo',  
  43.                 nextLinkText: 'Next Photo,  
  44.                 nextPageLinkText: 'Next ,  
  45.                 prevPageLinkText: 'Prev',  
  46.                 enableHistory: false,  
  47.                 autoStart: false,  
  48.                 syncTransitions: true,  
  49.                 defaultTransitionDuration: 900,  
  50.                 onSlideChange: function (prevIndex, nextIndex) {  
  51.                     // 'this' refers to the gallery, which is an extension of $('#thumbs')  
  52.                     this.find('ul.thumbs').children()  
  53.                                                 .eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()  
  54.                                                 .eq(nextIndex).fadeTo('fast', 1.0);  
  55.                 },  
  56.                 onPageTransitionOut: function (callback) {  
  57.                     this.fadeTo('fast', 0.0, callback);  
  58.                 },  
  59.                 onPageTransitionIn: function () {  
  60.                     this.fadeTo('fast', 1.0);  
  61.                 }  
  62.             });  
  63.         });  
  64.     </script>  
  65. </head>  

 

Now go to the code view.

Next add a reference of the following Google gdata DLL to your website:

  1. Google.GData.Client.dll
  2. Google.GData.Extensions.dll
  3. Google.GData.Photos.dll

And write the following code .cs file:

  1. using System;  
  2. using System.Web.UI.WebControls;  
  3. using Google.GData.Photos;  
  4. using System.Data;  
  5. using System.Text;  
  6.   
  7. public partial class _Default : System.Web.UI.Page  
  8. {  
  9.     protected void Page_Load(object sender, EventArgs e)  
  10.     {  
  11.         lblMsg.Text = string.Empty;  
  12.     }  
  13.     /// <summary>  
  14.     /// This method used to bind the albums to grid  
  15.     /// </summary>  
  16.     private void BindAlbum()  
  17.     {  
  18.         AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(txtUserName.Text));  
  19.         PicasaService service = new PicasaService("sample");  
  20.         service.setUserCredentials(txtUserName.Text, txtpwd.Text);  
  21.         Session["token"] = service.QueryClientLoginToken();  
  22.         PicasaFeed feed = service.Query(query);  
  23.         DataSet ds = new DataSet();  
  24.         DataTable dt = new DataTable();  
  25.         dt.Columns.Add(new DataColumn("Album"typeof(string)));  
  26.         dt.Columns.Add(new DataColumn("NO of photo"typeof(string)));  
  27.         dt.Columns.Add(new DataColumn("url"typeof(string)));  
  28.         dt.Columns.Add(new DataColumn("id"typeof(string)));  
  29.         foreach (PicasaEntry entry in feed.Entries)  
  30.         {  
  31.             DataRow dr1 = dt.NewRow();  
  32.             AlbumAccessor ac = new AlbumAccessor(entry);  
  33.             dr1["Album"] = entry.Title.Text;  
  34.             dr1["NO of photo"] = ac.NumPhotos;  
  35.             dr1["id"] = ac.Id;  
  36.             dr1["url"] = entry.Media.Thumbnails[0].Url;  
  37.             dt.Rows.Add(dr1);  
  38.         }  
  39.         ds.Tables.Add(dt);  
  40.         ViewState["Album"] = ds;  
  41.         GridView1.DataSource = ds;  
  42.         GridView1.DataBind();  
  43.     }  
  44.   
  45.     /// <summary>  
  46.     /// this event used to bind the photo to gallery using the albumid  
  47.     /// </summary>  
  48.   
  49.       protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  50.     {  
  51.         if (e.CommandName == "Download")  
  52.         {  
  53.             Panel1.Visible = true;  
  54.             PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(txtUserName.Text, e.CommandArgument.ToString()));  
  55.             PicasaService service = new PicasaService("Picasa");  
  56.             service.setUserCredentials(hfUsername.Value, hfPwd.Value);  
  57.             PicasaFeed feed = service.Query(query);  
  58.             StringBuilder html = new StringBuilder();  
  59.             string title;  
  60.             html.Append("<ul class=\"thumbs noscript\">");  
  61.             foreach (PicasaEntry entry in feed.Entries)  
  62.             {  
  63.                 if (entry.Title.Text.LastIndexOf(".") > -1)  
  64.                     title = entry.Title.Text.Substring(0, entry.Title.Text.LastIndexOf("."));  
  65.                 else  
  66.                     title = entry.Title.Text;  
  67.   
  68.                 html.Append(String.Format("<li><a class=\"thumb\" name={0} href=\"{1}\" title=\"{2}\"><img src=\"{3}\" alt=\"{4}\"/></a>",  
  69.                     title, entry.Media.Content.Url, title, entry.Media.Thumbnails[0].Url, title));  
  70.                 html.Append(String.Format("<div class=\"caption\"><div class=\"image-title\">{0}</div><div class=\"image-desc\">{1}</div></div></li>",  
  71.                     title, entry.Summary.Text));  
  72.   
  73.             }  
  74.             html.Append("</ul>");  
  75.             divSlider.InnerHtml = html.ToString();  
  76.         }  
  77.     }  
  78.   
  79.     protected void GridView1_SelectedIndexChanging(object sender, GridViewPageEventArgs e)  
  80.     {  
  81.         DataSet ds = ViewState["Album"as DataSet;  
  82.         GridView1.PageIndex = e.NewPageIndex;  
  83.         GridView1.DataSource = ds;  
  84.         GridView1.DataBind();  
  85.     }  
  86.     protected void btnLogin_Click(object sender, EventArgs e)  
  87.     {  
  88.         if (txtUserName.Text != string.Empty && txtpwd.Text!=string.Empty)  
  89.         {  
  90.             hfUsername.Value = txtUserName.Text;  
  91.             hfPwd.Value = txtpwd.Text;  
  92.             tdlogin.Visible = false;  
  93.             BindAlbum();  
  94.         }  
  95.         else  
  96.             lblMsg.Text = "Enter Your gmail id and pwd";  
  97.     }  
  98. }   

Now build your application. And enter the Gmail id and password in the corresponding TextBox.

Click on the "Login" button. It will show all your Albums with names, number of photos and album cover photo in the grid.

Image2.jpeg

Image3.jpeg

Now click on any album cover photo to see all photos. It will show all photos in the gallery. In the gallery you can also see the slide show.

Image4.jpg

For any modifications or problems please comment.

Thank You


Similar Articles