Пример #1
0
        //convert file size to 500X500 and save it on disk
        public void SaveImage(IFormFile file, IHostingEnvironment env, string currentFolder)
        {
            string fileName = Path.GetFileName(file.FileName);

            fileName = DisplayUtils.RenameDuplicates(env, currentFolder, fileName);
            string filePath = Path.Combine(env.WebRootPath, currentFolder, fileName);

            var format = GetFormat(filePath);

            using (var imageStream = file.OpenReadStream())
                using (var inputStream = new SKManagedStream(imageStream))
                    using (var codec = SKCodec.Create(inputStream))
                        using (var original = SKBitmap.Decode(codec))
                            using (var image = HandleOrientation(original, codec.Origin))
                            //using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                            {
                                int width  = imageSize;
                                int height = (int)Math.Round(width * ((float)image.Height / image.Width));

                                var info = new SKImageInfo(width, height);

                                using (var resized = image.Resize(info, SKBitmapResizeMethod.Lanczos3))
                                    using (var thumb = SKImage.FromBitmap(resized))
                                        using (var fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
                                        {
                                            thumb.Encode(format, Quality)
                                            .SaveTo(fs);
                                        }


                                //imageProcessor.CreateThumbnails(imageStream, filePath);
                                //await file.CopyToAsync(fileStream);
                            }
        }