Пример #1
0
 private FaceRestAPI.FaceAPI Parse(string text)
 {
     FaceRestAPI.FaceAPI fd = null;
     if (this.format == "json")
     {
         fd         = Newtonsoft.Json.JsonConvert.DeserializeObject <FaceRestAPI.FaceAPI>(text);
         fd.rawData = text;
     }
     if (this.format == "xml")
     {
         //fd = DeserializeObject<FaceRestAPI.FaceAPI>(result);
         fd = new FaceRestAPI.FaceAPI {
             rawData = text
         };
     }
     return(fd);
 }
Пример #2
0
        private void ExecuteTask(object state)
        {
            CloudQueueMessage queueMessage = (CloudQueueMessage)state;

            this.Log.DebugFormat("Executing render task {0}", queueMessage.AsString);
            Data.Comic comic = null;
            RenderTask task  = null;

            string            connectionString = ConfigurationManager.ConnectionStrings["ComicModelContext"].ConnectionString;
            ComicModelContext entityContext    = new ComicModelContext(connectionString);

            try
            {
                // Read task details from storage
                CloudBlobContainer container = this.BlobClient.GetContainerReference(ComicConfigSectionGroup.Blob.TaskContainer);
                CloudBlobDirectory directory = container.GetDirectoryReference(ComicConfigSectionGroup.Blob.RenderTaskDirectory);
                CloudBlob          blob      = directory.GetBlobReference(queueMessage.AsString);

                XmlSerializer serializer = new XmlSerializer(typeof(RenderTask));

                using (MemoryStream stream = new MemoryStream())
                {
                    blob.DownloadToStream(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    task        = (RenderTask)serializer.Deserialize(stream);
                    task.Status = TaskStatus.Executing;
                    this.UpdateTask(task);
                }

                User user = entityContext.TryGetUser(task.OwnerUid);

                FacebookClient facebook = new FacebookClient(task.FacebookToken);

                // Get template
                Template            template      = entityContext.ListTemplates().First(t => t.TemplateId == task.TemplateId);
                List <TemplateItem> templateItems = template.TemplateItems.OrderBy(t => t.Ordinal).ToList();

                List <TextBubbleDirection> bubbles = entityContext.ListTextBubbles().SelectMany(b => b.TextBubbleDirections).ToList();

                TextBubble speechBubble = entityContext.ListTextBubbles().First(b => b.Title == "speech");
                TextBubble bubbleShout  = entityContext.ListTextBubbles().First(b => b.Title == "shout");
                TextBubble squareBubble = entityContext.ListTextBubbles().First(b => b.Title == "square");

                comic = new Data.Comic();
                entityContext.AddToComics(comic);

                comic.Author      = user;
                comic.Template    = template;
                comic.CreateTime  = DateTime.Now;
                comic.UpdateTime  = DateTime.Now;
                comic.PublishTime = null;
                comic.FeatureTime = null;
                comic.Title       = "";
                comic.Description = "";
                comic.ShareText   = "";
                comic.IsPublished = false;
                comic.IsPrivate   = false;
                comic.IsDeleted   = false;
                comic.Locale      = user.Locale ?? "en-US";
                comic.StorageKey  = Guid.NewGuid().ToString();

                if (task.RemixComicId.HasValue)
                {
                    comic.RemixedComic = entityContext.TryGetComic(task.RemixComicId.Value, user);
                }

                // Comic generator only used to size text
                ComicGenerator generator = new ComicGenerator(template.Width, template.Height);

                // Render effect parameters
                Dictionary <string, object> parameterValues = new Dictionary <string, object>();

                switch (task.Effect)
                {
                case ComicEffectType.ColorSketch:
                case ComicEffectType.PencilSketch:
                    parameterValues.Add("edging", 2);
                    parameterValues.Add("coloring", 35);
                    break;

                case ComicEffectType.Comic:
                default:
                    parameterValues.Add("coloring", 6);
                    break;
                }

                // Get photos for each frame
                for (int f = 0; f < task.Frames.Count && f < templateItems.Count; f++)
                {
                    Photo  photo    = null;
                    Bitmap image    = null;
                    string imageUrl = String.Empty;
                    ComicGenerator.ImageAlign imageAlignment = ComicGenerator.ImageAlign.Center;
                    Point tag          = Point.Empty;
                    bool  tagConfident = false;

                    if (task.PhotoSource == "Internal" && task.Frames[f].PhotoId.HasValue)
                    {
                        // Load image from database
                        photo           = entityContext.TryGetPhoto(task.Frames[f].PhotoId.Value);
                        photo.ImageData = this.GetStoredImage(photo.StorageKey);
                        image           = new Bitmap(new MemoryStream(photo.ImageData));
                    }
                    else
                    {
                        // Tagged facebook photos
                        if (task.PhotoSource == "Tagged")
                        {
                            try
                            {
                                // List photos of the user
                                Dictionary <string, object> args = new Dictionary <string, object>();
                                args.Add("limit", "50");

                                dynamic photoResult = facebook.Get(String.Format("/{0}/photos", task.Frames[f].Id), args);

                                if (photoResult.data.Count > 0)
                                {
                                    // Pick a random photo with 2 or fewer tags
                                    dynamic photoData = ((IList <dynamic>)photoResult.data)
                                                        .OrderBy(p => Guid.NewGuid())
                                                        .FirstOrDefault(p => p.tags.data.Count <= 2);

                                    if (photoData != null)
                                    {
                                        imageUrl = (string)photoData.source;

                                        // Look for user tag location
                                        int     id;
                                        dynamic tagData = ((IList <dynamic>)photoData.tags.data)
                                                          .FirstOrDefault(t => int.TryParse(t.id, out id) && id == task.Frames[f].Id);

                                        if (tagData != null)
                                        {
                                            tag          = new Point((int)Math.Round((double)tagData.x), (int)Math.Round((double)tagData.y));
                                            tagConfident = false;
                                        }
                                    }
                                }
                            }
                            catch (Exception x)
                            {
                                this.Log.Error("Unable to retrieve tagged photo from facebook.", x);
                            }
                        }

                        // Look for any photo of the user
                        else if (task.PhotoSource == "Any")
                        {
                            try
                            {
                                FaceRestAPI         faceApi   = this.CreateFaceApi(task.FacebookToken, user.Uid);
                                List <string>       ids       = new List <string>(new string[] { String.Format("{0}@facebook.com", task.Frames[f].Id) });
                                FaceRestAPI.FaceAPI anyResult = faceApi.facebook_get(ids, null, "1", null, "random");

                                if (anyResult.status == "success" && anyResult.photos.Count > 0)
                                {
                                    FaceRestAPI.Photo p = anyResult.photos[0];
                                    imageUrl     = p.url;
                                    tag          = new Point((int)Math.Round(p.tags.First().mouth_center.x), (int)Math.Round(p.tags.First().mouth_center.y));
                                    tagConfident = true;
                                }
                            }
                            catch (Exception x)
                            {
                                this.Log.Error("Unable to retrieve photo through face.com api.", x);
                            }
                        }

                        // Use profile photo as backup image
                        if (String.IsNullOrEmpty(imageUrl))
                        {
                            imageUrl = String.Format("https://graph.facebook.com/{0}/picture?access_token={1}&type=large", task.Frames[f].Id, facebook.AccessToken);
                        }

                        image = this.GetImage(imageUrl);


                        // Find faces when confidence in tag location is low
                        if (!tagConfident)
                        {
                            try
                            {
                                FaceRestAPI tagApi = this.CreateFaceApi(task.FacebookToken, user.Uid);
                                //List<string> tagIds = new List<string>(new string[] { String.Format("{0}@facebook.com", task.Frames[f].Id) });
                                List <string>       urls      = new List <string>(new string[] { imageUrl });
                                FaceRestAPI.FaceAPI tagResult = tagApi.faces_detect(urls, null, "Normal", null, null);

                                if (tagResult.status == "success" && tagResult.photos.Count > 0 && tagResult.photos[0].tags.Count > 0)
                                {
                                    FaceRestAPI.Tag t = tagResult.photos[0].tags.First();
                                    tag          = new Point((int)Math.Round(t.mouth_center.x), (int)Math.Round(t.mouth_center.y));
                                    tagConfident = true;
                                }
                            }
                            catch (Exception x)
                            {
                                this.Log.Error("Unable to detected faces.", x);
                            }
                        }

                        if (tag != Point.Empty && tag.Y <= image.Height / 3)
                        {
                            imageAlignment = ComicGenerator.ImageAlign.Top;
                        }
                    }

                    // Resize to fit frame
                    image = ComicGenerator.FitImage(new Size(templateItems[f].Width, templateItems[f].Height), image);

                    // Apply render effect
                    if (task.Effect != ComicEffectType.None)
                    {
                        RenderHelper    effectHelper = new RenderHelper(image.Size);
                        ImageRenderData renderResult = effectHelper.RenderEffect(image, task.Effect, parameterValues);
                        image = new Bitmap(renderResult.RenderStream);
                    }

                    // Read raw photo into memory
                    MemoryStream imageStream = new MemoryStream();
                    image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imageStream.Seek(0, SeekOrigin.Begin);


                    // Frame text bubbles
                    if (!String.IsNullOrWhiteSpace(task.Frames[f].Message))
                    {
                        ComicTextBubble comicBubble = new ComicTextBubble();
                        entityContext.AddToComicTextBubbles(comicBubble);
                        comicBubble.Comic = comic;
                        comicBubble.Text  = task.Frames[f].Message;

                        // Remove newlines
                        comicBubble.Text = comicBubble.Text.Replace('\n', ' ');

                        // Font size
                        int fontSize = 7;
                        if (comicBubble.Text.Length > 160)
                        {
                            fontSize = 6;
                        }
                        if (comicBubble.Text.Length > 200)
                        {
                            fontSize = 5;
                        }
                        comicBubble.Font = new Font(ComicGenerator.ComicFont, fontSize, FontStyle.Regular, GraphicsUnit.Point);

                        // Shouting / excited?
                        TextBubble bubble = speechBubble;
                        if (comicBubble.Text.Contains('!') || Regex.Matches(comicBubble.Text, "[A-Z]").Count > comicBubble.Text.Length / 4)
                        {
                            bubble = bubbleShout;
                        }

                        // Calculate tag x/y coords relative to the whole comic
                        if (tag != Point.Empty)
                        {
                            Size      templateSize = new Size(templateItems[f].Width, templateItems[f].Height);
                            Rectangle cropArea     = ComicGenerator.GetCropImageSize(image.Size, templateSize, imageAlignment);
                            tag.X = image.Size.Width * tag.X / 100 - cropArea.X + templateItems[f].X;
                            tag.Y = image.Size.Height * tag.Y / 100 - cropArea.Y + templateItems[f].Y;
                        }

                        // Position text bubble
                        this.PositionFrameBubble(comicBubble, image, generator, bubble, squareBubble, templateItems[f], tag, imageAlignment);

                        // Add photo as template item
                        photo            = new Photo();
                        photo.User       = user;
                        photo.CreateTime = DateTime.Now;
                        photo.ImageData  = imageStream.ToArray();
                        photo.StorageKey = Guid.NewGuid().ToString();
                        photo.Width      = image.Width;
                        photo.Height     = image.Height;
                        entityContext.AddToPhotos(photo);
                    }

                    // Tag users
                    //if (task.Frames[f].Id > 0)
                    //{
                    //    try
                    //    {

                    //        // Lookup existing user
                    //        User taggedUser = entityContext.TryGetUser(task.Frames[f].Id, true);
                    //        if (taggedUser == null)
                    //        {
                    //            // User doesn't exist in the db yet - grab from facebook
                    //            dynamic facebookUser = facebook.Get(String.Format("/{0}", task.Frames[f].Id));
                    //            taggedUser = new User();
                    //            taggedUser.Uid = long.Parse(facebookUser.id);
                    //            taggedUser.IsDeleted = false;
                    //            taggedUser.IsSubscribed = false;
                    //            taggedUser.Locale = facebookUser.locale;
                    //            taggedUser.Name = facebookUser.name;
                    //            taggedUser.Nickname = facebookUser.name;
                    //            taggedUser.FbLink = facebookUser.link;
                    //            entityContext.AddToUsers(taggedUser);
                    //        }

                    //        ComicTag comicTag = new ComicTag();
                    //        comicTag.User = taggedUser;
                    //        comicTag.Comic = comic;
                    //        if (tag != Point.Empty)
                    //        {
                    //            comicTag.X = tag.X;
                    //            comicTag.Y = tag.Y;
                    //        }
                    //    }
                    //    catch (Exception x)
                    //    {
                    //        this.Log.ErrorFormat("Failed to tag user {0} in comic. {1}", task.Frames[f].Id, x.ToString());
                    //    }
                    //}


                    ComicPhoto comicPhoto = new ComicPhoto();
                    comicPhoto.Comic        = comic;
                    comicPhoto.Photo        = photo;
                    comicPhoto.TemplateItem = templateItems[f];
                    comicPhoto.Alignment    = imageAlignment;
                    comic.ComicPhotos.Add(comicPhoto);

                    // Update task progress
                    task.CompletedOperations++;
                    this.UpdateTask(task);
                }

                for (int b = 0; task.Bubbles != null && b < task.Bubbles.Count; b++)
                {
                    ComicTextBubble comicBubble = new ComicTextBubble();
                    entityContext.AddToComicTextBubbles(comicBubble);
                    comicBubble.Comic = comic;
                    comicBubble.Text  = task.Bubbles[b].Text;
                    comicBubble.Font  = new Font(ComicGenerator.ComicFont, 7, FontStyle.Regular, GraphicsUnit.Point);
                    comicBubble.TextBubbleDirection = bubbles.First(d => d.TextBubbleDirectionId == task.Bubbles[b].TextBubbleDirectionId);
                    comicBubble.Position            = new Rectangle(new Point(task.Bubbles[b].X, task.Bubbles[b].Y), generator.MeasureText(comicBubble.Text, comicBubble.Font).ToSize());
                }

                // Fix for position to x,y coordinates
                foreach (ComicTextBubble b in comic.ComicTextBubbles)
                {
                    b.X = b.Position.X;
                    b.Y = b.Position.Y;
                }

                this.SaveComic(comic, entityContext);

                task.CompletedOperations = task.TotalOperations;
                task.Status  = TaskStatus.Complete;
                task.ComicId = comic.ComicId;
                this.UpdateTask(task);
                this.Log.DebugFormat("Completed render task {0}", task.TaskId);
            }
            catch (Exception x)
            {
                this.Log.Error("Unable to complete render task.", x);

                if (task != null)
                {
                    task.Status = TaskStatus.Failed;
                    this.UpdateTask(task);
                }

                if (comic != null)
                {
                    this.Log.DebugFormat("Text bubble info [{0}] [{1}]",
                                         String.Join(",", comic.ComicTextBubbles.Select(b => b.ComicTextBubbleId.ToString()).ToArray()),
                                         String.Join(",", comic.ComicTextBubbles.Select(b => b.TextBubbleDirectionId.ToString()).ToArray()));
                }
            }
        }
Пример #3
0
    private FaceAPI post_method(string request, Dictionary<string, string> param)
    {
        string result = "";
        string url = this.api_server + request;
        string paramQS = http_build_query(param);

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        req.Method = "POST";

        string filename;
        if (param.TryGetValue("_file", out filename))
        {
            string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            req.ContentType = "multipart/form-data; boundary=" + boundary;
            req.KeepAlive = true;

            req.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Stream memStream = new System.IO.MemoryStream();
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

            foreach (KeyValuePair<string, string> s in param)
            {
                string formitem = string.Format(formdataTemplate, s.Key, s.Value);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }

            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"_file\"; filename=\"_files\"\r\n Content-Type: application/octet-stream\r\n\r\n";

            string header = string.Format(headerTemplate, "file1", filename);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            memStream.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(filename.Replace("@", ""), FileMode.Open,
            FileAccess.Read);
            byte[] buffer = new byte[1024];

            int bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }

            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            fileStream.Close();

            req.ContentLength = memStream.Length;
            Stream requestStream = req.GetRequestStream();

            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            result = reader.ReadToEnd();

            response.Close();
            response = null;
            reader.Close();
            reader.Dispose();
        }
        else
        {
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = paramQS.Length;

            req.GetRequestStream().Write(Encoding.UTF8.GetBytes(paramQS), 0, paramQS.Length);
            req.GetRequestStream().Close();
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            result = reader.ReadToEnd();
            response.Close();
            response = null;
            reader.Close();
            reader.Dispose();
        }
        req = null;

        FaceRestAPI.FaceAPI fd = null;
        if (this.format == "json")
        {
           // JavaScriptSerializer jss = new JavaScriptSerializer();
            var jo = JObject.Parse(result);
            fd = JsonConvert.DeserializeObject<FaceRestAPI.FaceAPI>(result);
            //fd = jss.Deserialize<FaceRestAPI.FaceAPI>(result);
            fd.rawData = result;
        }
        if (this.format == "xml")
        {
            //fd = DeserializeObject<FaceRestAPI.FaceAPI>(result);
            fd = new FaceRestAPI.FaceAPI();
            fd.rawData = result;
        }
        return fd;
    }
