Exemplo n.º 1
0
        /// <inheritdoc/>
        public async Task GetAvatarAsync(CancellationToken cancellationToken)
        {
            Ensure.ParamNotNull(graphClient, nameof(graphClient));
            try
            {
                // GET /me
                Stream photoresponse = await graphClient.Me.Photo.Content.Request().GetAsync(cancellationToken).ConfigureAwait(false);

                if (photoresponse != null)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        photoresponse.CopyTo(ms);
                        System.Drawing.Image microsoftAvatar = System.Drawing.Image.FromStream(ms);
                        ResizeImageHelper.ResizeImage(microsoftAvatar, 32, 32).Save(Path.Combine(mSettingsManager.PathToAppData, "PTM2020", "UserAvatar.png"), ImageFormat.Png);
                    }
                }
                else
                {
                    throw new ArgumentException("Photo response is null", nameof(Microsoft));
                }
            }
            catch (ServiceException ex)
            {
                // Jesli uzytkownik nie posiada avatara to usuwa poprzedni zapamietany zeby nie zostal wczytany do programu zły avatar
                System.IO.File.Delete(mSettingsManager.PathToAppData + "\\PTM2020" + "\\UserAvatar.png");
            }
        }
Exemplo n.º 2
0
        public async Task GetAvatarAsync(CancellationToken cancellationToken)
        {
            JObject jsonParsed = JObject.Parse(mTokenResponse);
            string  photoUrl   = (string)jsonParsed.SelectToken("picture");

            using (WebClient webClient = new WebClient())
            {
                byte[] data = webClient.DownloadData(photoUrl);
                using (MemoryStream mem = new MemoryStream(data))
                {
                    using (System.Drawing.Image googleAvatar = System.Drawing.Image.FromStream(mem))
                    {
                        ResizeImageHelper.ResizeImage(googleAvatar, 32, 32).Save(Path.Combine(mSettingsManager.PathToAppData, "PTM2020", "UserAvatar.png"), ImageFormat.Png);
                    }
                }
            }
        }