private void VerifyBitmapImageConstraints(int templateCode, BitmapImageElementConstraints imageElementConstraints)
        {
            VerifyBinaryConstraints(templateCode, imageElementConstraints);

            if (imageElementConstraints.SupportedFileFormats.Any(x => !BitmapImageFileFormats.Contains(x)))
            {
                throw new TemplateValidationException(templateCode, TemplateElementValidationError.UnsupportedImageFileFormat);
            }

            if (imageElementConstraints.SupportedImageSizes == null)
            {
                throw new TemplateValidationException(templateCode, TemplateElementValidationError.MissingSupportedImageSizes);
            }

            if (!imageElementConstraints.SupportedImageSizes.Any())
            {
                throw new TemplateValidationException(templateCode, TemplateElementValidationError.EmptySupportedImageSizes);
            }

            if (imageElementConstraints.SupportedImageSizes.Contains(ImageSize.Empty))
            {
                throw new TemplateValidationException(templateCode, TemplateElementValidationError.InvalidImageSize);
            }

            if (imageElementConstraints.SupportedImageSizes.Any(x => x.Height < 0 || x.Width < 0))
            {
                throw new TemplateValidationException(templateCode, TemplateElementValidationError.NegativeImageSizeDimension);
            }
        }
Exemple #2
0
        public static void ValidateBitmapImageHeader(int templateCode, BitmapImageElementConstraints constraints, FileFormat fileFormat, Stream inputStream)
        {
            var imageInfo = ValidateBitmapImageFormat(templateCode, constraints, fileFormat, inputStream);

            if (constraints.SupportedImageSizes.All(x => imageInfo.Width != x.Width || imageInfo.Height != x.Height))
            {
                throw new InvalidBinaryException(templateCode, new ImageUnsupportedSizeError(new ImageSize {
                    Height = imageInfo.Height, Width = imageInfo.Width
                }));
            }
        }
Exemple #3
0
        public static string ValidateBitmapImage(int templateCode, BitmapImageElementConstraints constraints, Stream inputStream)
        {
            var imageFormats = constraints.SupportedFileFormats
                               .Aggregate(
                new List <string>(),
                (result, next) =>
            {
                if (BitmapImageFormatsMap.TryGetValue(next, out var imageFormat))
                {
                    result.Add(imageFormat);
                }

                return(result);
            });

            Image <Rgba32> image;
            IImageFormat   format;

            try
            {
                image = Image.Load(inputStream, out format);
            }
            catch (Exception)
            {
                throw new InvalidBinaryException(templateCode, new InvalidImageError());
            }

            using (image)
            {
                if (!imageFormats.Contains(format.DefaultMimeType, StringComparer.OrdinalIgnoreCase))
                {
                    throw new InvalidBinaryException(templateCode, new BinaryInvalidFormatError(format.Name.ToLowerInvariant()));
                }

                if (constraints.SupportedImageSizes.All(x => image.Width != x.Width || image.Height != x.Height))
                {
                    throw new InvalidBinaryException(templateCode, new ImageUnsupportedSizeError(new ImageSize {
                        Height = image.Height, Width = image.Width
                    }));
                }

                if (constraints.IsAlphaChannelRequired && !IsImageContainsAlphaChannel(image))
                {
                    throw new InvalidBinaryException(templateCode, new ImageAlphaChannelError());
                }
            }

            return(format.DefaultMimeType);
        }
        public static void ValidateBitmapImage(int templateCode, BitmapImageElementConstraints constraints, Stream inputStream)
        {
            if (!constraints.IsAlphaChannelRequired)
            {
                return;
            }

            Image <Rgba32> decodedImage;

            try
            {
                inputStream.Position = 0;
                decodedImage         = Image.Load(inputStream);
            }
            catch (Exception)
            {
                throw new InvalidBinaryException(templateCode, new InvalidImageError());
            }

            if (!IsImageContainsAlphaChannel(decodedImage))
            {
                throw new InvalidBinaryException(templateCode, new ImageMissingAlphaChannelError());
            }
        }