locked
Why my upload code turn the image of 90° when resize the image, what is wrong? RRS feed

  • Question

  • User1991482975 posted

    Hi, i do not understa why, sometimes, not all the time... some images uploaded, when i reduce the size in my code, the image resized is saved as turned of 90°...

    This is my code in my controller :

            [HttpPost]
            [Authorize]
            [ValidateAntiForgeryToken]
            public ActionResult CreationUpload(CreationHairTagsModel creationbind, IEnumerable<HttpPostedFileBase> files)
            {
                if (ModelState.IsValid)
                {
                    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.ToString("yyyy-MM-dd HH:mm:ss");
                                    var todaydatefriendly = todaydate.ImageFriendly();
    
                                    var myuploadiwthoutextension = Path.GetFileNameWithoutExtension(file.FileName);
                                    int myuploadiwthoutextensionlen = (myuploadiwthoutextension.Length);
                                    if (myuploadiwthoutextensionlen > 50)
                                    {
                                        myuploadiwthoutextension = (myuploadiwthoutextension.Substring(0, 50));
                                    }
                                    var myuploadiwthoutextensionfriendly = myuploadiwthoutextension.ImageFriendly();
    
                                    var UserId = User.Identity.GetUserId();
                                    int UserIdlen = (UserId.Length);
                                    if (UserIdlen > 8)
                                    {
                                        UserId = (UserId.Substring(0, 8));
                                    }
    
                                    var fileName = todaydatefriendly + "-" + UserId + "-" + myuploadiwthoutextensionfriendly + myuploadextension;
                                    var path = Path.Combine(Server.MapPath("~/Content/UserCreations"), fileName);
    
                                    var fileName200 = todaydatefriendly + "-" + UserId + "-" + myuploadiwthoutextensionfriendly + "-200" + myuploadextension;
                                    var path200 = Path.Combine(Server.MapPath("~/Content/UserCreations"), fileName200);
    
                                    var fileName750 = todaydatefriendly + "-" + UserId + "-" + myuploadiwthoutextensionfriendly + "-650" + myuploadextension;
                                    var path750 = Path.Combine(Server.MapPath("~/Content/UserCreations"), fileName750);
    
                                    file.SaveAs(path);
    
                                    WebImage img = new WebImage(path);                                
    
                                    int CurrentWidth = img.Width;
                                    int CurrentHeight = img.Height;
    
                                    // With this i resize image highter than longer
                                    var CurrentWidthHeightChoosen = "Width";
                                    int CurrentWidthHeight = img.Width;
                                    if (CurrentHeight > CurrentWidth)
                                    {
                                        CurrentWidthHeight = img.Height;
                                        CurrentWidthHeightChoosen = "Height";
                                    }
    
                                    //***********
                                    //RESIZE 200
                                    //***********
                                    if (CurrentWidthHeight > 200)
                                    {
                      
                                        int maxWidth = 200;
                                        int CreationWidthPercentual = 20000 / CurrentWidth;
                                        int maxHeight = (CurrentHeight * CreationWidthPercentual) / 100;
    
                                        // With this i resize image highter than longer
                                        if (CurrentWidthHeightChoosen == "Height")
                                        {
                                            maxHeight = 200;
                                            int CreationHeightPercentual = 20000 / CurrentHeight;
                                            maxWidth = (CurrentWidth * CreationHeightPercentual) / 100;
                                        }
    
                                        // Resize image
                                        var image = System.Drawing.Image.FromFile(path);
                                        var ratioX = (double)maxWidth / image.Width;
                                        var ratioY = (double)maxHeight / image.Height;
                                        var ratio = Math.Min(ratioX, ratioY);
                                        var newWidth = (int)(image.Width * ratio);
                                        var newHeight = (int)(image.Height * ratio);
                                        var newImage = new Bitmap(newWidth, newHeight);
                                        Graphics thumbGraph = Graphics.FromImage(newImage);
    
                                        thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
                                        thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
                                        thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
                                        thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);
                                        image.Dispose();
    
                                        string fileRelativePath = "~/Content/UserCreations/" + Path.GetFileName(path200);
                                        newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat);
    
                                        //Save database
                                        creationbind.Creation.CreationPhotoBis = fileName200;
                                    }
                                    else
                                    {
                                        //Save database
                                        creationbind.Creation.CreationPhotoBis = fileName;
                                    }
    
    
                                    //***********
                                    //RESIZE 650 (It was 750)
                                    //***********
                                    if (CurrentWidthHeight > 650)
                                    {
                                        int maxWidth = 650;
                                        int CreationWidthPercentual = 65000 / CurrentWidth;
                                        int maxHeight = (CurrentHeight * CreationWidthPercentual) / 100;
    
                                        // With this i resize image highter than longer
                                        if (CurrentWidthHeightChoosen == "Height")
                                        {
                                            maxHeight = 650;
                                            int CreationHeightPercentual = 65000 / CurrentHeight;
                                            maxWidth = (CurrentWidth * CreationHeightPercentual) / 100;
                                        }
    
                                        // Resize image
                                        var image = System.Drawing.Image.FromFile(path);
                                        var ratioX = (double)maxWidth / image.Width;
                                        var ratioY = (double)maxHeight / image.Height;
                                        var ratio = Math.Min(ratioX, ratioY);
                                        var newWidth = (int)(image.Width * ratio);
                                        var newHeight = (int)(image.Height * ratio);
                                        var newImage = new Bitmap(newWidth, newHeight);
                                        Graphics thumbGraph = Graphics.FromImage(newImage);
    
                                        thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
                                        thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
                                        thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
                                        thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);
                                        image.Dispose();
    
                                        string fileRelativePath = "~/Content/UserCreations/" + Path.GetFileName(path750);
                                        newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat);
    
                                        //Save database
                                        creationbind.Creation.CreationPhoto750 = fileName750;
                                    }
                                    else
                                    {
                                        //Save database
                                        creationbind.Creation.CreationPhoto750 = fileName;
                                    }
    
                                    creationbind.Creation.CreationPhotoReal = fileName; //after add
    
                                    db.Creations.Add(creationbind.Creation);
                                    db.SaveChanges();
                                }
    
    
                            }
                        }
                    }
    
                    //UserId
                    return RedirectToAction("CreationList", "Creation", new { UserId = User.Identity.GetUserId() });
                }
    
                return View(creationbind);
            }
    

    For example this piscture, which is fine as original :

    http://www.collectionhair.it/Content/UserCreations/2016-10-18-040416-6470fdbe-img-6238.JPG

    when resized to 650 became like this, is turned of 90° (the same happen when i resize of 200)... as i show :

    http://www.collectionhair.it/Content/UserCreations/2016-10-18-040416-6470fdbe-img-6238-650.JPG

    What is wrong in my code?

    Tuesday, October 18, 2016 2:14 PM

