Ejemplo n.º 1
0
        public Photo ProcessRGBForExistingPhoto(long photoID)
        {
            Photo photo = PhotoRepository.Single(p => p.ID == photoID);

            if (photo == null)
            {
                return(null);
            }

            using (System.Drawing.Bitmap bitmap = new Bitmap(System.Drawing.Image.FromFile(string.Format("{0}{1}", PathManager.GetRawPath(), photo.FileName))))
            {
                int[]   counts      = CountRGBPixels(bitmap);
                float[] percentages = ConvertCountsToPercentage(counts);

                photo.Black = percentages[(int)PixelColor.BLACK];
                photo.White = percentages[(int)PixelColor.WHITE];
                photo.Red   = percentages[(int)PixelColor.RED];
                photo.Green = percentages[(int)PixelColor.GREEN];
                photo.Blue  = percentages[(int)PixelColor.BLUE];

                return(photo);
            }
        }
Ejemplo n.º 2
0
        public Photo ProcessUserPhoto(Stream stream, string fileName, User user, long collectionID)
        {
            Collection collection = CollectionRepository.Find(c => c.ID == collectionID && c.Type == CollectionType.USER, c => c.Site, c => c.Photos).FirstOrDefault();

            string userFolder = Path.Combine(PathManager.GetUserCollectionPath(), Convert.ToString(user.ID));

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

            try
            {
                // create the directory for the image and its components
                string basePath = Path.Combine(userFolder, collection.ContainerID, string.Format("{0}.phocalstream", fileName));
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }

                // open a Bitmap for the image to parse the meta data from
                using (System.Drawing.Image img = System.Drawing.Image.FromStream(stream))
                {
                    string savePath = Path.Combine(basePath, fileName);
                    img.Save(savePath);

                    Photo photo = CreatePhotoWithProperties(img, fileName);
                    photo.FileName = fileName;
                    photo.Site     = collection.Site;

                    PhotoRepository.Insert(photo);

                    collection.Photos.Add(photo);
                    collection.Status = CollectionStatus.INVALID;

                    // if first photo, set as cover photo
                    if (collection.Photos.Count == 1)
                    {
                        collection.CoverPhoto = photo;
                    }

                    Unit.Commit();

                    // only generate the phocalstream image if it has not already been generated
                    if (File.Exists(Path.Combine(basePath, @"High.jpg")) == false)
                    {
                        // this is a dirty hack, figure out why the image isn't opening with the correct width and height
                        if (photo.Portrait)
                        {
                            photo.Width  = img.Height;
                            photo.Height = img.Width;
                        }

                        ResizeImageTo(savePath, 1200, 800, Path.Combine(basePath, @"High.jpg"), photo.Portrait);
                        ResizeImageTo(savePath, 800, 533, Path.Combine(basePath, @"Medium.jpg"), photo.Portrait);
                        ResizeImageTo(savePath, 400, 266, Path.Combine(basePath, @"Low.jpg"), photo.Portrait);
                    }

                    return(photo);
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Exception processing photo {0}. Message: {1}", fileName, e.Message));
            }
        }