2009年9月17日 星期四

Upload image to the picasa web album

在 C# 要使用 picasa web album data api , 在下在完 google api 後, 記得 include 以下的 dll

  • Google Data API Access Control Library
  • Google Data API Core Library
  • Google Data API Extansions Library
  • Google Data API Picasa Library

這個範例輸入 以下資訊就可以上傳 圖片到picasa,不同於google 官方範例是從檔案路徑讀取檔案在上傳 ,這裡 傳入一個 BitmapImage 即可取得 圖片的資料


註 1、 Image Type
  • image/bmp
  • image/gif
  • image/jpeg
  • image/png
註 2、 Picasa Web Albums Data API - C# delelper's guide

Code 如下





/**
* @Name UpLoadImageToWebAlbum
*
* Upload photo to the the picasa web album,
* In there, using the StreamSource get the photo byte array
*
* @param Account [in] - the google accpount
* @param Password [in] - the google accpount's password
* @param AlbumUri [in] - the google picasa web album uri
* @param PhotoID [in] - the photo id
* @param Image [in] - the image which want upload
*
*/
public void UpLoadImageToWebAlbum(string Account, string Password, string AlbumUri, String PhotoID, BitmapImage Image)
{


/// Set Service
PicasaService service = new PicasaService("UploadImag");

/// Set Account and Password
service.setUserCredentials(Account, Password);


/// Set Uri
Uri postUri = new Uri(AlbumUri);


/// Set proxy
GDataRequestFactory requestFactory = (GDataRequestFactory)service.RequestFactory;
IWebProxy iProxy = WebRequest.DefaultWebProxy;
WebProxy myProxy = new WebProxy(iProxy.GetProxy(postUri));

///potentially, setup credentials on the proxy here
myProxy.Credentials = CredentialCache.DefaultCredentials;

myProxy.UseDefaultCredentials = true;
requestFactory.Proxy = myProxy;


/// Insert photo
/// service.Insert(the album uri, image's StreamSource, image type , the image name );
service.Insert(postUri, Image.StreamSource, "image/png", "" + PhotoID);

///Insert Pic

/// Close stream
Image.StreamSource.Close();

MessageBox.Show("OK");



}



沒有留言: