[RESOLVED]I need iformation of my image and resize it

Hi, i i have my code to upload my picture :

if (ModelState.IsValid)
            {
               
                
                // update each field manually
                foreach (var file in files)
                {
                    if (file != null)
                    {
                        if (file.ContentLength > 0)
                        {

                            var myuploadextension = Path.GetExtension(file.FileName);

                            if (myuploadextension.ToUpper() == ".JPEG" || myuploadextension.ToUpper() == ".JPG" || myuploadextension.ToUpper() == ".BMP" || myuploadextension.ToUpper() == ".PNG" || myuploadextension.ToUpper() == ".GIF")
                        {


                            var sizeMb = (file.ContentLength / 1024f) / 1024f; //file.contentlength is in bytes

                            var todaydate = DateTime.Now.ToShortDateString();
                            var todaydatefriendly = todaydate.ImageFriendly();
                            var myuploadiwthoutextension = Path.GetFileNameWithoutExtension(file.FileName);
                            var myuploadiwthoutextensionfriendly = myuploadiwthoutextension.ImageFriendly();
                            var UserId = User.Identity.GetUserId();

                            var fileName = todaydatefriendly + "-" + UserId + "-" + myuploadiwthoutextensionfriendly + myuploadextension;
                            //var fileNameResize250 = todaydatefriendly + "-" + UserId + "-" + myuploadiwthoutextensionfriendly + "250" + myuploadextension;
                            var path = Path.Combine(Server.MapPath("~/Content/UserCreations"), fileName);
                            file.SaveAs(path);




                            //Language
                            var actualculturetocheck = "en";
                            var actualculture = this.Session["ActualCulture"];
                            if (actualculture != null)
                            {
                                actualculturetocheck = actualculture.ToString();
                            }

                            
                            //Ipaddress
                            var ipAddress = Request.ServerVariables["REMOTE_ADDR"] + " " + Request.UserHostAddress;

                            creation.UserId = User.Identity.GetUserId();
                            creation.CreationLanguage = actualculturetocheck;
                            creation.CreationVisible = true;
                            creation.CreationDelete = false;
                            creation.CreationDate = DateTime.Now;
                            creation.CreationIpAdress = ipAddress;
                            creation.CreationPhotoBis = fileName; //after add

                            db.Creations.Add(creation);
                            db.SaveChanges();



                        }


                        }
                    }
                }

                //UserId
                return RedirectToAction("CreationList", "Creation", new { UserId = User.Identity.GetUserId() });
            }

Now i would love to get the heght and the width of my image and resize the saved the imageanother 2 times

with width 25 and eight in proportion

with width 250 and eight in proportion

There is the System.Web.Helpers namespace

WebImage img = new WebImage(path);

int w = img.Width;
int h = img.Height;

img.Resize(new_width, new_height);

img.Save(new_path);

If you would need more functionality, you could consider http://imageresizing.net

You will need similar code, example

ImageResizer.ImageJob i1 = new ImageResizer.ImageJob(path, new string[] { "source.width", "source.height" });
i1.Build();

int width = Convert.ToInt32(i1.ResultInfo["source.width"]);
int height = Convert.ToInt32(i1.ResultInfo["source.height"]);

// to resize
ImageResizer.ImageJob i2 = new ImageResizer.ImageJob(path, new_path, new ImageResizer.Instructions("width=" + new_width));

i2.Build();

Hope this helps.

 

Leave a Reply