Exemplo n.º 1
0
        public async Task <IActionResult> ResizeImage([FromForm] ImageResizeDTO imageResizeDTO)
        {
            try
            {
                var sw = new Stopwatch();
                sw.Start();

                var timestamps = new TimestampDTO();

                if (imageResizeDTO.Image.Length <= 0)
                {
                    return(BadRequest());
                }

                using var memStream = new MemoryStream();

                await imageResizeDTO.Image.CopyToAsync(memStream);

                timestamps.t1 = sw.ElapsedMilliseconds;

                using var image = new Bitmap(memStream);

                timestamps.t2 = sw.ElapsedMilliseconds;

                var resized = new Bitmap(imageResizeDTO.ResizeWidth, imageResizeDTO.ResizeHeight);
                using var graphics = Graphics.FromImage(resized);

                timestamps.t3 = sw.ElapsedMilliseconds;

                graphics.CompositingQuality = CompositingQuality.HighSpeed;
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                graphics.CompositingMode    = CompositingMode.SourceCopy;
                graphics.DrawImage(image, 0, 0, imageResizeDTO.ResizeWidth, imageResizeDTO.ResizeHeight);

                timestamps.t4 = sw.ElapsedMilliseconds;

                using var outStream = new MemoryStream();
                resized.Save(outStream, ImageFormat.Jpeg);

                sw.Stop();

                timestamps.t5 = sw.ElapsedMilliseconds;

                _timestamps.AddResizeTimestamps(timestamps);

                return(new FileContentResult(outStream.ToArray(), "image/jpeg"));
            }catch (Exception ex)
            {
                Console.WriteLine($"{ex}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Exemplo n.º 2
0
        public ObjectResult Post()
        {
            var results = new BaseResponse <AdImageDTO>();

            if (httpContextAccessor.HttpContext != null &&
                httpContextAccessor.HttpContext.Request != null &&
                httpContextAccessor.HttpContext.Request.Form != null &&
                httpContextAccessor.HttpContext.Request.Form.Files != null &&
                httpContextAccessor.HttpContext.Request.Form.Files.Any() &&
                httpContextAccessor.HttpContext.Request.Form.ContainsKey("guid"))

            {
                var guid = Guid.Empty;

                if (Guid.TryParse(httpContextAccessor.HttpContext.Request.Form["guid"], out guid))
                {
                    adImagePath.SetGuidProduct(guid);
                    var relative = adImagePath.RelativePath();
                    var path     = adImagePath.PhysicalPath();
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    try
                    {
                        foreach (var file in httpContextAccessor.HttpContext.Request.Form.Files)
                        {
                            var fileInfo = new FileInfo(file.FileName);

                            var guidSplit = Guid.NewGuid().ToString().Split('-');

                            var newFileName = string.Concat(guidSplit[0], fileInfo.Extension);

                            var pathOriginalFile = adImagePath.FullPhysicalPath(fileInfo.Name);

                            var relativePathFile = adImagePath.FullRelativePath(newFileName);

                            using (var fileStream = new FileStream(pathOriginalFile, FileMode.Create))
                            {
                                file.CopyTo(fileStream);
                            }

                            var imageResizeDTO = new ImageResizeDTO(pathOriginalFile, path, newFileName, 600, 600, true);

                            var resizeReturn = imageHelper.Resize(imageResizeDTO);

                            if (resizeReturn.IsOk())
                            {
                                var index = 0;
                                int.TryParse(httpContextAccessor.HttpContext.Request.Form[fileInfo.Name], out index);

                                var dto = new AdImageDTO
                                {
                                    Index       = index,
                                    GuidProduct = guid,
                                    File        = newFileName,
                                    Full        = relativePathFile,
                                    idOwner     = GetOwner()
                                };

                                var request = new BaseRequest <AdImageDTO>(dto);

                                var response = adImageService.Save(request);

                                results.Add(response.Dto);
                            }
                        }

                        base.PrepareResult <BaseResponse <AdImageDTO> >(results);
                    }
                    catch (Exception ex)
                    {
                        base.PrepareBadRequestResult <BaseResponse <AdImageDTO> >(results);
                    }
                }
                else
                {
                    base.PrepareBadRequestResult();
                }
            }
            else
            {
                base.PrepareBadRequestResult();
            }

            return(result);
        }