2012-02-22 76 views
2

我正在努力與facebook圖片上傳。 我使用Facebook C#SDK 5.4.1從我的Silverligth 5應用程序(在瀏覽器中)上傳圖片。Facebook C#SDK拋出ArgumentNullException

身份驗證正在工作,我得到我的訪問令牌具有所需的權限。 (我存儲我的訪問令牌在cookie中,因爲我不希望我的Silverlight應用程序重新加載。)

這是我的代碼:

void img_LoadingCompleted(object sender, EventArgs e) 
    { 

     Dispatcher.BeginInvoke(new Action(() => { string fbcode = ReadCookie("FBCode"); 

     ExtendedImage img = sender as ExtendedImage; 
     string myAppId = "MY_APPID"; 
     string myAppSecret = "MY_SECRET"; 

     FacebookClient fbclient = new FacebookClient(fbcode); 
     fbclient.PostCompleted += new EventHandler<FacebookApiEventArgs>(fbclient_PostCompleted); 

     FacebookMediaObject image = new FacebookMediaObject 
     { 
      ContentType = "image/jpg", 
      FileName = "Testpic" 
     }; 
     image.SetValue(img.Pixels); 

     IDictionary<string, object> photoDetails = new Dictionary<string, object> 
      { 
       {"message", "Test"}, 
       {"source", image} 
      }; 

     fbclient.PostAsync("/me/photos", photoDetails); 
     })); 
    } 

    void fbclient_PostCompleted(object sender, FacebookApiEventArgs e) 
    { 
     if (e.Cancelled) 
     { 
      Dispatcher.BeginInvoke(new Action(() => 
      { 
       MessageBox.Show("Cancelled: "+e.Error); 
      })); 
     } 
    } 

唯一的例外是的PostCompleted事件拋出FacebookClient。

{System.ArgumentNullException: Der Wert darf nicht NULL sein. 
Parametername: stream 
    bei System.IO.StreamReader..ctor(Stream stream, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean leaveOpen) 
    bei System.IO.StreamReader..ctor(Stream stream) 
    bei Facebook.FacebookClient.ProcessResponse(HttpHelper httpHelper, Stream responseStream, Type resultType, String& responseStr, Exception& exception, Boolean& cancelled)} 

感謝您的幫助。

回答

2

我自己想出了這個問題。

如果圖片的編碼錯誤,您會得到ArgumentNullException,因爲Facebook根本沒有迴應。 我也試過solution(稍微調整了一下代碼,因爲Silverlight 5不支持WriteableBitmap.SaveJpeg()),HTTP錯誤404是我得到的響應。

如果別人encouters類似的問題(和我:-)是混淆)也許我可以用下面的解決方案(使用Facebook的C#SDK 5.4.1和Silverlight 5在瀏覽器中)幫助:

JpegEncoder JE = new JpegEncoder(); 
MemoryStream M = new MemoryStream(); 
JE.Encode(Img.ToImage(), M); 

Byte[] ImgData = new Byte[M.Length]; 
M.Seek(0, SeekOrigin.Begin); 
M.Read(ImgData, 0, (int)M.Length); 

FacebookClient fbclient = new FacebookClient(fbcode); 
fbclient.UploadProgressChanged += new EventHandler<FacebookUploadProgressChangedEventArgs>(fbclient_UploadProgressChanged); 
fbclient.PostCompleted += new EventHandler<FacebookApiEventArgs>(fbclient_PostCompleted); 

FacebookMediaObject image = new FacebookMediaObject 
{ 
    ContentType = "image/jpg", 
    FileName = "Testpic" 
}; 
image.SetValue(ImgData); 

IDictionary<string, object> photoDetails = new Dictionary<string, object> 
{ 
    {"message", "Test"}, 
    {"source", image} 
}; 

new Thread(new ParameterizedThreadStart((fbc) => { StartWait(); ((FacebookClient)fbc).PostAsync("me/photos", photoDetails); })).Start(fbclient); 

其中變量Img是一個WriteableBitmap。 對於正確的編碼,我使用的是Imagetools from Codeplex

注意:您需要有效的access_token才能使其正常工作!

+0

考慮將此標記爲答案!感謝您的跟蹤。 – 2012-02-24 03:51:03