Ejemplo n.º 1
0
        private Gallery GetThumbGallery(string location, string rootPath)
        {
            int thumbnailSize = 280;

            int mainImageMaxWidth  = 2000; // note: images aren't really this size more like 640x480
            int mainImageMaxHeight = 860;

            Gallery g = new Gallery();

            g.imageData = new List <ImageData>();

            // build list of images
            var thumbs = GetThumbs(location);

            g.imageData.AddRange(thumbs);

            // add to gallery structure

            foreach (var image in g.imageData)
            {
                // thumbnail
                ImageSize    thumbSize  = new ImageSize(thumbnailSize, thumbnailSize, CropType.Square);
                ImagePath    thumbPath  = new ImagePath(rootPath, site + "/nails", "thumb_4t" + thumbnailSize + "_" + image.imageName);
                ImageVersion thumbImage = new ImageVersion(ImageVersionTypes.DesktopForGallery, thumbSize, thumbPath);
                image.Versions.Add(thumbImage);

                // large popup
                ImageSize    desktopForGalleryImageSize  = new ImageSize(mainImageMaxHeight, mainImageMaxWidth, CropType.None);
                ImagePath    desktopForGalleryImagePaths = new ImagePath(rootPath, site + "/nails", "thumb_b" + mainImageMaxHeight + "_" + mainImageMaxWidth + "_" + image.imageName);
                ImageVersion desktopForGalleryImage      = new ImageVersion(ImageVersionTypes.DesktopMaxSize, desktopForGalleryImageSize, desktopForGalleryImagePaths);
                image.Versions.Add(desktopForGalleryImage);
            }
            return(g);
        }
Ejemplo n.º 2
0
        private Gallery GetMainGallery(string location, string rootPath)
        {
            int mainImageMaxWidth  = 2000; // note: images aren't really this size more like 640x480
            int mainImageMaxHeight = 860;

            Gallery g = new Gallery();

            g.imageData = new List <ImageData>();

            // build list of images
            var main = GetMainImage(location);

            g.imageData.Add(main);

            // add to gallery structure

            foreach (var image in g.imageData)
            {
                // main image
                ImageSize    desktopForGalleryImageSize  = new ImageSize(mainImageMaxHeight, mainImageMaxWidth, CropType.None);
                ImagePath    desktopForGalleryImagePaths = new ImagePath(rootPath, site + "/nails", "thumb_b" + mainImageMaxHeight + "_" + mainImageMaxWidth + "_" + image.imageName);
                ImageVersion desktopForGalleryImage      = new ImageVersion(ImageVersionTypes.DesktopMaxSize, desktopForGalleryImageSize, desktopForGalleryImagePaths);
                image.Versions.Add(desktopForGalleryImage);
            }
            return(g);
        }
Ejemplo n.º 3
0
        public async Task <FileData> GetFile(Guid id, string filename, ImageVersion imageVersion, int?width = 0, int?height = 0)
        {
            if (imageVersion == ImageVersion.Custom)
            {
                return(await GetFileFromFolder(id, filename, width.Value, height.Value));
            }
            var size = GetSize(imageVersion);

            return(await GetFileFromFolder(id, filename, size, size));
        }
Ejemplo n.º 4
0
        private int GetSize(ImageVersion imageVersion)
        {
            var imageSizeMap = new Dictionary <ImageVersion, int> {
                { ImageVersion.Thumbnail, 128 },
                { ImageVersion.Small, 512 },
                { ImageVersion.Large, 2048 },
                { ImageVersion.Custom, 0 }
            };

            return(imageSizeMap[imageVersion]);
        }
 private IReadOnlyDictionary<string, IImageVersion> Versions()
 {
     var versions = new Dictionary<string, IImageVersion>();
     var thumb = new ImageVersion
     {
         Width = 100,
         Format = new GifFormat { Quality = 10 },
     };
     versions.Add("test", thumb);
     return new ReadOnlyDictionary<string, IImageVersion>(versions);
 }
