February 10, 2011

Picasa Integration In ASP.NET Part1

Integrate the photo Album Uploaded in Picasa in ASP.NET Application.
1.) Download Client Libraries For .NET Provided by Google that use the Picasa Web Albums Data API.from the below Link
http://code.google.com/apis/picasaweb/code.html
It will install the sdk on below path
C:\Program Files\Google\Google Data API SDK




2.) Create ASP.NET application and From bin Add refrences of
Google.GData.Client.dll
Google.GData.Extensions.dll
Google.GData.Photos.dll


from the below path
C:\Program Files\Google\Google Data API SDK\Redist


3.) Write The code for Integrating Picasa Album Into ASP.NET Application


Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PicasaWebIntegration._Default"
    Theme="Main" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Picasa Web Integration</title>
    <link rel="stylesheet" href="css/vlightbox1.css" type="text/css" /> 
    <link rel="stylesheet" href="css/global.css" type="text/css" /> 
<link rel="stylesheet" href="css/visuallightbox.css" type="text/css" media="screen" /> 
    <script src="js/jquery.min.js" type="text/javascript"></script> 
    <script src="js/visuallightbox.js" type="text/javascript"></script> 
    <script src="js/vlbdata.js" type="text/javascript"></script> 
</head>
<body>
    <form id="form1" runat="server">
   
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace PicasaWebIntegration
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


          
            if (Request.QueryString["reset"] == "y")
                PhotoManager.Reset();
        }


        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {


            string[] m_Photos;
            string[] m_Original_Photos;
            PhotoManager.GetAlbums();
            m_Photos = PhotoManager.GetPhotos();
            m_Original_Photos = PhotoManager.GetOriginalPhotos();
            for (int i = 0; i < m_Photos.Length; i++)
            {
                HyperLink hp = new HyperLink();
                
                Image img = new Image();
                img.ImageUrl = m_Photos[i];
                hp.Controls.Add(img);
                hp.NavigateUrl = m_Original_Photos[i];
                hp.CssClass = "vlightbox";
                form1.Controls.Add(hp);


                Literal lit = new Literal();
                lit.Text = "                 ";
                form1.Controls.Add(lit);
                
            }
           
        }


        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }


       




    }
}

PhotoManager.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using Google.GData.Photos;


namespace PicasaWebIntegration
{
    public class PhotoManager
    {
        private static Albums m_albums;
        private static PicasaService m_picasaService;
        private static String[] myImages;
        private static String[] myThumnailsImages;


        public static PicasaService PicasaService
        {
            get
            {
                if (m_picasaService == null)
                    m_picasaService = new PicasaService("PhotoBrowser");
                return m_picasaService;
            }
        }


        public static bool TryGetAlbum(string id, out Album album)
        {
            if (m_albums == null)
                InitializeAlbums();
            return m_albums.TryGetItem(id, out album);
        }


        public static Albums GetAlbums()
        {
            if (m_albums == null)
                InitializeAlbums();
            return m_albums;
        }


        public static void Reset()
        {
            InitializeAlbums();
        }


        private static void InitializeAlbums()
        {
            m_albums = new Albums();


            AlbumQuery albumQuery = new AlbumQuery();
           albumQuery.Uri = new Uri(PicasaQuery.CreatePicasaUri(ConfigurationManager.AppSettings.Get("PicasaWebUserId")));
           // AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri("btilokani@gmail.com"));
            albumQuery.Access = PicasaQuery.AccessLevel.AccessPublic;


            //PicasaFeed feed = PicasaService.Query(albumQuery);


            /* if (feed != null && feed.Entries.Count > 0)
            {
                foreach (PicasaEntry entry in feed.Entries)
                {
                    Album album = new Album();
                    album.Title = entry.Title.Text;
                    album.Summary = entry.Summary.Text.Replace("\r\n", "<br/>");
                    album.FeedUri = entry.FeedUri;
                    album.ThumbnailUrl = entry.Media.Thumbnails[0].Attributes["url"].ToString();
                    album.NumberOfPhotos = ((GPhotoNumPhotos)entry.ExtensionElements[5]).IntegerValue;


                    m_albums.Add(album);
                }
            } */


            PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri("PICASA USER ID", "ALBUM NAME"));
            query.Access = PicasaQuery.AccessLevel.AccessPublic;


            PicasaFeed feed = PicasaService.Query(query);


            myImages = new String[feed.Entries.Count];
            myThumnailsImages = new String[feed.Entries.Count];
            int i = 0;


            foreach (PicasaEntry entry in feed.Entries)
            {
                //Console.WriteLine(entry.Title.Text);
                //PhotoAccessor ac = new PhotoAccessor(entry);
                String url = entry.Content.AbsoluteUri;
                myImages[i] = url;
               
                
                String thumbUrl=entry.Media.Thumbnails[0].Attributes["url"].ToString(); 
                myThumnailsImages[i]=thumbUrl;
                 i++;
            }


        }
        //Return Thumbnail Images
        public static String[] GetPhotos()
        {
            return myThumnailsImages;
        }


        //Return Original Images
        public static String[] GetOriginalPhotos()
        {
            return myImages;
        }
    }
}

Albums.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.GData.Photos;
using System.Collections.ObjectModel;


namespace PicasaWebIntegration
{
    public class Albums: KeyedCollection<string, Album>
    {
        protected override string GetKeyForItem(Album item)
        {
            return item.FeedUri;
        }


        public bool TryGetItem(string feedUri, out Album matchingAlbum)
        {
            matchingAlbum = null;
            if (Dictionary != null && Dictionary.TryGetValue(feedUri, out matchingAlbum))
                return true;
            else
                return false;
        }
    }
}


Album.CS
using System.Collections.Generic;

using System;


namespace PicasaWebIntegration
{
    public class Album
    {
        private string m_feedUri;
        private string m_title;
        private string m_summary;
        private string m_thumbnailUrl;
        private int m_count;


        public string Title
        {
            get { return m_title; }
            set { m_title = value; }
        }


        public string FeedUri
        {
            get { return m_feedUri; }
            set { m_feedUri = value; }
        }


        public string ThumbnailUrl
        {
            get { return m_thumbnailUrl; }
            set { m_thumbnailUrl = value; }
        }


        public string Summary
        {
            get
            {
                return m_summary;
            }
            set
            {
                m_summary = value;
            }
        }


        public int NumberOfPhotos
        {
            get
            {
                return m_count;
            }
            set
            {
                m_count = value;
            }
        }
    }
}


add below in web.config file

web.config
<appSettings>
<add key="PicasaWebUserId" value="PICASAUSERID"/>
</appSettings>


I give credit for this Post to Alano's Blog(http://aocampo.com/blog/index.php) AS I have taken help from that blog 
and modified it according to my need

Below is The 0/p


Next Part Is Instead of getting one album , Fetch all the albums form Picasa Gallery
 Picasa Integration In ASP.NET Part2
on the below link
http://www.dotnetissues.com/2011/06/picasa-integration-in-aspnet-part2.html

1 comment:

  1. Hiii...

    I am getting this error
    Execution of request failed: https://picasaweb.google.com/data/feed/api/user/prema8885@gmail.com/albumid/Hiiiiiiiiii?kind=photo&access=public


    while getting photos from album

    any solution for this?

    ReplyDelete