Picasaからアルバムをダウンロードする

実際、すべては非常に簡単です。



1. Google API [Google.GData.Client.dll、Google.GData.Extensions.dll、Google.GData.Photos.dll]をダウンロードしてインストールします



2. C#で新しいプロジェクトを作成します(WinFormsを使用します)

3.メインロジックのクラスを取得しましょう。







using System;

using System.IO;

using System.Net;

using System.Windows.Forms;

using Google.GData.Client;

using Google.GData.Extensions.MediaRss;

using Google.GData.Photos;

using System.Drawing;



namespace PicasaAlbumDownloader.Classes

{

class MainLogic

{

private WebClient webClient = new WebClient();



//

public AtomEntryCollection LoadAlbumList( string Login)

{

AlbumQuery aQuery = new AlbumQuery(PicasaQuery.CreatePicasaUri(Login));

PicasaService pService = new PicasaService( "PicasaService" );

AtomFeed kResultFeed = pService.Query(aQuery);



return kResultFeed.Entries;

}



// ListView

public ListViewItem[] LoadThumbnailsForListView( string Login, string AlbumTitle)

{

ListViewItem[] listViewItems = new ListViewItem[] {};



AlbumQuery aQuery = new AlbumQuery(PicasaQuery.CreatePicasaUri(Login));

PicasaService pService = new PicasaService( "PicasaService" );

AtomFeed kResultFeed = pService.Query(aQuery);



for ( int i = 0; i < kResultFeed.Entries.Count; i++)

{

if (kResultFeed.Entries[i].Title.Text != AlbumTitle) continue ;



PicasaEntry picasaEntry = (PicasaEntry) kResultFeed.Entries[i];

AtomEntry atomEntry = kResultFeed.Entries[i];



if (!picasaEntry.IsAlbum) continue ;



AtomLinkCollection atomLinkCollection = atomEntry.Links;



if (atomLinkCollection != null )

{

if (atomLinkCollection[1] != null )

{

if (! String .IsNullOrEmpty(atomLinkCollection[1].AbsoluteUri))

{

int LastSlash =

atomLinkCollection[1].AbsoluteUri.LastIndexOf( '/' ) + 1;



listViewItems =

LoadThumbsForAlbum(Login,

atomLinkCollection[1].AbsoluteUri.Substring(LastSlash));

}

}

}



break ;

}



return listViewItems;

}



//

public static ListViewItem[] LoadThumbsForAlbum( string Login, string AlbumName)

{

AlbumQuery aQuery = new AlbumQuery(PicasaQuery.CreatePicasaUri(Login, AlbumName));

PicasaService pService = new PicasaService( "PicasaService" );

aQuery.KindParameter = "" ;



AtomFeed kResultFeed = pService.Query(aQuery);



ListViewItem[] listViewItems = new ListViewItem[kResultFeed.Entries.Count];

for ( int i = 0; i < kResultFeed.Entries.Count; i++)

{

PicasaEntry entry = kResultFeed.Entries[i] as PicasaEntry;



if (entry != null )

{

PicasaEntry picasaEntry = kResultFeed.Entries[i] as PicasaEntry;

AtomEntry atomEntry = kResultFeed.Entries[i];



string PictureTitle = atomEntry.Title.Text;

ThumbnailCollection thumbnailCollection = entry.Media.Thumbnails;

MediaThumbnail Picture = thumbnailCollection[0];



string BigPhoto = Picture.Attributes[ "url" ].ToString();

if (picasaEntry != null )

{

if (picasaEntry.IsPhoto)

{

BigPhoto = picasaEntry.Content.Src.Content;

}

}



listViewItems[i] = new ListViewItem(PictureTitle, BigPhoto);

}

}



return listViewItems;

}



// PictureBox

public Image DownloadPicture( string PictureUrl)

{

Image image = null ;

webClient = new WebClient();



Stream PictureRaw = webClient.OpenRead(PictureUrl);

image = Image.FromStream(PictureRaw);



PictureRaw.Close();

PictureRaw.Dispose();



return image;

}



/// <summary>

/// .

/// </summary>

/// <param name="FromUrl">From URL.</param>

/// <param name="FullName">The full name.</param>

public void DownloadFile( string FromUrl, string FullName)

{

if ( File .Exists(FullName)) File .Delete(FullName);

if ( File .Exists(FullName)) return ;



webClient = new WebClient();

webClient.DownloadFile( new Uri (FromUrl), FullName);

webClient.Dispose();

}



// ,

public static bool CheckIntArrayOfDownloads( int [] Array)

{

for ( int i = 0; i < Array.Length; i++)

{

if (Array[i] == 0) return true ;

}



return false ;

}



//

public static string GetFullName( string DownloadPath, string album_image_to_parse)

{

int LastSlash = album_image_to_parse.LastIndexOf( '/' ) + 1;

string FileName = album_image_to_parse.Substring(LastSlash);

string FullName = String .Concat(DownloadPath, @"\", FileName);



return FullName;

}

}

}



* This source code was highlighted with Source Code Highlighter .








4.メインウィンドウのフォームを作成します。



私が使用するフォームでは:



txtLogin-テキストフィールド

btnLogin-アルバムリストをダウンロードするためのボタン

pctTitle-タイトルを表示するラベル

album_list-アルバムのリストを表示するためのリストボックス

pictureBox1-画像を表示する

albumThumbs-サムネイルを表示するための紅葉ですが、今のところはファイル名のみをリストしています。



pictureMenu(写真にハングアップ)およびalbumMenu(アルバムのリストにハングアップ)-コンテキストメニュー。



5.フォームコード:



using System;

using System.Collections. Generic ;

using System.Drawing;

using System.IO;

using System.Windows.Forms;

using Google.GData.Client;

using PicasaAlbumDownloader.Classes;



namespace PicasaAlbumDownloader

{

public partial class MainForm : Form

{

private readonly MainLogic MainLogic = new MainLogic();

private Image CurrentPhoto { get ; set ; }

private readonly List < String > album_big_images = new List < string >();



public MainForm()

{

InitializeComponent();

}



//

private void btnLogin_Click( object sender, EventArgs e)

{

AtomEntryCollection AlbumListAtomFormat = null ;



//

try

{

AlbumListAtomFormat = MainLogic.LoadAlbumList(txtLogin.Text);

}

catch (GDataRequestException)

{

MessageBox.Show( " " );

}



//

if (AlbumListAtomFormat == null ) return ;



try

{

album_list.Items.Clear();



//

album_list.BeginUpdate();



foreach (AtomEntry entry in AlbumListAtomFormat)

{

album_list.Items.Add(entry.Title.Text);

}



album_list.EndUpdate();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}



}



//

private void album_list_SelectedIndexChanged( object sender, EventArgs e)

{

albumThumbs.Clear();

album_big_images.Clear();



ListViewItem[] listViewItems =

MainLogic.LoadThumbnailsForListView(txtLogin.Text,

album_list.SelectedItem.ToString());



// ....

foreach (ListViewItem listViewItem in listViewItems)

{

album_big_images.Add(listViewItem.ImageKey);

}



//

albumThumbs.Items.AddRange(listViewItems);

}



//

private void albumThumbs_SelectedIndexChanged( object sender, EventArgs e)

{

if (albumThumbs.SelectedItems.Count == 0) return ;



string PictureTitle = albumThumbs.SelectedItems[0].Text;

string PictureUrl = albumThumbs.SelectedItems[0].ImageKey;



pctTitle.Text = PictureTitle;

Image TempImage = MainLogic.DownloadPicture(PictureUrl);



if (TempImage != null )

{

CurrentPhoto = TempImage;

pictureBox1.Image = TempImage;

}

else

{

MessageBox.Show( " " );

}

}



//

private void mnuPictureboxSave_Click( object sender, EventArgs e)

{

if (CurrentPhoto == null ) return ;



SaveFileDialog saveFileDialog = new SaveFileDialog

{

DefaultExt = "jpg" ,

Filter = "Jpeg File (*.jpg)|*.jpg)"

};



DialogResult SaveDialog = saveFileDialog.ShowDialog( this );

if (SaveDialog != System.Windows.Forms.DialogResult.OK) return ;

if ( File .Exists(saveFileDialog.FileName)) File .Delete(saveFileDialog.FileName);



CurrentPhoto.Save(saveFileDialog.FileName);

}



//

private void menuDownloadAllAlbum_Click( object sender, EventArgs e)

{

FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

DialogResult dialogResult = folderBrowserDialog.ShowDialog( this );



if (DialogResult.OK != dialogResult) return ;



string DownloadPath = folderBrowserDialog.SelectedPath;

int [] DownloadedFiles = new int [album_big_images.Count];



for ( int i = 0; i < album_big_images.Count; i++)

{

string FullName = MainLogic.GetFullName(DownloadPath, album_big_images[i]);

MainLogic.DownloadFile(album_big_images[i], FullName);

DownloadedFiles[i] = 0;

}



do

{

for ( int i = 0; i < album_big_images.Count; i++)

{

string FullName = MainLogic.GetFullName(DownloadPath, album_big_images[i]);

if ( File .Exists(FullName)) DownloadedFiles[i] = 1;

}



Application.DoEvents();

}

while (MainLogic.CheckIntArrayOfDownloads(DownloadedFiles));



MessageBox.Show( " !" );

}



//

private void pictureMenu_Opening( object sender, System.ComponentModel.CancelEventArgs e)

{

mnuPictureboxSave.Enabled = CurrentPhoto != null ;

}



//

private void albumMenu_Opening( object sender, System.ComponentModel.CancelEventArgs e)

{

albumMenu.Enabled = albumThumbs.SelectedItems.Count != 0;

}

}

}



* This source code was highlighted with Source Code Highlighter .








PS



ログインテキストボックスはむしろギャラリーであり、

私がプロジェクトを行ったとき、ログインが必要だと思いました。



プロジェクトソースをダウンロードする



All Articles