Ejemplo n.º 6
0
        public static async Task <ImageAndOsType> UpdateImageAndOsTypeAsync(
            this IClient client, string imageName, string location)
        {
            if (imageName.Contains(':'))
            {
                var imageArray = imageName.Split(':');
                if (imageArray.Length != 4)
                {
                    throw new InvalidOperationException(
                              string.Format(Resources.InvalidImageName, imageName));
                }
                var image = new ImageReference
                {
                    Publisher = imageArray[0],
                    Offer     = imageArray[1],
                    Sku       = imageArray[2],
                    Version   = imageArray[3],
                };
                var compute = client.GetClient <ComputeManagementClient>();
                if (image.Version.ToLower() == "latest")
                {
                    var images = await compute.VirtualMachineImages.ListAsync(
                        location, image.Publisher, image.Offer, image.Sku);

                    // According to Compute API:
                    // "The allowed formats are Major.Minor.Build or 'latest'.
                    //  Major, Minor, and Build are decimal numbers."
                    image.Version = images
                                    .Select(i => ImageVersion.Parse(i.Name))
                                    .Aggregate((a, b) => a.CompareTo(b) < 0 ? b : a)
                                    .ToString();
                }
                var imageModel = await compute.VirtualMachineImages.GetAsync(
                    location, image.Publisher, image.Offer, image.Sku, image.Version);

                return(new ImageAndOsType(imageModel.OsDiskImage.OperatingSystem, image));
            }
            else
            {
                // get image
                return(Images
                       .Instance
                       .SelectMany(osAndMap => osAndMap
                                   .Value
                                   .Where(nameAndImage => nameAndImage.Key.ToLower() == imageName.ToLower())
                                   .Select(nameAndImage => new ImageAndOsType(
                                               osAndMap.Key == "Windows"
                                ? OperatingSystemTypes.Windows
                                : OperatingSystemTypes.Linux,
                                               nameAndImage.Value)))
                       .FirstOrDefault());
            }
        }
        public async Task Process()
        {
            var dir   = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            var f     = dir.Substring(6, dir.Length - 6) + @"\icon.png";
            var bytes = File.ReadAllBytes(f);
            var data  = new ImageQueued()
            {
                Identifier        = Guid.NewGuid(),
                FileNameFormat    = "good_{0}_file",
                OriginalExtension = string.Empty,
            };
            var versions = new Dictionary <string, IImageVersion>();
            var version  = new ImageVersion()
            {
                Width  = 100,
                Height = 100,
                Format = new JpegFormat {
                    Quality = 70
                },
            };

            versions.Add("temp", version);
            var fileName = string.Format(data.FileNameFormat, "temp");
            var original = string.Format(data.FileNameFormat, Naming.Original);

            var imaging = Substitute.For <IImaging>();

            imaging.Resize(bytes, version).Returns(bytes);
            var naming = Substitute.For <INaming>();

            naming.OriginalFileName(data).Returns(original);
            naming.FileName(data, "temp", version.Format.DefaultExtension).Returns(fileName);
            var streamer = Substitute.For <IStreamer>();

            streamer.Bytes(original).Returns(Task.FromResult(bytes));
            var store = Substitute.For <IDataStore>();

            store.Streamer.Returns(streamer);
            store.Save(fileName, bytes, "temp", version.Format.MimeType, data.Identifier, false, null, (byte)version.Format.Quality);

            var ip     = new Processor(store, versions, imaging, naming);
            var result = await ip.Process(data);

            Assert.IsTrue(result);

            imaging.Received().Resize(bytes, version);
            naming.Received().OriginalFileName(data);
            naming.Received().FileName(data, "temp", version.Format.DefaultExtension);
            streamer.Received().Bytes(original);
            var s = store.Streamer.Received();

            store.Received().Save(fileName, bytes, "temp", version.Format.MimeType, data.Identifier, false, null, (byte)version.Format.Quality);
        }
