Exemplo n.º 1
0
        private IImageEncoder ConvertFormat(TextureImageFormat format)
        {
            ImageFormatManager imageFormatManager = new ImageFormatManager();
            IImageFormat       imageFormat        = null;

            if (format == TextureImageFormat.image_jpeg)
            {
                imageFormat = imageFormatManager.FindFormatByFileExtension(".jpg");
            }
            else
            {
                imageFormat = imageFormatManager.FindFormatByFileExtension(".png");
            }

            return(imageFormatManager.FindEncoder(imageFormat));
        }
Exemplo n.º 2
0
        public static string UploadImage(IFormFile file, string path, string fileName, int width, int height, bool maintainAspectRatio = false)
        {
            string fileExtension         = Path.GetExtension(file.FileName);
            string fileNameWithExtension = fileName + fileExtension;
            string pathWithFileName      = Path.Combine(path, fileNameWithExtension);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string[] files = Directory.GetFiles(path, fileName + ".*");
            foreach (string item in files)
            {
                File.Delete(item);
            }

            if (file.Length > 0)
            {
                using (var stream = new FileStream(pathWithFileName, FileMode.Create))
                {
                    using (Image <Rgba32> image = Image.Load(file.OpenReadStream()))
                    {
                        int destinationWidth  = width;
                        int destinationHeight = height;

                        int originalWidth  = image.Width;
                        int originalHeight = image.Height;

                        if (maintainAspectRatio && destinationWidth < originalWidth)
                        {
                            double ratio = 0;

                            ratio             = (double)destinationWidth / (double)originalWidth;
                            destinationWidth  = Convert.ToInt32(originalWidth * ratio);
                            destinationHeight = Convert.ToInt32(originalHeight * ratio);
                        }

                        image.Mutate(x => x.Resize(destinationWidth, destinationHeight));

                        ImageFormatManager imageFormatManager = new ImageFormatManager();
                        imageFormatManager.AddImageFormat(ImageFormats.Jpeg);
                        imageFormatManager.AddImageFormat(ImageFormats.Png);
                        image.Save(stream, imageFormatManager.FindFormatByFileExtension(fileExtension));
                    }
                }
            }

            return(fileNameWithExtension);
        }