我无法用图片发送 Facebook 状态。找到代码但它抛出异常
The Safe handle is closed
如果我遗漏了什么,请告诉我。还有另一个使用 File.OpenRead(filename) 的例子,但它抛出了 UnAuthorizedAccessException
代码如下:
public static Stream ImageToShare
{
set
{
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(ImageToShareKey))
myIsolatedStorage.DeleteFile(ImageToShareKey);
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(ImageToShareKey);
StreamResourceInfo sri = null;
Uri uri = new Uri(ImageToShareKey, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(value);
//bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
}
catch (Exception ex)
{
AppHelper.ErrorOccured(ex);
}
}
private void postFBWithImage()
{
try
{
using (IsolatedStorageFile myFile = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream = myFile.OpenFile("SharePhoto", FileMode.Open, FileAccess.Read))
using (var imgFile = new FacebookMediaStream
{
ContentType = "image/jpeg",
FileName = Path.GetFileName(imgPath.Text),
}.SetValue(stream))
{
var fb = new FacebookClient(AppSettings.FacebookPIN);
fb.PostCompleted += (o, args) =>
{
if (args.Error != null)
{
notPosted(args);
return;
}
Dispatcher.BeginInvoke(() =>
{
posted();
});
};
fb.PostAsync("me/photos", new { message = ShareComments, imgFile });
}
}
catch (Exception ex)
{
AppHelper.ErrorOccured(ex);
postFBWithoutImage();
}
}