Пример #4
0
    private FaceAPI post_method(string request, Dictionary <string, string> param)
    {
        string result  = "";
        string url     = this.api_server + request;
        string paramQS = http_build_query(param);

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        req.Timeout = this.timeout;
        req.Method  = "POST";

        string filename;

        if (param.TryGetValue("_file", out filename))
        {
            string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            req.ContentType = "multipart/form-data; boundary=" + boundary;
            req.KeepAlive   = true;

            req.Credentials = System.Net.CredentialCache.DefaultCredentials;



            Stream memStream     = new System.IO.MemoryStream();
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");


            string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

            foreach (KeyValuePair <string, string> s in param)
            {
                string formitem      = string.Format(formdataTemplate, s.Key, s.Value);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }



            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"_file\"; filename=\"_files\"\r\n Content-Type: application/octet-stream\r\n\r\n";

            string header      = string.Format(headerTemplate, "file1", filename);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            memStream.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(filename.Replace("@", ""), FileMode.Open,
                                                   FileAccess.Read);
            byte[] buffer = new byte[1024];

            int bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }


            memStream.Write(boundarybytes, 0, boundarybytes.Length);


            fileStream.Close();

            req.ContentLength = memStream.Length;
            Stream requestStream = req.GetRequestStream();

            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();


            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader    reader   = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            result = reader.ReadToEnd();

            response.Close();
            response = null;
            reader.Close();
            reader.Dispose();
        }
        else
        {
            req.ContentType   = "application/x-www-form-urlencoded";
            req.ContentLength = paramQS.Length;

            req.GetRequestStream().Write(Encoding.UTF8.GetBytes(paramQS), 0, paramQS.Length);
            req.GetRequestStream().Close();
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader    reader   = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            result = reader.ReadToEnd();
            response.Close();
            response = null;
            reader.Close();
            reader.Dispose();
        }
        req = null;

        FaceRestAPI.FaceAPI fd = null;
        if (this.format == "json")
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            fd         = jss.Deserialize <FaceRestAPI.FaceAPI>(result);
            fd.rawData = result;
        }
        if (this.format == "xml")
        {
            //fd = DeserializeObject<FaceRestAPI.FaceAPI>(result);
            fd         = new FaceRestAPI.FaceAPI();
            fd.rawData = result;
        }
        return(fd);
    }
