Ejemplo n.º 1
0
        private static string GetResizedImagePath(string filepath, int width, int height)
        {
            string resizedPath = filepath;

            if (width > 0 || height > 0)
            {
                resizedPath = filepath.GetPathForResizedImage(width, height);

                if (!Directory.Exists(resizedPath))
                    Directory.CreateDirectory(new FileInfo(resizedPath).DirectoryName);

                var originalFile = new FileInfo(filepath);
                var resizedFile = new FileInfo(resizedPath);
                if (originalFile.LastWriteTimeUtc > resizedFile.LastWriteTimeUtc)
                {
                    File.Delete(resizedPath);
                }

                if (!File.Exists(resizedPath))
                {
                    var imageResizer = new ImageResizer(filepath);
                    if (width > 0 && height > 0)
                    {
                        imageResizer.Resize(width, height, ImageEncoding.Jpg90);
                    }
                    else if (width > 0)
                    {
                        imageResizer.Resize(width, ImageEncoding.Jpg90);
                    }
                    imageResizer.SaveToFile(resizedPath);
                    imageResizer.Dispose();
                }
            }
            return resizedPath;
        }
Ejemplo n.º 2
0
        protected override void WriteFile(HttpResponseBase response)
        {
            var imageRisizer = new ImageResizer(this.FileContents);

            int width = _width ?? 0;
            int height = _height ?? 0;

            if (!_width.HasValue || !_height.HasValue)
            {
                using (Image originalImage = Image.FromStream(new MemoryStream(this.FileContents)))
                {
                    width = _width ?? originalImage.Width;
                    height = _width ?? originalImage.Height;
                }
            }

            var resizedImage = imageRisizer.Resize(width, height, true, ImageEncoding.Png);

            using (var ms = new MemoryStream(resizedImage))
            {
                ms.WriteTo(response.OutputStream);
            }
        }
