示例#1
0
        public static void SaveMetaFile(
            Stream source,
            Stream destination,
            float scale                  = 4f,
            Color?backgroundColor        = null,
            ImageFormat format           = null,
            EncoderParameters parameters = null)

        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            using (var img = new Metafile(source))
            {
                var f = format ?? ImageFormat.Png;

                //Determine default background color.
                //Not all formats support transparency.
                if (backgroundColor == null)
                {
                    var transparentFormats  = new ImageFormat[] { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Wmf, ImageFormat.Emf };
                    var isTransparentFormat = transparentFormats.Contains(f);

                    backgroundColor = isTransparentFormat ? Color.Transparent : Color.White;
                }

                //header contains DPI information
                var header = img.GetMetafileHeader();

                //calculate the width and height based on the scale
                //and the respective DPI
                var width  = (int)Math.Round((scale * img.Width / header.DpiX * 100), 0, MidpointRounding.ToEven);
                var height = (int)Math.Round((scale * img.Height / header.DpiY * 100), 0, MidpointRounding.ToEven);

                using (var bitmap = new Bitmap(width, height))
                {
                    using (var g = System.Drawing.Graphics.FromImage(bitmap))
                    {
                        //fills the background
                        g.Clear(backgroundColor.Value);

                        //reuse the width and height to draw the image
                        //in 100% of the square of the bitmap
                        g.DrawImage(img, 0, 0, bitmap.Width, bitmap.Height);
                    }

                    //get codec based on GUID
                    var codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == f.Guid);

                    bitmap.Save(destination, codec, parameters);
                }
            }
        }
        public static bool ValidateImage(this IFormFile file, ModelStateDictionary ModelState)
        {
            // Allowed image formats
            ImageFormat[] allowedImageFormats = new ImageFormat[]
            {
                ImageFormat.Bmp,
                ImageFormat.Gif,
                ImageFormat.Jpeg,
                ImageFormat.Png,
                ImageFormat.Tiff
            };

            string ext = Path.GetExtension(file.FileName).ToLower();

            if (!DataConstants.AllowedImageExtensions.Contains(ext))
            {
                string allowedImageExtensions = string.Join(", ", DataConstants.AllowedImageExtensions);
                ModelState.AddModelError(string.Empty, $"Allowed image extensions {allowedImageExtensions}");

                return(false);
            }

            if (file.Length > DataConstants.PosterMaxLength)
            {
                string mb = $"{DataConstants.ThumbnailMaxLength / 1024 / 1024} MB";
                ModelState.AddModelError(string.Empty, $"Allowed max file size {mb}");

                return(false);
            }

            try
            {
                // Create an Image from the specified data stream
                using (Image img = Image.FromStream(file.OpenReadStream()))
                {
                    // Return true if the image format is allowed
                    if (!allowedImageFormats.Contains(img.RawFormat))
                    {
                        ModelState.AddModelError(string.Empty, $"{file.FileName} has no valid image format.");

                        return(false);
                    }
                }
            }
            catch
            {
                ModelState.AddModelError(string.Empty, $"{file.FileName} is not a valid image.");

                return(false);
            }

            return(true);
        }
示例#3
0
        public static bool IsValidFileType(HttpPostedFileBase file)
        {
            bool flag;

            if ((file == null ? false : file.ContentLength > 0))
            {
                ImageFormat[] jpeg = new ImageFormat[] { ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif, ImageFormat.Bmp, ImageFormat.Jpeg, ImageFormat.Tiff, ImageFormat.Icon };
                using (Image image = Image.FromStream(file.InputStream))
                {
                    if (!jpeg.Contains <ImageFormat>(image.RawFormat))
                    {
                        flag = false;
                        return(flag);
                    }
                }
            }
            flag = true;
            return(flag);
        }