Esempio n. 1
0
        public static string SaveAndCropColunista(HttpPostedFileBase image, string path, int cropX = 0, int cropY = 0, int cropWidth = 0, int cropHeight = 0, bool keepFileName = false, string fileNameKeep = "")
        {
            var fileName = keepFileName ? (!string.IsNullOrEmpty(fileNameKeep) ? fileNameKeep : GetFileName(image.FileName, path)) : GetFileName(image.FileName, path);
            var mimeType = MimeMapping.GetMimeMapping(fileName);

            using (var origem = Image.FromStream(image.InputStream, true, true))
            {
                //var media = Imager.Resize(origem, 620, 200, true);
                Image media = null;

                int startX = cropX;
                int startY = cropY;

                media = Imager.Crop(origem, new Rectangle(startX, startY, cropWidth, cropHeight));

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

                var imageCodecInfo = Imager.GetEncoderInfo(mimeType);

                // Imager.SaveJpeg(path + fileName, media != null ? media : origem);
                Imager.Save(path + fileName, media != null ? media : origem, imageCodecInfo);
            }

            return(fileName);
        }
Esempio n. 2
0
        public static string resizeImageAndSave2(HttpPostedFileBase Imagem, int width, int heigth, string path)
        {
            using (var origem = Image.FromStream(Imagem.InputStream, true, true))
            {
                var mimeType = MimeMapping.GetMimeMapping(Imagem.FileName);

                var media = Imager.Resize(origem, width, heigth, true);

                Imager.Save(path + Imagem.FileName, media != null ? media : origem, Imager.GetEncoderInfo(mimeType));

                return(Imagem.FileName);
            }
        }
Esempio n. 3
0
        //DONT USE THIS IF YOU NEED TO ALLOW LARGE FILES UPLOADS
        //Credit to i-e-b and his ASP.Net uploader for the bulk of the upload helper methods - https://github.com/i-e-b/jQueryFileUpload.Net
        private void UploadWholeFile(HttpRequestBase request, List <ViewDataUploadFile> statuses)
        {
            for (var i = 0; i < request.Files.Count; i++)
            {
                var file      = request.Files[i];
                var fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                var idGaleria = request.Form["galeria_id"];
                var mimeType  = MimeMapping.GetMimeMapping(file.FileName);
                var ext       = Path.GetExtension(file.FileName);

                var foto = new Midias
                {
                    legenda   = request.Form["legenda"],
                    midia     = fileName + ext,
                    excluido  = false,
                    video     = false,
                    idGaleria = int.Parse(request.Form["galeria_id"])
                };

                db.Midias.Add(foto);

                db.SaveChanges();

                var path = Path.Combine(Server.MapPath("/admin/Conteudo/Galeria/"), idGaleria);

                //var j = 0;
                //while(System.IO.File.Exists(Path.Combine(path, fileName))) {
                //    j++;
                //    fileName = Path.GetFileNameWithoutExtension(file.FileName) + "_" + j + ".jpg";
                //}

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                    Directory.CreateDirectory(path + "/min");
                }

                using (var origem = Image.FromStream(file.InputStream, true, true))
                {
                    var imageCodecInfo = Imager.GetEncoderInfo(mimeType);

                    // Imager.SaveJpeg(path + fileName, media != null ? media : origem);
                    // Imager.Save(path + fileName, media != null ? media : origem, imageCodecInfo);

                    Imager.Save(path + @"\" + fileName + ext, origem, imageCodecInfo);

                    var media = Imager.Resize(origem, 117, 73, true);

                    // Imager.SaveJpeg(path + @"\min\" + fileName, media);
                    Imager.Save(path + @"\min\" + fileName + ext, media, imageCodecInfo);
                }

                statuses.Add(new ViewDataUploadFile
                {
                    url           = "/admin/Conteudo/Galeria/" + idGaleria + "/" + fileName + ext,
                    thumbnail_url = "/admin/Conteudo/Galeria/" + idGaleria + "/min/" + fileName + ext,
                    name          = fileName + ext,
                    type          = file.ContentType,
                    size          = file.ContentLength,
                    delete_url    = "/Admin/Fotos/Delete/" + foto.id,
                    delete_type   = "DELETE"
                });
            }
        }
Esempio n. 4
0
        public static string SaveAndCropImage(HttpPostedFileBase image, string path, int cropX = 0, int cropY = 0, int cropWidth = 0, int cropHeight = 0, bool keepFileName = false, string fileNameKeep = "")
        {
            var fileName = keepFileName ? (!string.IsNullOrEmpty(fileNameKeep) ? fileNameKeep : GetFileName(image.FileName, path)) : GetFileName(image.FileName, path);
            var mimeType = MimeMapping.GetMimeMapping(fileName);

            using (var origem = Image.FromStream(image.InputStream, true, true))
            {
                //var media = Imager.Resize(origem, 620, 200, true);
                Image media = null;


                double croppedHeightToWidth = (double)cropHeight / cropWidth;
                double croppedWidthToHeight = (double)cropWidth / cropHeight;
                int    startX = cropX;
                int    startY = cropY;


                #region
                if (origem.Width > cropWidth && origem.Height > cropHeight)
                {
                    if (cropWidth > 0 && cropHeight > 0)
                    {
                        //try to get the center of image
                        #region GetCenter
                        if (origem.Width > origem.Height)
                        {
                            cropWidth = (int)(Math.Round(origem.Height * croppedWidthToHeight));

                            if (cropWidth < origem.Width)
                            {
                                cropHeight = origem.Height;
                                startX     = (origem.Width - cropWidth) / 2;
                            }
                            else
                            {
                                cropHeight = (int)Math.Round(origem.Height * ((double)origem.Width / cropWidth));
                                cropWidth  = origem.Width;
                                startY     = (origem.Height - cropHeight) / 2;
                            }
                        }
                        else
                        {
                            cropHeight = (int)(Math.Round(origem.Width * croppedHeightToWidth));
                            if (cropHeight < origem.Height)
                            {
                                cropWidth = origem.Width;
                                startY    = (origem.Height - cropHeight) / 2;
                            }
                            else
                            {
                                cropWidth  = (int)Math.Round(origem.Width * ((double)origem.Height / cropHeight));
                                cropHeight = origem.Height;
                                startX     = (origem.Width - cropWidth) / 2;
                            }
                        }
                        #endregion
                    }
                    media = Imager.Crop(origem, new Rectangle(startX, startY, cropWidth, cropHeight));
                }
                #endregion


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

                var imageCodecInfo = Imager.GetEncoderInfo(mimeType);

                // Imager.SaveJpeg(path + fileName, media != null ? media : origem);
                Imager.Save(path + fileName, media != null ? media : origem, imageCodecInfo);
            }

            return(fileName);
        }