Beispiel #1
1
        /// <summary>
        /// Convert string to ImageFormat
        /// </summary>
        /// <param name="format">Image format name</param>
        /// <example>
        /// <code>GetImageFormat("bmp")</code>
        /// </example>
        /// <returns>ImageFormat</returns>
        private static ImageFormat GetImageFormat(string format)
        {
            ImageFormat imageFormat = null;

            try
            {
                var imageFormatConverter = new ImageFormatConverter();
                imageFormat = (ImageFormat)imageFormatConverter.ConvertFromString(format);
            }
            catch (Exception e)
            {

                throw new FormatException();
            }

            return imageFormat;
        }
        public ProfileModule(PeopleContext people)
        {
            this.RequiresAuthentication();

            _people = people;

            Get["/profile"] = parameters =>
            {
                // call when user visit it's own profile
                var identity = Context.CurrentUser as FloreamIdentity;
                var user = _people.People.FirstOrDefault(p => p.AdUser == identity.UserName);

                return View["profile", user];
            };

            Post["/profile/upload"] = parameters =>
            {
                var file = Request.Files.FirstOrDefault();
                if (file == null)
                {
                    return new Response().WithStatusCode(HttpStatusCode.BadRequest);
                }
                
                var identity = Context.CurrentUser as FloreamIdentity;
                var imageType = file.ContentType.Split('/')[1];
                var imageHeight = int.Parse(ConfigurationManager.AppSettings.Get("profile-image-height"));
                var imageWidth = int.Parse(ConfigurationManager.AppSettings.Get("profile-image-width"));

                var memStream = new MemoryStream();
                var img = Image.FromStream(file.Value);
                if (img.Height > imageHeight || img.Width > imageWidth)
                {
                    // Resize the image
                    var bmp = ScaleImage(img, imageWidth, imageHeight);
                    // Save the resized image to a stream
                    var imageFormatConverter = new ImageFormatConverter();
                    var imageObj = imageFormatConverter.ConvertFromString(imageType);
                    if (imageObj != null)
                    {
                        bmp.Save(memStream, (ImageFormat) imageObj);
                    }
                }
                else
                {
                    img.Save(memStream, img.RawFormat);
                }
                     
                var array = memStream.ToArray();

                // Update the user's profile
                var user = _people.People.FirstOrDefault(p => p.AdUser == identity.UserName);
                if (user != null)
                {
                    user.PictureExtension = imageType;
                    user.Picture = array;
                    _people.SaveChanges();
                }

                return Response.AsText(HtmlHelper.GetProfileImage(array, imageType));
            };
        }