예제 #1
0
        /// <summary>
        /// Resize, rotate, incoming image, disposing it and saving transformations to disk
        /// </summary>
        /// <param name="image"></param>
        /// <param name="pathFileName"></param>
        /// <param name="angle"></param>
        private static void PrepareAndSaveEditablePhoto(Image image, string pathFileName, float angle)
        {
            Image img = null;

            try
            {
                // Resize to maximum allowed size (but not upscale) to allow user cropping later
                var auxImg = LcImaging.Rotate(image, angle, DefaultBackground);
                var w      = FixedSizeWidth * WorkPhotoOriginalScale;
                var h      = FixedSizeHeight * WorkPhotoOriginalScale;
                if (auxImg.Width < auxImg.Height)
                {
                    // It's portrait, keep original in portrait to prevent the problem of photo getting smaller
                    // with each rotation; further processing creating optimized photos will make it landscape.
                    var aux = w;
                    w = h;
                    h = aux;
                }
                img = LcImaging.Resize(auxImg, w, h, LcImaging.SizeMode.Contain, LcImaging.AnchorPosition.Center, DefaultBackground);
                auxImg.Dispose();

                // Free source, no needed any more and if comes from file, saving will crash
                image.Dispose();

                // Save:
                img.Save(pathFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            finally
            {
                if (img != null)
                {
                    img.Dispose();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Save the uploaded photo as an image on the user folder
        /// with a greater size than usual but limited,
        /// for later use on cropping and resizing tasks.
        /// </summary>
        /// <param name="photo"></param>
        /// <param name="virtualPath"></param>
        public static void SaveEditableProfilePicture(int userID, Stream photo, float angle)
        {
            // Check folder or create
            string virtualPath = LcUrl.RenderAppPath + GetUserPhotoFolder(userID);
            var    folder      = System.Web.HttpContext.Current.Server.MapPath(virtualPath);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            // Use file as image
            using (var srcImg = System.Drawing.Image.FromStream(photo))
            {
                // Resize to maximum allowed size (but not upscale) to allow user cropping later
                var auxImg = LcImaging.Rotate(srcImg, angle, DefaultBackground);
                var img    = LcImaging.Resize(auxImg, profilePictureFixedSizeWidth * profilePictureOriginalScale, profilePictureFixedSizeHeight * profilePictureOriginalScale, profilePictureSizeMode, LcImaging.AnchorPosition.Center, DefaultBackground);
                auxImg.Dispose();

                // Save:
                img.Save(folder + avatarName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                img.Dispose();
            }

            photo.Dispose();
        }
예제 #3
0
        /// <summary>
        /// Ask to edit the original saved profile picture applying a rotation.
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="angle"></param>
        /// <returns>True when exists and everything fine, false if no photo exists so couldn't perform task. Throw exception if error</returns>
        public static bool EditEditableProfilePicture(int userID, float angle)
        {
            string virtualPath = LcUrl.RenderAppPath + GetUserPhotoFolder(userID);
            var    folder      = System.Web.HttpContext.Current.Server.MapPath(virtualPath);

            if (!Directory.Exists(folder))
            {
                return(false);
            }

            var file = folder + avatarName + ".jpg";

            if (!File.Exists(file))
            {
                return(false);
            }

            // Use file as image
            Image img = null;

            try
            {
                using (var srcImg = System.Drawing.Image.FromFile(file))
                {
                    // Resize to maximum allowed size (but not upscale) to allow user cropping later
                    var auxImg = LcImaging.Rotate(srcImg, angle, DefaultBackground);
                    img = LcImaging.Resize(auxImg, profilePictureFixedSizeWidth * profilePictureOriginalScale, profilePictureFixedSizeHeight * profilePictureOriginalScale, profilePictureSizeMode, LcImaging.AnchorPosition.Center, DefaultBackground);
                    auxImg.Dispose();
                }
                // Save:
                img.Save(file, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            finally
            {
                if (img != null)
                {
                    img.Dispose();
                }
            }

            return(true);
        }