Ejemplo n.º 1
0
        public ActionResult OnPostCriarMiniatura()
        {
            MemoryStream stream = new MemoryStream();

            Request.Body.CopyTo(stream);
            stream.Position = 0;

            //Exemplo de retorno
            List <string> lstString = new List <string>();

            using (StreamReader reader = new StreamReader(stream))
            {
                string requestBody = reader.ReadToEnd();
                if (requestBody.Length > 0)
                {
                    var obj = JsonConvert.DeserializeObject <ImagemNoticia>(requestBody);
                    if (obj != null)
                    {
                        ImagemNoticia i = obj;
                        i.idNoticia = this.Noticia.ID;

                        using (Image <Rgba32> thumb = Image.Load(hostingEnvironment.WebRootPath + i.ImagemCaminho))
                        {
                            Rectangle cropRectangle = new Rectangle(i.cropPointX, i.cropPointY, i.imageCropWidth, i.imageCropHeight);
                            thumb.Mutate(ctx => ctx.Crop(cropRectangle));
                            thumb.Mutate(ctx => ctx.Resize(150, 0));
                            thumb.Save(hostingEnvironment.WebRootPath + i.ThumbCaminho);
                            lstString.Add("~" + i.ThumbCaminho);
                        }
                    }
                }
            }
            return(new JsonResult(lstString));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Noticia.Add(Noticia);
            await _context.SaveChangesAsync();

            if (this.Imagem != null)
            {
                ImagemNoticia i = new ImagemNoticia();
                i.imagemNome = GetUniqueName(this.Imagem.FileName);
                i.idNoticia  = this.Noticia.ID;

                if (!Directory.Exists(hostingEnvironment.WebRootPath + i.ImagemPasta))
                {
                    Directory.CreateDirectory(hostingEnvironment.WebRootPath + i.ImagemPasta);
                }

                if (!Directory.Exists(hostingEnvironment.WebRootPath + i.ThumbPasta))
                {
                    Directory.CreateDirectory(hostingEnvironment.WebRootPath + i.ThumbPasta);
                }

                using (var stream = new FileStream(hostingEnvironment.WebRootPath + i.ImagemCaminho, FileMode.Create))
                {
                    this.Imagem.CopyTo(stream);
                    stream.Dispose();
                }

                using (Image <Rgba32> img = Image.Load(hostingEnvironment.WebRootPath + i.ImagemCaminho))
                {
                    if (img.Width > 1200)
                    {
                        img.Mutate(ctx => ctx.Resize(1200, 0));
                    }
                    img.Save(hostingEnvironment.WebRootPath + i.ImagemCaminho);

                    img.Mutate(ctx => ctx.Resize(150, 0));
                    img.Save(hostingEnvironment.WebRootPath + i.ThumbCaminho);
                }

                this.Noticia.Imagem = i.imagemNome;
                await _context.SaveChangesAsync();
            }


            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 3
0
        public ActionResult OnPostEnviarFoto()
        {
            MemoryStream stream = new MemoryStream();

            Request.Body.CopyTo(stream);
            stream.Position = 0;

            using (StreamReader reader = new StreamReader(stream))
            {
                string requestBody = reader.ReadToEnd();
                if (requestBody.Length > 0)
                {
                    var obj = JsonConvert.DeserializeObject <ImagemNoticia>(requestBody);
                    if (obj != null)
                    {
                        ImagemNoticia i = new ImagemNoticia();
                        i.imagem     = obj.imagem;
                        i.idNoticia  = this.Noticia.ID;
                        i.imagemNome = obj.imagem.FileName;

                        if (!Directory.Exists(hostingEnvironment.WebRootPath + i.ImagemPasta))
                        {
                            Directory.CreateDirectory(hostingEnvironment.WebRootPath + i.ImagemPasta);
                        }

                        using (var stream2 = new FileStream(hostingEnvironment.WebRootPath + i.ImagemCaminho, FileMode.Create))
                        {
                            i.imagem.CopyTo(stream2);
                            stream2.Dispose();
                        }

                        using (Image <Rgba32> img = Image.Load(hostingEnvironment.WebRootPath + i.ImagemCaminho))
                        {
                            if (img.Width > 1200)
                            {
                                img.Mutate(ctx => ctx.Resize(1200, 0));
                            }
                            img.Save(hostingEnvironment.WebRootPath + i.ImagemCaminho);
                        }
                        return(new JsonResult("~" + i.ImagemCaminho));
                    }
                }
            }

            return(new JsonResult(""));
        }
Ejemplo n.º 4
0
        public ActionResult OnPostUpload(List <IFormFile> files)
        {
            if (files != null && files.Count > 0)
            {
                foreach (IFormFile item in files)
                {
                    if (item.Length > 0)
                    {
                        ImagemNoticia i = new ImagemNoticia();
                        i.imagem     = item;
                        i.idNoticia  = this.Noticia.ID;
                        i.imagemNome = item.FileName;

                        if (!Directory.Exists(hostingEnvironment.WebRootPath + i.ImagemPasta))
                        {
                            Directory.CreateDirectory(hostingEnvironment.WebRootPath + i.ImagemPasta);
                        }

                        using (var stream2 = new FileStream(hostingEnvironment.WebRootPath + i.ImagemCaminho, FileMode.Create))
                        {
                            i.imagem.CopyTo(stream2);
                            stream2.Dispose();
                        }

                        using (Image <Rgba32> img = Image.Load(hostingEnvironment.WebRootPath + i.ImagemCaminho))
                        {
                            if (img.Width > 1200)
                            {
                                img.Mutate(ctx => ctx.Resize(1200, 0));
                            }
                            img.Save(hostingEnvironment.WebRootPath + i.ImagemCaminho);
                        }
                        return(new JsonResult("~" + i.ImagemCaminho));
                    }
                }
            }
            return(this.Content("Fail"));
        }