I am trying to upload an image to a server through a ASP.net core. My frontend sends a base64 string of an image to my server, and I want my server to resize it to 480*680 px and save it. I convert the image to a byte array:
byte[] bytes = Convert.FromBase64String(newItem.Image);
And use this function to Resize and Save:
private string ResizeAndSave(byte[] bytes)
{
Guid guid = Guid.NewGuid();
string newFileName = $"{guid}.jpg";
using (var ms = new MemoryStream(bytes))
{
var image = new Bitmap(ms);
var destRect = new Rectangle(0, 0, 640, 480);
var destImage = new Bitmap(640, 480);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
};
var saveDir = _hostingEnvironment.WebRootPath + "\\images";
if (!Directory.Exists(saveDir))
{
Directory.CreateDirectory(saveDir);
}
destImage.Save($"{saveDir}/{newFileName}", ImageFormat.Jpeg);
};
return newFileName;
}
If I save the ByteArray directly, everything works, and the image comes out as I want orientation wise. But since I plan on uploading directly from a phone I want to scale the images down some. So I convert it to a Bitmap and work some magic.
However when the bitmap is loaded the image is flipped 90 degrees on the side.
I thought whatever and switched the width/height of the BitMap and Rectangle functions. Which works. Except the image was on its side, so I added a:
destImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
Before the image is saved again. It works. For IPhone - But if I upload from an Android the image is completely upside down.
I take that I need to read some EXIF tags, but there doesn't seem to be any orientation exif tags on the image I am testing with (and the bitmap PropertyIdList is empty), and even without EXIF tags it shouldn't rotate the image just by making a new Bitmap - Right?
Can anyone suggest a good way to get the correct rotation as well as downscaling of the image to a manageable size?