private ImageInfo ResizeImage(ImageSet set, int width, int height)
        {
            lock (set) {
                ImageInfo info = new ImageInfo(set);
                info.Width       = width;
                info.Height      = height;
                info.Date        = DateTime.UtcNow;
                info.ImageFormat = set.PrimaryImage.ImageFormat;
                set.ResizedImages.Add(info);

                using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(set.PrimaryImage.Path))
                    using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height))
                        using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp)) {
                            graphic.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            graphic.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            graphic.PixelOffsetMode    = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                            graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                            graphic.DrawImage(bmp, 0, 0, width, height);

                            MemoryStream ms = new MemoryStream();
                            newBmp.Save(ms, info.ImageFormat);

                            using (var fs = ProtectedFileStream.OpenExclusiveWriter(info.Path)) {
                                BinaryWriter bw = new BinaryWriter(fs);
                                bw.Write(ms.ToArray());
                            }
                        }

                return(info);
            }
        }
 public ImageSet GetImageSet(Guid id)
 {
     lock (this) {
         ImageSet set = null;
         lock (imageInfoCache) {
             imageInfoCache.TryGetValue(id, out set);
         }
         return(set);
     }
 }
        public List <ImageSize> AvailableSizes(Guid id)
        {
            ImageSet set = GetImageSet(id);

            if (set != null)
            {
                return(set.ResizedImages.Select(_ => _.Size).Concat(new ImageSize[] { set.PrimaryImage.Size }).ToList());
            }
            else
            {
                return(null);
            }
        }
        private ImageSet GetOrCreateImageSet(Guid id)
        {
            ImageSet imageSet;

            lock (this) {
                imageSet = GetImageSet(id);

                if (imageSet == null)
                {
                    imageSet           = new ImageSet(this, id);
                    imageInfoCache[id] = imageSet;
                }
            }
            return(imageSet);
        }
 private static void DeleteImageSet(ImageSet imageSet, bool includeResized)
 {
     try {
         if (imageSet.PrimaryImage != null)
         {
             int  retries    = 0;
             bool successful = false;
             while (retries < 3 && !successful)
             {
                 try
                 {
                     File.Delete(imageSet.PrimaryImage.Path);
                     successful = true;
                 }
                 catch (Exception e)
                 {
                     Logger.ReportException("Error attempting to delete image: " + imageSet.PrimaryImage.Path + ". Will retry...", e);
                     retries++;
                 }
             }
         }
         if (includeResized)
         {
             foreach (var resized in imageSet.ResizedImages)
             {
                 int  retries    = 0;
                 bool successful = false;
                 while (retries < 3 && !successful)
                 {
                     try
                     {
                         File.Delete(resized.Path);
                         successful = true;
                     }
                     catch (Exception e)
                     {
                         Logger.ReportException("Error attempting to delete image: " + resized.Path + ". Will retry...", e);
                         retries++;
                     }
                 }
             }
         }
     } finally {
         imageSet.ResizedImages = new List <ImageInfo>();
         imageSet.PrimaryImage  = null;
     }
 }
        public string GetImagePath(Guid id, int width, int height)
        {
            ImageSet set = GetImageSet(id);

            if (set != null && set.PrimaryImage != null)
            {
                lock (set) {
                    var image = set.ResizedImages.FirstOrDefault(info => info.Width == width && info.Height == height);
                    if (image != null)
                    {
                        return(image.Path);
                    }
                    else
                    {
                        return(ResizeImage(set, width, height).Path);
                    }
                }
            }

            // no such image
            return(null);
        }
Exemple #7
0
 public ImageInfo(ImageSet parent)
 {
     this.Parent = parent;
 }
Exemple #8
0
        private ImageInfo ResizeImage(ImageSet set, int width, int height)
        {
            lock (set) {
                ImageInfo info = new ImageInfo(set);
                info.Width = width;
                info.Height = height;
                info.Date = DateTime.UtcNow;
                info.ImageFormat = set.PrimaryImage.ImageFormat;
                set.ResizedImages.Add(info);

                using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(set.PrimaryImage.Path))
                using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height))
                using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp)) {

                    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                    graphic.DrawImage(bmp, 0, 0, width, height);

                    MemoryStream ms = new MemoryStream();
                    newBmp.Save(ms, info.ImageFormat);

                    using (var fs = ProtectedFileStream.OpenExclusiveWriter(info.Path)) {
                        BinaryWriter bw = new BinaryWriter(fs);
                        bw.Write(ms.ToArray());
                    }
                }

                return info;
            }
        }
Exemple #9
0
        private ImageSet GetOrCreateImageSet(Guid id)
        {
            ImageSet imageSet;
            lock (this) {
                imageSet = GetImageSet(id);

                if (imageSet == null) {
                    imageSet = new ImageSet(this, id);
                    imageInfoCache[id] = imageSet;
                }
            }
            return imageSet;
        }