Пример #5
0
 public FaceRecognitionEventArgs(FaceRestAPI.FaceAPI faceAPI)
 {
     this.faceAPI = faceAPI;
 }
Пример #6
0
        private FaceAPI post_method_BitmapSource(string request, Dictionary<string, string> param, BitmapSource bmap)
        {
            string result = "";
            string url = this.api_server + request;
            string paramQS = http_build_query(param); Debug.WriteLine("request:" + url + "?" + paramQS);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url + "?" + paramQS);

            req.Method = "POST";

            string filename = "tmp.jpg";
            string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            req.ContentType = "multipart/form-data; boundary=" + boundary;
            req.KeepAlive = true;

            req.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Stream memStream = new System.IO.MemoryStream();
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

            //WTF is the real purpose of this?
            foreach (KeyValuePair<string, string> s in param)
            {
                string formitem = string.Format(formdataTemplate, s.Key, s.Value);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }

            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"_file\"; filename=\"_files\"\r\n Content-Type: application/octet-stream\r\n\r\n";

            string header = string.Format(headerTemplate, "file1", filename);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            memStream.Write(headerbytes, 0, headerbytes.Length);

            MemoryStream jStream = new MemoryStream();
            JpegBitmapEncoder jpegEnc = new JpegBitmapEncoder();
            jpegEnc.Frames.Add(BitmapFrame.Create(bmap));
            jpegEnc.Save(jStream);

            byte[] buffer = new byte[1024];

            int bytesRead = 0;

            while ((bytesRead = jStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }
            memStream.Write(boundarybytes, 0, boundarybytes.Length);

            jStream.Close();

            req.ContentLength = memStream.Length;
            Stream requestStream = req.GetRequestStream();

            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            result = reader.ReadToEnd();

            response.Close();
            response = null;
            reader.Close();
            reader.Dispose();

            req = null;

            FaceRestAPI.FaceAPI fd = null;
            if (this.format == "json")
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                fd = jss.Deserialize<FaceRestAPI.FaceAPI>(result);
                fd.rawData = result;
            }
            if (this.format == "xml")
            {
                //fd = DeserializeObject<FaceRestAPI.FaceAPI>(result);
                fd = new FaceRestAPI.FaceAPI();
                fd.rawData = result;
            }
            return fd;
        }