/// <summary>
        /// Finds the image perspective.
        /// </summary>
        /// <param name="photo">The photo.</param>
        /// <param name="maxImageDimension">The max image dimension.</param>
        /// <returns></returns>
        public static ImageDimension FindImagePerspective(Bitmap photo, ImageDimension maxImageDimension)
        {
            ImageDimension dimension = new ImageDimension();

            //determine if it's a portrait or landscape
            if (photo.Height > photo.Width)
            {
                //portrait
                decimal ratio = (photo.Width / (decimal)photo.Height);

                dimension.Height = (photo.Height < maxImageDimension.Height ? photo.Height : maxImageDimension.Height);
                dimension.Width = (int)(ratio * dimension.Height);

                dimension.PhotoProfile = PhotoProfile.Portrait;
            }
            else
            {
                //landscape
                decimal ratio = (photo.Height / (decimal)photo.Width);

                dimension.Width = (photo.Width < maxImageDimension.Width ? photo.Width : maxImageDimension.Width);
                dimension.Height = (int)(ratio * dimension.Width);

                dimension.PhotoProfile = PhotoProfile.Landscape;

            }

            return dimension;
        }
        /// <summary>
        /// Resizes the bitmap, pass in the new sizes
        /// </summary>
        /// <param name="bitmap">bitmap to be resized</param>
        /// <param name="dimensions">The dimensions.</param>
        /// <returns></returns>
        public static Bitmap Resize(Bitmap bitmap, ImageDimension dimensions)
        {
            Bitmap result = new Bitmap(dimensions.Width, dimensions.Height);
            using (Graphics graphic = Graphics.FromImage(result))
            {
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                graphic.DrawImage(bitmap, 0, 0, dimensions.Width, dimensions.Height);
            }

            return result;
        }
        /// <summary>
        /// Resizes the specified bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <param name="dimensions">The dimensions.</param>
        /// <returns></returns>
        public static byte[] Resize(byte[] bitmap, ImageDimension dimensions)
        {
            byte[] imageBytes;

            Bitmap image = ConvertByteArrayToBitmap(bitmap);
            image = Resize(image, dimensions);

            using (MemoryStream outStream = new MemoryStream())
            {
                image.Save(outStream, ImageFormat.Jpeg);
                outStream.Position = 0;
                imageBytes = new byte[outStream.Length];
                outStream.Read(imageBytes, 0, (int)outStream.Length);
            }

            return imageBytes;
        }
        /// <summary>
        /// Inserts the specified photo.
        /// </summary>
        /// <param name="photo">The photo.</param>
        /// <param name="galleryPhoto">The gallery photo.</param>
        public void Insert(Photo photo, GalleryPhoto galleryPhoto)
        {
            GalleryPhoto adminPhotos = new GalleryPhoto();

            GallerySettings settings = GallerySettings.Load();
            Bitmap orginalBitmap = ImageConversion.ConvertByteArrayToBitmap(galleryPhoto.OriginalImage);

            //hardcoded administration image dimensions. This will allow the user to change their image sizes and not have it effect my pretty admin layout.
            ImageDimension adminThumbnailDimensions = new ImageDimension { Height = 100, Width = 100 };
            ImageDimension adminFullsizeDimensions = new ImageDimension { Height = 600, Width = 600 };

            adminThumbnailDimensions = FindImagePerspective(orginalBitmap, adminThumbnailDimensions);
            adminFullsizeDimensions = FindImagePerspective(orginalBitmap, adminFullsizeDimensions);

            //Resize the Admin images
            adminPhotos.ThumbnailImage = ImageConversion.Resize(galleryPhoto.OriginalImage, adminThumbnailDimensions);
            adminPhotos.DisplayImage = ImageConversion.Resize(galleryPhoto.OriginalImage, adminFullsizeDimensions);

            //calculate the correct dimensions
            ImageDimension thumbnailDimensions = FindImagePerspective(orginalBitmap, settings.ThumbnailDimensions);
            ImageDimension fullsizeDimensions = FindImagePerspective(orginalBitmap, settings.FullsizeDimensions);

            //Resize the images
            galleryPhoto.ThumbnailImage = ImageConversion.Resize(galleryPhoto.OriginalImage, thumbnailDimensions);
            galleryPhoto.DisplayImage = ImageConversion.Resize(galleryPhoto.OriginalImage, fullsizeDimensions);

            //Set photo profile
            photo.Profile = fullsizeDimensions.PhotoProfile;

            //Insert new images into the Database
            _resource.Insert(photo, galleryPhoto, adminPhotos);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="GallerySettings"/> class.
 /// </summary>
 public GallerySettings()
 {
     ThumbnailDimensions = new ImageDimension();
     FullsizeDimensions = new ImageDimension();
 }
Beispiel #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GallerySettings"/> class.
 /// </summary>
 public GallerySettings()
 {
     ThumbnailDimensions = new ImageDimension();
     FullsizeDimensions  = new ImageDimension();
 }