title: How to get Facebook profile Picture C# .Net
body: <p>I need to get the user´s facebook profile picture and input it in a crop structure. I´m using Asp.NET MVC, jcrop and the facebook SDK. Untill now i can input files from my computer. I also have a function that access the facebook of the user and returns a session with the user Id, and a GetPhoto function that should return the profile picture. Can someone help me?</p>
  
  <p>I use this code to input the files from the computer:</p>
  
  <pre><code>[ValidateAntiForgeryToken]
  public ActionResult _Upload(IEnumerable&lt;HttpPostedFileBase&gt; files)
  {
      if (files == null || !files.Any()) return Json(new { success = false, errorMessage = "No file uploaded." });
      var file = files.FirstOrDefault();  // get ONE only
      if (file == null || !IsImage(file)) return Json(new { success = false, errorMessage = "File is of wrong format." });
      if (file.ContentLength &lt;= 0) return Json(new { success = false, errorMessage = "File cannot be zero length." });
      var webPath = GetTempSavedFilePath(file);
      //mistertommat - 18 Nov '15 - replacing '\' to '//' results in incorrect image url on firefox and IE,
      //                            therefore replacing '\\' to '/' so that a proper web url is returned.            
      return Json(new { success = true, fileName = webPath.Replace("\\", "/") }); // success
  }
  </code></pre>
  
  <p>i tried doing this but the GetPhoto() is returning a null element.</p>
  
  <pre><code> public ActionResult RetornoFb()
          {
              var _fb = new FacebookClient();
              FacebookOAuthResult oauthResult;
  
              if (!_fb.TryParseOAuthCallbackUrl(Request.Url, out oauthResult))
              {
              // Error
          }
  
          if (oauthResult.IsSuccess)
          {
              dynamic parameters = new ExpandoObject();
              parameters.client_id = id;
              parameters.redirect_uri = "http://localhost:4323/Avatar/RetornoFb/";
              parameters.client_secret = secretkey;
              parameters.code = oauthResult.Code;
  
              dynamic result = _fb.Get("/oauth/access_token", parameters);
  
              var accessToken = result.access_token;
              Session.Add("FbUserToken", accessToken);
          }
          else
          {
          }
  
          //return RedirectToAction("Upload");
          HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(GetPhoto());
          var webPath = GetTempSavedFilePath(objFile);
  
          return Json(new { success = true, fileName = webPath.Replace("\\", "/") }); // success
      }
  
  public byte[] GetPhoto()
          {
              try
              {
                  string url = "https://graph.facebook.com/" + GetProfileId() + "?fields=picture.width(480).height(480)";
  
                  WebClient webClient = new WebClient();
                  string response = webClient.DownloadString(url);
  
                  dynamic json = JObject.Parse(response);
  
                  string urlPicture = json.picture.data.url;
  
                  return webClient.DownloadData(urlPicture);
              }
              catch (Exception)
              {
                  return null;
              }
          }
  </code></pre>