Techbirds | Knowledge Hub | Page 23

private void postFBWithImage()

        {

            try

            {

                Stream objMemory = null;

                MediaLibrary mediaLibrary = new MediaLibrary();

                var pictures = mediaLibrary.SavedPictures;

                foreach (var picture in pictures)

                {

                    if (picture.Name == App.filename)

                    {

                        objMemory = picture.GetImage();

                        break;

                    }

                }

                byte[] byteArr = null;

                using (var binRdr = new BinaryReader(objMemory))

                using (var memStr = new MemoryStream())

                {

                    const int ReadSize = 8196;

                    int chunkSize = 0;

                    do

                    {

                        byte[] buf = new byte[ReadSize];

                        chunkSize = binRdr.Read(buf, 0, ReadSize);

                        memStr.Write(buf, 0, ReadSize);

                    } while (chunkSize > 0);

                    byteArr = new byte[memStr.Length];

                    memStr.Position = 0;

                    memStr.Read(byteArr, 0, (int)memStr.Length);

                }

                var fb = new FacebookClient(App.AccessToken);                      

                fb.PostCompleted += (o, args) =>

                {

                    if (args.Error != null)

                    {

                        Dispatcher.BeginInvoke(() =>

                    {

                        MessageBox.Show(“Not Posted”)

                    });

                        return;

                    }

                    Dispatcher.BeginInvoke(() =>

                    {

                         MessageBox.Show(“Posted”);

                    });

                };

                var parameters = new Dictionary();

                parameters[“message”] = “my image”;

                parameters[“file”] = new FacebookMediaObject

                {

                    ContentType = “image/jpeg”,

                    FileName = “image.jpeg”,

                }.SetValue(byteArr);

                fb.PostTaskAsync(“me/photos”, parameters);

            }

            catch (Exception ex)

            {

                //   MessageBox.Show(ex.Message);

            }

        }