Ejemplo n.º 3
0
        // called when a user uploads images recieving the listing id
        public ActionResult Upload(int id)
        {
            int listingId = id;
            CoreysListEntities db = new CoreysListEntities();

            // for each of the requested files
            for (int i = 0; i < Request.Files.Count; i++)
            {
                try
                {
                    // create a new [image] 
                    CoreysList.Entity.Image newImage = new CoreysList.Entity.Image();

                    // Uploaded file
                    HttpPostedFileBase file = Request.Files[i];

                    // Get the size of the file
                    newImage.ImageSize = file.ContentLength;

                    // get the file name
                    newImage.FileName = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);

                    // get the type of file .jpg .gif .png etc..
                    newImage.ImageType = file.ContentType;

                    // create a new byte array to fit the content size
                    byte[] imageData = new byte[file.ContentLength];

                    // read in the file withe the byte array and content size
                    file.InputStream.Read(imageData, 0, (int)file.ContentLength);

                    // reposition the input stream to the beginning
                    file.InputStream.Position = 0;

                    // stream the file again into a System.Drawing.Image
                    System.Drawing.Image sysImg = System.Drawing.Image.FromStream(file.InputStream);

                    // assign the sizes from system image to coreyslist image
                    newImage.ImageHeight = sysImg.Height;
                    newImage.ImageWidth = sysImg.Width;
                    newImage.ImageContent = imageData;

                    // create encoding object to send image type
                    ImageEncoding imgResizerEnc = new ImageEncoding();

                    // set the values for thumb images
                    int thumbHeight = 75;
                    int thumbWidth = 75;

                    // switch statement to get the content type 
                    switch (file.ContentType)
                    {
                        case "image/jpeg":
                            imgResizerEnc = ImageEncoding.Jpg90;
                            break;
                        case "image/gif":
                            imgResizerEnc = ImageEncoding.Gif;
                            break;
                        case "image/png":
                            imgResizerEnc = ImageEncoding.Png;
                            break;
                    }

                    // create a resizer and send the image content
                    ImageResizer resizer = new ImageResizer(imageData);

                    // call the resizer method along with the desired height, width and img type
                    byte[] thumbData = resizer.Resize(thumbHeight, thumbWidth, imgResizerEnc);

                    // save the new thumb data for the coreyslist image entity
                    newImage.ThumbContent = thumbData;
                    newImage.ThumbSize = thumbData.Length;
                    newImage.ThumbWidth = thumbWidth;
                    newImage.ThumbHeight = thumbHeight;

                    // connect image to the correct listing through listing ID
                    newImage.ListingID = listingId;
                    newImage.CreatedDate = DateTime.Now;
                    newImage.CreatedBy = System.Web.HttpContext.Current.Session["UserId"].ToString();

                    // To save file, use SaveAs method
                    db.Images.Add(newImage);
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            // Return partial view 
            EditListingImagesViewModel editListingImagesViewModel = new EditListingImagesViewModel(listingId);
            return PartialView("~/views/Accounts/_EditListingImages.cshtml", editListingImagesViewModel);
        }
        public JsonResult FileUpload()
        {
            var retList = new LinkedList<RetJsonModel>();
            for (var i = 0; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i];
                if (file == null) continue;
                var ret = new RetJsonModel
                {
                    ContentType = file.ContentType,
                    UserId = Session["LoggedUserID"].ToString()
                };
                if (ret.ContentType.Contains("image/"))
                {
                    ret.IsAccept = 0;
                    ret.FileTypeAccept = "yes";
                    ret.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + '_' + i + '_' + file.FileName;
                    if (!string.IsNullOrEmpty(ret.UserId))
                    {

                        ret.Url = "/Content/Users/" + ret.UserId + "/" + ret.FileName;
                        var path = Path.Combine(Server.MapPath("~/Content/Users/" + ret.UserId + ""), ret.FileName);
                        var stream = file.InputStream;
                        using (var fileStream = System.IO.File.Create(path))
                        {
                            stream.CopyTo(fileStream);
                        }
                        var resizer = new ImageResizer(@path);
                        var thumbtailPath = Path.Combine(Server.MapPath("~/Content/Users/" + ret.UserId + "/thumbtail/"), ret.FileName);
                        resizer.Resize(400, 400, ImageEncoding.Jpg90);
                        if (!Directory.Exists(Server.MapPath("~/Content/Users/" + ret.UserId + "/thumbtail/")))
                            Directory.CreateDirectory(Server.MapPath("~/Content/Users/" + ret.UserId + "/thumbtail/"));
                        resizer.SaveToFile(@thumbtailPath);
                        var image = new ImageViewModel();
                        var imageMetaDate = new ImageMetaData(Server.MapPath(ret.Url));
                        image.FileName = ret.FileName;
                        image.UpdateDate = DateTime.Now;
                        image.UserId = long.Parse(ret.UserId);
                        image.Url = ret.Url;
                        image.ContentType = file.ContentType;
                        _db.Images.Add(image);
                        imageMetaDate.FetchData();
                        var metadata = imageMetaDate.GetMetaData();
                        _db.ImageMetaData.Add(metadata);
                        _db.SaveChanges();
                    }
                    else
                    {
                        ret.IsAccept = 1;
                        ret.Error = "Member Session Expired";
                    }

                }
                else
                {
                    ret.IsAccept = 1;
                    ret.FileName = file.FileName;
                    ret.Error = "Content Type Deny";
                }
                retList.AddLast(ret);
            }
            var javaScriptSerializer = new JavaScriptSerializer();
            var jsonString = javaScriptSerializer.Serialize(retList);
            return Json(jsonString);
        }
 public ActionResult UploadImage(int id, HttpPostedFileWrapper upload)
 {
     string ret;
     if (upload != null)
     {
         if (upload.ContentLength <= 1024 * 1024 * 5)
         {
             var imageName = DateTime.Now.ToString("yyyyMMddHHmmss") + '_' + upload.FileName;
             if (!System.IO.Directory.Exists(Server.MapPath("/Content/Users/" + id)))
             {
                 System.IO.Directory.CreateDirectory(Server.MapPath("/Content/users/" + id));
             }
             var path = System.IO.Path.Combine(Server.MapPath("/Content/users/" + id), imageName);
             var url = "/Content/users/" + id + "/" + imageName;
             upload.SaveAs(path);
             var resizer = new ImageResizer(@path);
             var thumbtailPath = System.IO.Path.Combine(Server.MapPath("~/Content/Users/" + id + "/thumbtail/"), upload.FileName);
             resizer.Resize(400, 400, ImageEncoding.Jpg90);
             if (!System.IO.Directory.Exists(Server.MapPath("~/Content/Users/" + id + "/thumbtail/")))
                 System.IO.Directory.CreateDirectory(Server.MapPath("~/Content/Users/" + id + "/thumbtail/"));
             resizer.SaveToFile(@thumbtailPath);
             var image = new ImageViewModel();
             var imageMetaDate = new ImageMetaData(Server.MapPath(image.Url));
             imageMetaDate.FetchData();
             var metadata = imageMetaDate.GetMetaData();
             image.ContentType = upload.ContentType;
             image.UpdateDate = DateTime.Now;
             image.UserId = long.Parse(Session["LoggedUserID"].ToString());
             image.Url = url;
             image.FileName = imageName;
             _db.Images.Add(image);
             _db.ImageMetaData.Add(metadata);
             _db.SaveChanges();
             ret = url;
         }
         else
         {
             ret = "<p style=\"color:red\">image exceed the maximum 5MB</p>";
         }
     }
     else
     {
         ret = "<p style=\"color:red\">image is empty</p>";
     }
     return Content(ret);
 }