/// <summary>
        /// Method to upload provided WriteableBitmap to Imgur.
        /// </summary>
        /// <param name="imageBitmap">WriteableBitmap to upload.</param>
        /// <returns>String representing the direct image url for the uploaded image.</returns>
        private async Task <string> uploadPhotoToImgur(WriteableBitmap imageBitmap)
        {
            using (var client = new HttpClient())
            {
                // Upload to Imgur as a form post with provided Client-Id
                using (var content =
                           new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
                {
                    content.Add(new ByteArrayContent(await EncodeJpeg(imageBitmap)), "image", "deck.jpg");

                    client.DefaultRequestHeaders.Add("Authorization", "Client-ID " + _imgur_client_id);
                    using (var message = await client.PostAsync("https://api.imgur.com/3/upload", content))
                    {
                        if (message.IsSuccessStatusCode)
                        {
                            // Read HTTP POST response into the Imgur object.
                            Imgur imgurResponse = JsonConvert.DeserializeObject <Imgur>(await message.Content.ReadAsStringAsync());

                            return(imgurResponse.data.link);
                        }
                        else
                        {
                            return(string.Empty);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Method to upload provided WriteableBitmap to Imgur.
        /// </summary>
        /// <param name="imageBitmap">Memory Access Stream of the image to upload.</param>
        /// <returns>String representing the direct image url for the uploaded image.</returns>
        private async Task <string> uploadPhotoToImgur(IRandomAccessStream imageBitmap)
        {
            using (var client = new HttpClient())
            {
                // Upload to Imgur as a form post with provided Client-Id
                using (var content =
                           new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
                {
                    // Convert to bytes
                    DataReader dr    = new DataReader(imageBitmap.GetInputStreamAt(0));
                    byte[]     bytes = new byte[imageBitmap.Size];
                    await dr.LoadAsync((uint)imageBitmap.Size);

                    dr.ReadBytes(bytes);

                    content.Add(new ByteArrayContent(bytes), "image", "deck.jpg");

                    client.DefaultRequestHeaders.Add("Authorization", "Client-ID " + _imgur_client_id);
                    using (var message = await client.PostAsync("https://api.imgur.com/3/upload", content))
                    {
                        if (message.IsSuccessStatusCode)
                        {
                            // Read HTTP POST response into the Imgur object.
                            string jsonString = await message.Content.ReadAsStringAsync();

                            Imgur imgurResponse = JsonConvert.DeserializeObject <Imgur>(jsonString, new JsonSerializerSettings {
                                NullValueHandling = NullValueHandling.Ignore
                            });

                            return(imgurResponse.data.link);
                        }
                        else
                        {
                            return(string.Empty);
                        }
                    }
                }
            }
        }