Ejemplo n.º 8
0
        public void CompareToTest()
        {
            var a = ImageVersion.Parse("1.23.456");
            var b = ImageVersion.Parse("1.23");
            var c = ImageVersion.Parse("01.023");
            var d = ImageVersion.Parse("1.23.457");

            Assert.Equal(1, a.CompareTo(b));
            Assert.Equal(-1, b.CompareTo(a));
            Assert.Equal(0, b.CompareTo(c));
            Assert.Equal(-1, a.CompareTo(d));
            Assert.Equal(1, d.CompareTo(a));
        }
        private IReadOnlyDictionary <string, IImageVersion> Versions()
        {
            var versions = new Dictionary <string, IImageVersion>();
            var thumb    = new ImageVersion
            {
                Width  = 100,
                Format = new GifFormat {
                    Quality = 10
                },
            };

            versions.Add("test", thumb);
            return(new ReadOnlyDictionary <string, IImageVersion>(versions));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Returns the ImageVersion that is most suitable to display at a certain
        /// width (in pixels)
        /// </summary>
        private ImageVersion GetBestFit(int width)
        {
            // Reference for the following
            //https://tiny.cc/4dbfcz

            int index = Versions.BinarySearch(ImageVersion.CustomVersion(width));

            // Width is below smallest option, return smallest option
            if (width <= Versions[0].Width)
            {
                return(Versions[0]);
            }

            // Required width matches a version perfectly
            if (0 < index)
            {
                return(Versions[index]);
            }

            // Get the bitwise complement of the value returned by the binary search
            index = ~index;

            // If the complement is equal to the list count, then the requested value
            // was higher than what we have available, so return our biggest version
            if (index == Versions.Count)
            {
                // If the requested size is more than 10% larger than the largest version,
                // return the original file
                if (width > Versions.Last().Width * 1.1)
                {
                    return(null);
                }

                // Otherwise return the largest of the reduced versions
                return(Versions[index - 1]);
            }

            // Otherwise get the image version above and the version below
            ImageVersion above, below;

            below = Versions[index - 1];
            above = Versions[index];

            // If the requested width is less than 10% larger than the smaller image, return the
            // smaller image, otherwise return the larger one.
            return((width - below.Width < (above.Width - below.Width) * 0.1) ? below : above);
        }
Ejemplo n.º 11
0
        public async Task Process()
        {
            var bytes = File.ReadAllBytes(Environment.CurrentDirectory + @"\icon.png");
            var data = new ImageQueued()
            {
                Identifier = Guid.NewGuid(),
                FileNameFormat = "good_{0}_file",
                OriginalExtension = string.Empty,
            };
            var versions = new Dictionary<string, IImageVersion>();
            var version = new ImageVersion()
            {
                Width = 100,
                Height = 100,
                Format = new JpegFormat { Quality = 70 },
            };
            versions.Add("temp", version);
            var fileName = string.Format(data.FileNameFormat, "temp");
            var original = string.Format(data.FileNameFormat, Naming.Original);

            var imaging = Substitute.For<IImaging>();
            imaging.Resize(bytes, version).Returns(bytes);
            var naming = Substitute.For<INaming>();
            naming.OriginalFileName(data).Returns(original);
            naming.FileName(data, "temp", version.Format.DefaultExtension).Returns(fileName);
            var streamer = Substitute.For<IStreamer>();
            streamer.Bytes(original).Returns(Task.FromResult(bytes));
            var store = Substitute.For<IDataStore>();
            store.Streamer.Returns(streamer);
            store.Save(fileName, bytes, "temp", version.Format.MimeType, data.Identifier, false, null, (byte)version.Format.Quality);

            var ip = new Processor(store, versions, imaging, naming);
            var result = await ip.Process(data);

            Assert.IsTrue(result);

            imaging.Received().Resize(bytes, version);
            naming.Received().OriginalFileName(data);
            naming.Received().FileName(data, "temp", version.Format.DefaultExtension);
            streamer.Received().Bytes(original);
            var s = store.Streamer.Received();
            store.Received().Save(fileName, bytes, "temp", version.Format.MimeType, data.Identifier, false, null, (byte)version.Format.Quality);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// The method of setting pictures on the form.
        /// </summary>
        /// <param name="imageVersion"> Version of image. </param>
        /// <param name="bitmapImage"> Bitmap to be set on form. </param>
        private void SetImageOnForm(ImageVersion imageVersion, Bitmap bitmapImage)
        {
            switch (imageVersion)         // version of image
            {
            case ImageVersion.LEFT_IMAGE: // set left image on form
                this.pictureBoxLeft.Source = this.BitmapToImageSource(bitmapImage);
                this.leftImageSet          = true;
                break;

            case ImageVersion.RIGHT_IMAGE:     // set right image on form
                this.pictureBoxRight.Source = this.BitmapToImageSource(bitmapImage);
                this.rightImageSet          = true;
                break;

            case ImageVersion.ANAGLYPH_IMAGE:     // set anaglyph image on form
                this.pictureBoxAnaglyph.Source = this.BitmapToImageSource(bitmapImage);
                break;
            }
        }
        public void Resize()
        {
            var dir     = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            var f       = dir.Substring(6, dir.Length - 6) + @"\icon.png";
            var bytes   = File.ReadAllBytes(f);
            var version = new ImageVersion()
            {
                Format = new GifFormat(),
                Width  = 10,
                Height = 10,
            };

            var i    = new Imaging();
            var data = i.Resize(bytes, version);

            Assert.IsNotNull(data);
            var size = i.Size(data);

            Assert.AreEqual(version.Width, size.Width);
            Assert.AreEqual(version.Height, size.Height);
        }
Ejemplo n.º 14
0
        public static async Task <ImageAndOsType> UpdateImageAndOsTypeAsync(
            this IClient client,
            ImageAndOsType imageAndOsType,
            string resourceGroupName,
            string imageName,
            string location)
        {
            if (imageAndOsType != null)
            {
                return(imageAndOsType);
            }

            var compute = client.GetClient <ComputeManagementClient>();

            if (imageName.Contains(':'))
            {
                if (location == null)
                {
                    return(null);
                }

                var imageArray = imageName.Split(':');
                if (imageArray.Length != 4)
                {
                    throw new InvalidOperationException(
                              string.Format(Resources.InvalidImageName, imageName));
                }
                var image = new ImageReference
                {
                    Publisher = imageArray[0],
                    Offer     = imageArray[1],
                    Sku       = imageArray[2],
                    Version   = imageArray[3],
                };

                if (image.Version.ToLower() == "latest")
                {
                    var images = await compute.VirtualMachineImages.ListAsync(
                        location, image.Publisher, image.Offer, image.Sku);

                    // According to Compute API:
                    // "The allowed formats are Major.Minor.Build or 'latest'.
                    //  Major, Minor, and Build are decimal numbers."
                    image.Version = images
                                    .Select(i => ImageVersion.Parse(i.Name))
                                    .Aggregate((a, b) => a.CompareTo(b) < 0 ? b : a)
                                    .ToString();
                }
                var imageModel = await compute.VirtualMachineImages.GetAsync(
                    location, image.Publisher, image.Offer, image.Sku, image.Version);

                return(new ImageAndOsType(
                           imageModel.OsDiskImage.OperatingSystem,
                           image,
                           imageModel.DataDiskImages.GetLuns()));
            }
            else if (imageName.Contains("/"))
            {
                var resourceId = ResourceId.TryParse(imageName);
                if (resourceId == null ||
                    resourceId.ResourceType.Namespace != ComputeStrategy.Namespace ||
                    (resourceId.ResourceType.Provider != "images") &&
                    resourceId.ResourceType.Provider != "galleries")
                {
                    throw new ArgumentException(string.Format(Resources.ComputeInvalidImageName, imageName));
                }

                if (resourceId.ResourceType.Provider == "galleries")
                {
                    var compute2 = client.GetClient <ComputeManagementClient>();
                    compute2.SubscriptionId = resourceId.SubscriptionId;
                    return(await compute2.GetGalleryImageAndOsTypeAsync(resourceId.ResourceGroupName, imageName));
                }
                else
                {
                    return(await compute.GetImageAndOsTypeAsync(resourceId.ResourceGroupName, resourceId.Name));
                }
            }
            else
            {
                try
                {
                    return(await compute.GetImageAndOsTypeAsync(resourceGroupName, imageName));
                }
                catch
                {
                }

                // get generic image
                var result = Images
                             .Instance
                             .SelectMany(osAndMap => osAndMap
                                         .Value
                                         .Where(nameAndImage => nameAndImage.Key.ToLower() == imageName.ToLower())
                                         .Select(nameAndImage => new ImageAndOsType(
                                                     osAndMap.Key == "Windows"
                                ? OperatingSystemTypes.Windows
                                : OperatingSystemTypes.Linux,
                                                     nameAndImage.Value,
                                                     null)))
                             .FirstOrDefault();

                if (result == null)
                {
                    throw new ArgumentException(string.Format(Resources.ComputeNoImageFound, imageName));
                }

                return(result);
            }
        }
Ejemplo n.º 15
0
        public void Resize()
        {
            var bytes = File.ReadAllBytes(Environment.CurrentDirectory + @"\icon.png");
            var version = new ImageVersion()
            {
                Format = new GifFormat(),
                Width = 10,
                Height = 10,
            };

            var i = new Imaging();
            var data = i.Resize(bytes, version);

            Assert.IsNotNull(data);
            var size = i.Size(data);
            Assert.AreEqual(version.Width, size.Width);
            Assert.AreEqual(version.Height, size.Height);
        }