I am uploading images to my AWS S3 storage using asp.net website.
I have a process before uploading the image. I downgrade the image quality before uploading it to the cloud.
When I give it an image that its width is bigger than its height, the process works without any problem, but when I try it on an image that its height is bigger than its width, it rotates the image -90 degrees.
public void image_Upload(){
Image fixed_image = ResizeImage(Path.GetFileName(FileUpload1.FileName), myimg, new Size(800, 500));
}
public Image ResizeImage(string fileName, Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercentW = (size.Width / (float)sourceWidth);
float nPercentH = (size.Height / (float)sourceHeight);
float nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;
var destWidth = (int)(sourceWidth * nPercent);
var destHeight = (int)(sourceHeight * nPercent);
var src = imgToResize;
using (var dst = new Bitmap(destWidth, destHeight, imgToResize.PixelFormat))
{
dst.SetResolution(imgToResize.HorizontalResolution, imgToResize.VerticalResolution);
using (var g = Graphics.FromImage(dst))
{
var mime = Path.GetExtension(fileName).ToLower();
ImageFormat format;
if (mime == ".bmp" || mime == ".png")
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(src, 0, 0, dst.Width, dst.Height);
format = ImageFormat.Png;
}
else
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
format = ImageFormat.Jpeg;
}
g.DrawImage(src,0,0, dst.Width, dst.Height);
var m = new MemoryStream();
dst.Save(m, format);
var img = Image.FromStream(m);
return img;
}
}
}
What I have tried:
I have tried changing some parameters such as new Size(), nPercent, g.DrawImage() it still doesn't solve the problem.
In fact changing nPercent parameter will results upside down images when the width is bigger