All replies

  • User-654786183 posted

    Looks like the similar question is addressed in this SO thread

    http://stackoverflow.com/questions/33310562/some-images-are-being-rotated-when-resized

    Tuesday, October 18, 2016 2:33 PM
  • User1991482975 posted

    Hi, i am trying to update my code like this :

    //***********
                                    //RESIZE 200
                                    //***********
                                    if (CurrentWidthHeight > 200)
                                    {
                                        //es: l'80% di 1000... si fa 1000 x 80 = 80000 / 100 = 800 
                                        //invece per sapere 800 rispetto a 1500 che percentuale è... si fa la proporzione 800:1500= x : 100 quindi 800 x 100= 80000 / 1500 = 53,33 capito?
                                        //685
                                        //310
                                        // 200 rispetto 685 = 200 * 100 / 685 = 29,1970802919708
                                        // Percentuale 310 = 310 * 29 =  8990 / 100 = 89,9
                                        int maxWidth = 200;
                                        int CreationWidthPercentual = 20000 / CurrentWidth;
                                        int maxHeight = (CurrentHeight * CreationWidthPercentual) / 100;
    
                                        // With this i resize image highter than longer
                                        if (CurrentWidthHeightChoosen == "Height")
                                        {
                                            maxHeight = 200;
                                            int CreationHeightPercentual = 20000 / CurrentHeight;
                                            maxWidth = (CurrentWidth * CreationHeightPercentual) / 100;
                                        }
    
                                        // Resize image
                                        //int maxWidth = 250;
                                        //int maxHeight = 100;
                                        var image = System.Drawing.Image.FromFile(path);
                                        var ratioX = (double)maxWidth / image.Width;
                                        var ratioY = (double)maxHeight / image.Height;
                                        var ratio = Math.Min(ratioX, ratioY);
                                        var newWidth = (int)(image.Width * ratio);
                                        var newHeight = (int)(image.Height * ratio);
                                        var newImage = new Bitmap(newWidth, newHeight);
                                        Graphics thumbGraph = Graphics.FromImage(newImage);
    
                                        thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
                                        thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
                                        thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
                                        thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);
                                        image.Dispose();
    
    //ADDED CODE int OrientationKey = 0x0112; private const int NotSpecified = 0; private const int NormalOrientation = 1; private const int MirrorHorizontal = 2; private const int UpsideDown = 3; private const int MirrorVertical = 4; private const int MirrorHorizontalAndRotateRight = 5; private const int RotateLeft = 6; private const int MirorHorizontalAndRotateLeft = 7; private const int RotateRight = 8; // Fix orientation if needed. if (image.PropertyIdList.Contains(OrientationKey)) { var orientation = (int)image.GetPropertyItem(OrientationKey).Value[0]; switch (orientation) { case NotSpecified: // Assume it is good. case NormalOrientation: // No rotation required. break; case MirrorHorizontal: newBitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case UpsideDown: newBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case MirrorVertical: newBitmap.RotateFlip(RotateFlipType.Rotate180FlipX); break; case MirrorHorizontalAndRotateRight: newBitmap.RotateFlip(RotateFlipType.Rotate90FlipX); break; case RotateLeft: newBitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case MirorHorizontalAndRotateLeft: newBitmap.RotateFlip(RotateFlipType.Rotate270FlipX); break; case RotateRight: newBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: throw new NotImplementedException("An orientation of " + orientation + " isn't implemented."); } } string fileRelativePath = "~/Content/UserCreations/" + Path.GetFileName(path200); newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat); //Save database creationbind.Creation.CreationPhotoBis = fileName200; } else { //Save database creationbind.Creation.CreationPhotoBis = fileName; }

    But apaprently i cannot put constant inside my code...

    how to handle this?

    Can i have some help to fit that code into mine?

    Thanks a lot!!!

                                        
    Saturday, October 22, 2016 10:17 AM