Twitterのプロフィール画像(アイコン)をC#で取得する方法です。あまり難しくないためか、ネット上にサンプルが少なかったので書いておきます。
Twitterのプロフィール画像を取得するためには次のAPIを使用します。
http://api.twitter.com/1/users/profile_image/[screen_name].[format]
APIの説明はこの辺りを参照してください。
GET users/profile_image/:screen_name
このAPIは特殊で、formatにxmlまたはjsonを指定しますが、返されるのはプロフィール画像へのURIです。そのため、まず画像のURIを取得し、次に画像を取得する、という2段階の処理が必要になります。
画像のURIを取得するのは、WebRequest、WebResponseを使えば良いです。認証も不要なので、下記のようにすれば実現できます。
WebRequest api_req = WebRequest.Create(
"http://api.twitter.com/1/users/profile_image/screen_name.xml");
WebResponse api_res = api_req.GetResponse();
api_res.ResponseUriが画像のURIです。このURIから画像を取得するのも、WebRequest、WebResponseで良いです。
WebRequest image_req = WebRequest.Create(api_res.ResponseUri); WebResponse image_res = image_req.GetResponse();
画像はSystem.IO.Streamを使ってBitmapに変換するのが楽です。
System.IO.Stream stream = image_res.GetResponseStream(); Bitmap bitmap = new Bitmap(stream);
後は、取得したbitmapを好きなようにすれば良いです。
また、画像のURIを取得するときに、引数としてsizeの指定が可能です。mini(小サイズ)、normal(通常サイズ)、bigger(大サイズ)を指定することができます。省略した場合はnormalの扱いになります。
簡単なサンプルプログラムを作ってみました。
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
〜省略〜
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 画像を取得するID
string screen_name = "XXXX"; // ←画像を取得したいIDを入れてください
// 大きい画像を取得
Bitmap bigger_image = GetProfileImage(screen_name,
ProfileImageSize.bigger);
// PictureBoxに設定
PictureBox bigger_picture = new PictureBox();
bigger_picture.Left = 10;
bigger_picture.Top = 10;
bigger_picture.Size = bigger_image.Size;
bigger_picture.Image = bigger_image;
this.Controls.Add(bigger_picture);
// 通常の画像を取得
Bitmap normal_image = GetProfileImage(screen_name,
ProfileImageSize.normal);
// PictureBoxに設定
PictureBox normal_picture = new PictureBox();
normal_picture.Left = bigger_picture.Right + 10;
normal_picture.Top = 10;
normal_picture.Size = normal_image.Size;
normal_picture.Image = normal_image;
this.Controls.Add(normal_picture);
// 小さい画像を取得
Bitmap mini_image = GetProfileImage(screen_name,
ProfileImageSize.mini);
// PictureBoxに設定
PictureBox mini_picture = new PictureBox();
mini_picture.Left = normal_picture.Right + 10;
mini_picture.Top = 10;
mini_picture.Size = mini_image.Size;
mini_picture.Image = mini_image;
this.Controls.Add(mini_picture);
}
// Profile画像取得APIのアドレス
private static readonly string PROFILE_IMAGE_URL =
"http://api.twitter.com/1/users/profile_image/";
/// <summary>
/// ProfileImageのサイズ
/// </summary>
public enum ProfileImageSize
{
mini = 0, /// 小
normal = 1, /// 通常
bigger = 2, /// 大
} ;
/// <summary>
/// ProfileImageを取得する
/// </summary>
/// <param name="screen_name">ProfileImageを取得するID(screen_name)</param>
/// <param name="image_size">ProfileImageのサイズ</param>
/// <returns>取得した画像</returns>
public Bitmap GetProfileImage(string screen_name,
ProfileImageSize image_size)
{
// サイズ指定テキストの定義
string[] image_size_string = { "mini", "normal", "bigger" };
// 画像のURLを取得
WebRequest api_req = WebRequest.Create(
PROFILE_IMAGE_URL + screen_name + ".xml" +
"?size=" + image_size_string[(int)image_size]);
WebResponse api_res = api_req.GetResponse();
// 画像を取得
WebRequest image_req = WebRequest.Create(api_res.ResponseUri);
WebResponse image_res = image_req.GetResponse();
// BITMAPに変換
Stream stream = image_res.GetResponseStream();
Bitmap bitmap = new Bitmap(stream);
// 後始末
stream.Close();
image_res.Close();
api_res.Close();
// BITMAPを返却
return bitmap;
}
}
私の画像だと面白みがないですね。