Exemple #10
0
        private void AddToCache(IMediaLocation item)
        {
            if (item is IFolderMediaLocation) {
                return;
            }

            var extension = System.IO.Path.GetExtension(item.Path).ToLower();
            ImageFormat imageFormat = null;
            if (extension == ".png") {
                imageFormat = ImageFormat.Png;
            } else if (extension == ".jpg") {
                imageFormat = ImageFormat.Jpeg;
            } else if (extension == ".gif") {
                imageFormat = ImageFormat.Gif;
            }
            else if (extension == ".bmp")
            {
                imageFormat = ImageFormat.Bmp;
            }
            else
            {
                // bad file in image cache
                File.Delete(item.Path);
                return;
            }

            var match = infoRegex.Match(item.Name);
            if (match == null || match.Groups[1].Value == null) {
                // bad file
                File.Delete(item.Path);
                return;
            }

            bool isPrimary = match.Groups[1].Value == "z";
            Guid id = new Guid(match.Groups[2].Value);

            int width = -1;
            int height = -1;

            if (!string.IsNullOrEmpty(match.Groups[3].Value)) {
                width = Int32.Parse(match.Groups[3].Value);
            }
            if (!string.IsNullOrEmpty(match.Groups[4].Value)) {
                height = Int32.Parse(match.Groups[4].Value);
            }

            var imageSet = GetImageSet(id);
            if (imageSet == null) {
                imageSet = new ImageSet(this, id);
                imageInfoCache[id] = imageSet;
            }

            var info = new ImageInfo(imageSet);
            info.ImageFormat = imageFormat;
            info.Date = item.DateModified > item.DateCreated ? item.DateModified : item.DateCreated;

            //upgrade logic
            if (width == -1 || height == -1) {
                Image image = Image.FromFile(item.Path);
                isPrimary = true;
                info.Width = image.Width;
                info.Height = image.Height;
                imageSet.PrimaryImage = info;
                image.Dispose();
                File.Move(item.Path, info.Path);
            } else {
                info.Width = width;
                info.Height = height;
            }

            if (isPrimary) {
                imageSet.PrimaryImage = info;
            } else {
                imageSet.ResizedImages.Add(info);
            }
        }
Exemple #11
0
        private static void DeleteImageSet(ImageSet imageSet, bool includeResized)
        {
            try {
                if (imageSet.PrimaryImage != null) {
                    int retries = 0;
                    bool successful = false;
                    while (retries < 3 && !successful)
                    {
                        try
                        {
                            File.Delete(imageSet.PrimaryImage.Path);
                            successful = true;
                        }
                        catch (Exception e)
                        {
                            Logger.ReportException("Error attempting to delete image: " + imageSet.PrimaryImage.Path + ". Will retry...", e);
                            retries++;
                        }
                    }
                }
                if (includeResized)
                {
                    foreach (var resized in imageSet.ResizedImages)
                    {
                        int retries = 0;
                        bool successful = false;
                        while (retries < 3 && !successful)
                        {
                            try
                            {
                                File.Delete(resized.Path);
                                successful = true;
                            }
                            catch (Exception e)
                            {
                                Logger.ReportException("Error attempting to delete image: " + resized.Path + ". Will retry...", e);
                                retries++;
                            }
                        }

                    }
                }
            } finally {
                imageSet.ResizedImages = new List<ImageInfo>();
                imageSet.PrimaryImage = null;
            }
        }
Exemple #12
0
 private static void ClearImageSet(ImageSet imageSet)
 {
     imageSet.PrimaryImage = null;
     imageSet.ResizedImages = new List<ImageInfo>();
 }
 private static void ClearImageSet(ImageSet imageSet)
 {
     imageSet.PrimaryImage  = null;
     imageSet.ResizedImages = new List <ImageInfo>();
 }
 public ImageInfo(ImageSet parent)
 {
     this.Parent = parent;
 }
        public virtual string GetImagePath(Guid id)
        {
            ImageSet set = GetImageSet(id);

            return(set != null && set.PrimaryImage != null ? set.PrimaryImage.Path : null);
        }
        private void AddToCache(IMediaLocation item)
        {
            if (item is IFolderMediaLocation)
            {
                return;
            }

            var         extension   = System.IO.Path.GetExtension(item.Path).ToLower();
            ImageFormat imageFormat = null;

            if (extension == ".png")
            {
                imageFormat = ImageFormat.Png;
            }
            else if (extension == ".jpg")
            {
                imageFormat = ImageFormat.Jpeg;
            }
            else if (extension == ".gif")
            {
                imageFormat = ImageFormat.Gif;
            }
            else if (extension == ".bmp")
            {
                imageFormat = ImageFormat.Bmp;
            }
            else
            {
                // bad file in image cache
                File.Delete(item.Path);
                return;
            }


            var match = infoRegex.Match(item.Name);

            if (match == null || match.Groups[1].Value == null)
            {
                // bad file
                File.Delete(item.Path);
                return;
            }

            bool isPrimary = match.Groups[1].Value == "z";
            Guid id        = new Guid(match.Groups[2].Value);

            int width  = -1;
            int height = -1;

            if (!string.IsNullOrEmpty(match.Groups[3].Value))
            {
                width = Int32.Parse(match.Groups[3].Value);
            }
            if (!string.IsNullOrEmpty(match.Groups[4].Value))
            {
                height = Int32.Parse(match.Groups[4].Value);
            }


            var imageSet = GetImageSet(id);

            if (imageSet == null)
            {
                imageSet           = new ImageSet(this, id);
                imageInfoCache[id] = imageSet;
            }



            var info = new ImageInfo(imageSet);

            info.ImageFormat = imageFormat;
            info.Date        = item.DateModified > item.DateCreated ? item.DateModified : item.DateCreated;

            //upgrade logic
            if (width == -1 || height == -1)
            {
                Image image = Image.FromFile(item.Path);
                isPrimary             = true;
                info.Width            = image.Width;
                info.Height           = image.Height;
                imageSet.PrimaryImage = info;
                image.Dispose();
                File.Move(item.Path, info.Path);
            }
            else
            {
                info.Width  = width;
                info.Height = height;
            }

            if (isPrimary)
            {
                imageSet.PrimaryImage = info;
            }
            else
            {
                imageSet.ResizedImages.Add(info);
            }
        }