Showing posts with label Google Code API For PICASA. Show all posts
Showing posts with label Google Code API For PICASA. Show all posts

June 12, 2011

Picasa Integration In ASP.NET Part2

Read Below post first where I am integrating an album from a picasa gallery
http://www.dotnetissues.com/2011/02/picasa-integration-in-aspnet-part1.html

Now Below are the changes I made to read all the albums from the gallery instead of just one.



gallery.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gallery.aspx.cs" Inherits="gallery"
    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">
    <div class="maincontent" runat ="server" id="MainDIV"> 
    </form>
</body>
</html>


gallery.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class gallery : 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)
       {
 ////Code For Multiple Albums USe NewPhotoManager
    
        string[] m_Photos;
        string[] m_Original_Photos;

        Albums m_Albums = NewPhotoManager.GetAlbums();

        int albumCount = 0;

        foreach (Album alb  in m_Albums)
        {
            albumCount++;

            Label lbl = new Label();
            lbl.Text = alb.Summary;

            //MainDIV.Controls.Add(lbl);

            System.Web.UI.HtmlControls.HtmlGenericControl h4Tag =
               new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
            //h4Tag.Attributes.Add("style", "float:left;");
            h4Tag.InnerText = lbl.Text;
            MainDIV.Controls.Add(h4Tag);

            System.Web.UI.HtmlControls.HtmlGenericControl ulTag =
               new System.Web.UI.HtmlControls.HtmlGenericControl("ul");
            ulTag.Attributes.Add("class", "portfolio-4col");
            ulTag.Attributes.Add("id", "UnOList"+albumCount);
            //ulTag.Attributes.Add("style", "float:left;");

            m_Photos = NewPhotoManager.GetPhotos(alb.Title);
            m_Original_Photos = NewPhotoManager.GetOriginalPhotos();
            
            for (int i = 0; i < m_Photos.Length; i++)
            {


                System.Web.UI.HtmlControls.HtmlGenericControl liList =
               new System.Web.UI.HtmlControls.HtmlGenericControl("li");


                /////////////////////
                System.Web.UI.HtmlControls.HtmlGenericControl dynDiv1 =
               new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                dynDiv1.Attributes.Add("class", "portfolio-blockimg3");


                System.Web.UI.HtmlControls.HtmlGenericControl dynDiv2 =
              new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                dynDiv2.Attributes.Add("class", "portfolio-imgbox3");


                System.Web.UI.HtmlControls.HtmlGenericControl dynDiv3 =
             new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                dynDiv3.Attributes.Add("class", "zoom");


                HyperLink hp = new HyperLink();

                Image img = new Image();
                img.ImageUrl = m_Photos[i];
                img.Height = new Unit (86);
                img.Width = new Unit(147);
                //hp.Attributes.Add("style", "z-index:100000000;");
                img.CssClass = "boximg-pad fade";
                hp.Controls.Add(img);
                hp.NavigateUrl = m_Original_Photos[i];
                hp.Controls.Add(img);
                hp.Attributes.Add("rel", "prettyPhoto[portfolio]");
                dynDiv3.Controls.Add(hp);
                dynDiv2.Controls.Add(dynDiv3);
                dynDiv1.Controls.Add(dynDiv2);
                liList.Controls.Add(dynDiv1);
                ulTag.Controls.Add(liList);
               
             
                
            }

            MainDIV.Controls.Add(ulTag);

            System.Web.UI.HtmlControls.HtmlGenericControl spacerDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
            spacerDiv.Attributes.Add("class", "spacer");

            MainDIV.Controls.Add(spacerDiv);
        }
    }
}

    



NewPhotoManager.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;
using System.Web.Security;

/// <summary>
/// Summary description for NewPhotoManager
/// </summary>
public class NewPhotoManager
{
    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.Access = PicasaQuery.AccessLevel.AccessPublic;

        //Below Code Can be used if you want all the albums
        PicasaFeed feed1 = PicasaService.Query(albumQuery);

        if (feed1 != null && feed1.Entries.Count > 0)
        {
            foreach (PicasaEntry entry in feed1.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);
            }
        }

    }
    //Return Thumbnail Images
    public static String[] GetPhotos(String Album_Name)
    {

        /////////////////////////
        PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri("YOUR PICASAUSERID", 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 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;

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;
 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="(Your PICASAUSERID)"/>
</appSettings>

IMPORTANT POINT: Don't put spaces in AlbumName 


Picasa Integration in ASP.NET not working on Godaddy Shared Hositng because of System.Security.SecurityException

How to integrate Picasa gallery in ASP.NET you can read below posts
http://www.dotnetissues.com/2011/02/picasa-integration-in-aspnet-part1.html

When I Uploaded Picasa Integrated ASP.NET Application i got the following error.


System.Security.SecurityException: That assembly does not allow 
partially trusted callers.



I have a Google DLL that I am using  on my website to access 
 Google Picasa Gallery , I have it in my bin directory 


The problem is below dll 's I m referring doesn't allow Partial Trust level 
Google.GData.Client.dll
Google.GData.Extensions.dll
Google.GData.Photos.dll
( I have downlaoded it from this link http://code.google.com/apis/picasaweb/code.html)

I tried to change the Trust level in web.config file.
but as I am using Godaddy shared hosting they have blocked that setting , we can not update trust
level on godaddy shared hosting .


Then I searched a bit and found a hack , I found that BlogEngine has a feature which integrates your 
BlogEngine web Site to Picasa gallery.


So instead of using above three dll's ,I downlaoded these same dll's from the below link
which has AllowPartialTrust Attribure already set
http://rtur.net/blog/post/2011/01/04/Picasa-and-PrettyPhoto-updated-for-BlogEngineNET-20.aspx

I downloaded Picasa2.zip from here , It has all 3 above dll mentioned

Now in My Picasa Integrated application I deleted the references of previous dll's and added the new
eferences , compiled App , Published App, Uploaded it on Godaddy , and it worked





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