问题
I'm struggling with the facebook image upload. I'm using the Facebook C# SDK 5.4.1 to upload a picture from my Silverligth 5 Application (in Browser).
Authentication is working and I get my access token with the needed permissions. (I'm storing my access token in a cookie because I don't want my Silverlight App to reload.)
This is my code:
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);
}));
}
}
The exception is thrown in the PostCompleted event of the 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)}
Thanks for your help.
回答1:
I figured out the problem myself.
If the encoding of the image is wrong you'll get the ArgumentNullException because Facebook simply doesn't not respond. I also tried this solution (tweaked the code a little bit because Silverlight 5 doesn't support WriteableBitmap.SaveJpeg()) and the HTTP Error 404 was the response I got.
If anyone else encouters a similar problem (and is confused as I was :-)) maybe I can help with the following solution (using Facebook C# SDK 5.4.1 and Silverlight 5 In-Browser):
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);
Where the variable Img is a WriteableBitmap. For the right encoding I'm using the Imagetools from Codeplex
NOTICE: You need a valid access_token to get this working!
来源:https://stackoverflow.com/questions/9392016/facebook-c-sharp-sdk-throws-argumentnullexception