Пример #1
0
        public ImageOptions(object[] attributes, string entityName)
        {
            var imageAttribute =
                attributes.OfType <ImageAttribute>().FirstOrDefault();
            var imageSettingsAttributes =
                attributes.OfType <ImageSettingsAttribute>().ToList();

            if (imageAttribute != null)
            {
                AllowedFileExtensions = imageAttribute.AllowedFileExtensions;
                MaxFileSize           = imageAttribute.MaxFileSize;
                NameCreation          = imageAttribute.NameCreation;
                IsMultiple            = imageAttribute.IsMulti;
                Settings = new List <ImageSettings>();
            }
            else
            {
                AllowedFileExtensions = FileUploadDefault.ImageExtensions;
                MaxFileSize           = FileUploadDefault.MaxFileSize;
                NameCreation          = NameCreation.OriginalFileName;
                Settings = new List <ImageSettings>();
            }

            if (imageSettingsAttributes.Any())
            {
                var length = imageSettingsAttributes.Count;
                Settings = new ImageSettings[length];

                for (var i = 0; i < length; i++)
                {
                    var settings = imageSettingsAttributes[i].Settings;
                    settings.IsBig       = imageSettingsAttributes[i].IsBig;
                    settings.IsMiniature = imageSettingsAttributes[i].IsMiniature;
                    Settings[i]          = settings;
                }
            }
            else
            {
                Settings = new[]
                {
                    new ImageSettings("Content/" + entityName)
                };
            }
        }
Пример #2
0
        private static byte[] ResizeImage(ImageSettings settings, Image img)
        {
            if (settings.Width.HasValue || settings.Height.HasValue)
            {
                if (settings.Width.HasValue && settings.Height.HasValue)
                {
                    using (var ms = new MemoryStream())
                    {
                        var imgJob = new ImageJob(img, ms, new Instructions(
                                                      new NameValueCollection
                        {
                            { "width", settings.Width.Value.ToString() },
                            { "height", settings.Height.Value.ToString() },
                            { "format", "jpg" },
                            { "mode", "crop" }
                        }));

                        imgJob.Build();

                        return(ms.ToArray());
                    }
                }
                if (settings.Width.HasValue)
                {
                    using (var ms = new MemoryStream())
                    {
                        var imgJob = new ImageJob(
                            img,
                            ms,
                            new Instructions(new NameValueCollection
                        {
                            { "width", settings.Width.Value.ToString() },
                            { "format", "jpg" },
                            { "mode", "crop" }
                        }));

                        imgJob.Build();

                        return(ms.ToArray());
                    }
                }
                using (var ms = new MemoryStream())
                {
                    var imgJob = new ImageJob(
                        img,
                        ms,
                        new Instructions(new NameValueCollection
                    {
                        { "height", settings.Height.Value.ToString() },
                        { "format", "jpg" },
                        { "mode", "crop" }
                    }));

                    imgJob.Build();

                    return(ms.ToArray());
                }
            }
            using (var ms = new MemoryStream())
            {
                var imgJob = new ImageJob(img, ms, new Instructions());

                imgJob.Build();

                return(ms.ToArray());
            }
        }
Пример #3
0
        private static void SaveImage(
            ImageSettings settings,
            Image img,
            string fileName)
        {
            var path = Path.Combine(BasePath, settings.SubPath, fileName);

            if (settings.Width.HasValue || settings.Height.HasValue)
            {
                if (settings.Width.HasValue && settings.Height.HasValue)
                {
                    var imgJob = new ImageJob(
                        img,
                        path,
                        new Instructions(new NameValueCollection
                    {
                        { "width", settings.Width.Value.ToString() },
                        { "height", settings.Height.Value.ToString() },
                        { "format", "jpg" },
                        { "mode", "crop" }
                    }))
                    {
                        CreateParentDirectory = true
                    };

                    imgJob.Build();
                }
                else if (settings.Width.HasValue)
                {
                    var imgJob = new ImageJob(
                        img,
                        path,
                        new Instructions(new NameValueCollection
                    {
                        { "width", settings.Width.Value.ToString() },
                        { "format", "jpg" },
                        { "mode", "crop" }
                    }))
                    {
                        CreateParentDirectory = true
                    };

                    imgJob.Build();
                }
                else
                {
                    var imgJob = new ImageJob(
                        img,
                        path,
                        new Instructions(new NameValueCollection
                    {
                        { "height", settings.Height.Value.ToString() },
                        { "format", "jpg" },
                        { "mode", "crop" }
                    }))
                    {
                        CreateParentDirectory = true
                    };

                    imgJob.Build();
                }
            }
            else
            {
                var imgJob = new ImageJob(img, path, new Instructions())
                {
                    CreateParentDirectory = true
                };

                imgJob.Build();
            }

            img.Dispose();
        }