public static SetDisplayPictureResult SaveDisplayPicture(UserContext userContext)
        {
            SetDisplayPictureResult setDisplayPictureResult = DisplayPictureUtility.SaveToAD(userContext, false);

            if (setDisplayPictureResult.ResultCode != SetDisplayPictureResultCode.NoError)
            {
                return(setDisplayPictureResult);
            }
            string imageSmallHtml = string.Empty;
            string imageLargeHtml = string.Empty;
            string adpictureUrl   = RenderingUtilities.GetADPictureUrl(userContext.ExchangePrincipal.LegacyDn, "EX", userContext, true);

            using (StringWriter stringWriter = new StringWriter())
            {
                RenderingUtilities.RenderDisplayPictureImage(stringWriter, userContext, adpictureUrl, 64, true, ThemeFileId.DoughboyPerson);
                imageLargeHtml = stringWriter.ToString();
            }
            using (StringWriter stringWriter2 = new StringWriter())
            {
                RenderingUtilities.RenderDisplayPictureImage(stringWriter2, userContext, adpictureUrl, 32, true, ThemeFileId.DoughboyPersonSmall);
                imageSmallHtml = stringWriter2.ToString();
            }
            setDisplayPictureResult.SetSuccessResult(imageSmallHtml, imageLargeHtml);
            return(setDisplayPictureResult);
        }
        private static SetDisplayPictureResult SaveToAD(UserContext userContext, bool isClearPicture)
        {
            if (userContext == null)
            {
                throw new ArgumentNullException("userContext");
            }
            SetDisplayPictureResult noError = SetDisplayPictureResult.NoError;

            byte[] array = null;
            if (!isClearPicture)
            {
                if (userContext.UploadedDisplayPicture == null)
                {
                    noError.SetErrorResult(SetDisplayPictureResultCode.SaveError, SanitizedHtmlString.GetNonEncoded(-1306631087));
                    return(noError);
                }
                array = userContext.UploadedDisplayPicture;
            }
            IRecipientSession recipientSession = Utilities.CreateADRecipientSession(false, ConsistencyMode.IgnoreInvalid, userContext);
            ADRecipient       adrecipient      = Utilities.CreateADRecipientFromProxyAddress(userContext.ExchangePrincipal.ObjectId, null, recipientSession);

            if (adrecipient == null)
            {
                noError.SetErrorResult(SetDisplayPictureResultCode.GeneralError, SanitizedHtmlString.GetNonEncoded(-1496582182));
                return(noError);
            }
            try
            {
                adrecipient.SetProperties(new PropertyDefinition[]
                {
                    ADRecipientSchema.ThumbnailPhoto
                }, new object[]
                {
                    array
                });
                recipientSession.Save(adrecipient);
            }
            catch (Exception ex)
            {
                noError.SetErrorResult(SetDisplayPictureResultCode.SaveError, SanitizedHtmlString.GetNonEncoded(-1496582182));
                ExTraceGlobals.CoreTracer.TraceDebug <string>(0L, "Failed to save display picture to AD  - {0}", ex.Message);
                return(noError);
            }
            userContext.UpdateDisplayPictureCanary();
            userContext.UploadedDisplayPicture = null;
            userContext.HasPicture             = new bool?(array != null);
            string stringHash = Utilities.GetStringHash(userContext.ExchangePrincipal.LegacyDn);

            if (isClearPicture)
            {
                DisplayPictureUtility.RecipientsNegativeCache[stringHash] = DateTime.UtcNow;
            }
            else if (DisplayPictureUtility.IsInRecipientsNegativeCache(stringHash))
            {
                DisplayPictureUtility.RecipientsNegativeCache.Remove(stringHash);
            }
            return(noError);
        }
 private static byte[] ProcessImage(Stream stream, SetDisplayPictureResult result)
 {
     byte[] result2;
     try
     {
         Image image = Image.FromStream(stream);
         if (image == null)
         {
             result.SetErrorResult(SetDisplayPictureResultCode.FailedProcessImage, SanitizedHtmlString.GetNonEncoded(1235679903));
             result2 = null;
         }
         else
         {
             Size   imageScaledSize = DisplayPictureUtility.GetImageScaledSize(image.Size);
             Bitmap bitmap          = new Bitmap(imageScaledSize.Width, imageScaledSize.Height);
             using (Graphics graphics = Graphics.FromImage(bitmap))
             {
                 graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                 graphics.DrawImage(image, 0, 0, imageScaledSize.Width, imageScaledSize.Height);
             }
             PointF     imageCenterPosition = DisplayPictureUtility.GetImageCenterPosition((float)imageScaledSize.Width, (float)imageScaledSize.Height);
             RectangleF rect    = new RectangleF(imageCenterPosition, new SizeF((float)Math.Min(DisplayPictureUtility.maxImageSize, imageScaledSize.Width), (float)Math.Min(DisplayPictureUtility.maxImageSize, imageScaledSize.Height)));
             Bitmap     bitmap2 = bitmap.Clone(rect, bitmap.PixelFormat);
             byte[]     array   = null;
             using (Stream stream2 = new MemoryStream())
             {
                 bitmap2.Save(stream2, ImageFormat.Jpeg);
                 stream2.Position = 0L;
                 array            = DisplayPictureUtility.ReadFromStream(stream2);
             }
             result2 = array;
         }
     }
     catch (Exception ex)
     {
         result.SetErrorResult(SetDisplayPictureResultCode.FailedProcessImage, SanitizedHtmlString.GetNonEncoded(1235679903));
         ExTraceGlobals.CoreTracer.TraceDebug <string>(0L, "Failed to process image  - {0}", ex.Message);
         result2 = null;
     }
     return(result2);
 }
        public static SetDisplayPictureResult UploadDisplayPicture(HttpPostedFile file, UserContext userContext)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (userContext == null)
            {
                throw new ArgumentNullException("userContext");
            }
            SetDisplayPictureResult noError = SetDisplayPictureResult.NoError;

            if (file.ContentLength == 0)
            {
                noError.SetErrorResult(SetDisplayPictureResultCode.GeneralError, SanitizedHtmlString.GetNonEncoded(-1496582182));
                return(noError);
            }
            int num  = 20;
            int num2 = num * 1048576;

            if (file.ContentLength > num2)
            {
                noError.SetErrorResult(SetDisplayPictureResultCode.FileExceedsSizeLimit, SanitizedHtmlString.Format(LocalizedStrings.GetNonEncoded(1511892651), new object[]
                {
                    num
                }));
                return(noError);
            }
            byte[] uploadedDisplayPicture = DisplayPictureUtility.ProcessImage(file.InputStream, noError);
            if (noError.ResultCode != SetDisplayPictureResultCode.NoError)
            {
                return(noError);
            }
            userContext.UploadedDisplayPicture = uploadedDisplayPicture;
            return(noError);
        }
        public static SetDisplayPictureResult ClearDisplayPicture(UserContext userContext)
        {
            SetDisplayPictureResult setDisplayPictureResult = DisplayPictureUtility.SaveToAD(userContext, true);

            if (setDisplayPictureResult.ResultCode != SetDisplayPictureResultCode.NoError)
            {
                return(setDisplayPictureResult);
            }
            string imageSmallHtml = string.Empty;
            string imageLargeHtml = string.Empty;

            using (StringWriter stringWriter = new StringWriter())
            {
                RenderingUtilities.RenderDisplayPictureImage(stringWriter, userContext, string.Empty, 64, true, ThemeFileId.DoughboyPerson);
                imageLargeHtml = stringWriter.ToString();
            }
            using (StringWriter stringWriter2 = new StringWriter())
            {
                RenderingUtilities.RenderDisplayPictureImage(stringWriter2, userContext, string.Empty, 32, true, ThemeFileId.DoughboyPersonSmall);
                imageSmallHtml = stringWriter2.ToString();
            }
            setDisplayPictureResult.SetSuccessResult(imageSmallHtml, imageLargeHtml);
            return(setDisplayPictureResult);
        }