9

I am using facebook C# sdk from codeplex and trying to download user's profile picture.

I know I can get this from:

http://graph.facebook.com/UID/picture?type=large

but this URL then posts to a second url with the actual picture. How do I get the second URL? There is a post on stackoverflow that talks about parsing json, how do I do this?

            var app = new FacebookApp();
            var me = (IDictionary<string, object>)app.Get("me");
            string firstName = (string)me["first_name"];
            string lastName = (string)me["last_name"];
            string gender = (string)me["gender"];
            string email = (string)me["email"];
            long facebook_ID = app.UserId;
Bruce
  • 2,133
  • 12
  • 34
  • 52

5 Answers5

14

You could let the browser get the image for you using the graph api url - graph.facebook.com/UID/picture?type=large or use something like the method below to get the cached url

    public static string GetPictureUrl(string faceBookId)
    {
        WebResponse response = null;
        string pictureUrl = string.Empty;
        try
        {
            WebRequest request = WebRequest.Create(string.Format("https://graph.facebook.com/{0}/picture", faceBookId));
            response = request.GetResponse();
            pictureUrl = response.ResponseUri.ToString();
        }
        catch (Exception ex)
        {
            //? handle
        }
        finally
        {
            if (response != null) response.Close();
        }
        return pictureUrl;
    }
ak7
  • 338
  • 1
  • 2
  • 7
8

Here it goes it works well i am using it

Function is

  private Image getUrlImage(string url)
        {
            WebResponse result = null;
            Image  rImage = null;
            try
            {
                WebRequest request = WebRequest.Create(url);
                result = request.GetResponse();
                Stream stream = result.GetResponseStream();
                BinaryReader br = new BinaryReader(stream);
                byte[] rBytes = br.ReadBytes(1000000);
                br.Close();
                result.Close();
                MemoryStream imageStream = new MemoryStream(rBytes, 0, rBytes.Length);
                imageStream.Write(rBytes, 0, rBytes.Length);
                rImage = Image.FromStream(imageStream, true);
                imageStream.Close();
            }
            catch (Exception c)
            {
                //MessageBox.Show(c.Message);
            }
            finally
            {
                if (result != null) result.Close();
            }
            return rImage;

        }

and its call is

profilePic = getUrlImage("https://graph.facebook.com/" + me.id + "/picture");
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138
5

You don't actually need the second URL. The second URL is just a caching url. Just use the graph.facebook.com/username/picture?type=large url. The longer caching url could change so it is not a reliable source of the image anyway.

Nate Totten
  • 8,904
  • 1
  • 35
  • 41
2

You can get the picture URL through this : graph.facebook.com/username?fields=picture

The facebook C# sdk from codeplex (v.5.0.3) throws an exception when trying to do something like this: fb.GetAsync("me/picture", get_data_callback); I guess GetAsync doesn't support to retrieve the (binary) image data.

blelem
  • 86
  • 3
0

You can get the urls for the user's picture by using FQL

dim app as new facebookclient(token)
dim obj as jsonobject = app.get("me/")
dim fql as string = "Select pic_small, pic_big, pic_square from user where uid = " + CStr(obj("id"))
dim arr as jsonarray = app.query(fql)

Facebook FQL page for user table

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
EthR
  • 1,924
  • 1
  • 12
  • 13