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?