gpt4 book ai didi

c# - 从手机上传的图片是横向显示的

转载 作者:行者123 更新时间:2023-11-30 12:21:52 25 4
gpt4 key购买 nike

我可以选择上传图片

<input class="images" type="file" id="item" name="Images" />

然后它会像这样保存到我的项目中

Guid g = Guid.NewGuid();
Images.SaveAs(Server.MapPath("~/Uploads/" + g + ".jpg"));
fileNames = g.ToString() + ".jpg";

出于某种原因,当有人从手机上传图片到网站时,它会横向显示吗?

最佳答案

您可以查看文件的元数据以了解其旋转方式。

具体来说,将图像作为 .NET 图像类型拉入,然后调用 img.GetPropertyItem(&H112).Value(0)。

这将返回一个整数,表示图像的“旋转值”。

1 = Landscape
3 = Upside-down
6 = Rotated 90 degrees left
8 = Rotated 90 degrees right

一旦知道,就可以使用 img.RotateFlip 方法旋转图像。

下面是我为解决非常相似的问题而编写的一个类。

相关代码在RotateImage方法中。

注意:这是在 VB.NET 中进行的,我通过 telerik 代码转换器运行它,所以对于任何奇怪的语法我深表歉意

//get the image from the file they gave us, resize it, and rotate it if needed
OnlineImage onlineImageHelper = new OnlineImage(Context.Request.Files(0).InputStream);
byte[] pictureLarger = onlineImageHelper.StraightenedThumbnail(new Size(180, 180));
byte[] pictureSmaller = onlineImageHelper.StraightenedThumbnail(new Size(80, 80));

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class OnlineImage
{
public OnlineImage()
{
throw new NotImplementedException();
}

public OnlineImage(Stream imageStream)
{
_ImageFromUser = Image.FromStream(imageStream);
RotateImage();
}

private Image _ImageFromUser;
private Image _RotatedImage;
private Image _ResizedAndRotatedImage;

private void RotateImage()
{
if (_RotatedImage == null && _ImageFromUser != null && _ImageFromUser.PropertyIdList != null && _ImageFromUser.PropertyIdList.Contains(0x112)) {
int rotationValue = _ImageFromUser.GetPropertyItem(0x112).Value(0);
switch (rotationValue) {
case 1:
// landscape, do nothing
break;
case 8:
// rotated 90 right
// de-rotate:
_ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);
break;
case 3:
// bottoms up
_ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);
break;
case 6:
// rotated 90 left
_ImageFromUser.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);
break;
}
_RotatedImage = _ImageFromUser;
}
}

private void ResizeImage(Size size, bool preserveAspectRatio = true)
{
int newWidth = 0;
int newHeight = 0;
if (preserveAspectRatio) {
int originalWidth = _ImageFromUser.Width;
int originalHeight = _ImageFromUser.Height;
float percentWidth = Convert.ToSingle(size.Width) / Convert.ToSingle(originalWidth);
float percentHeight = Convert.ToSingle(size.Height) / Convert.ToSingle(originalHeight);
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = Convert.ToInt32(originalWidth * percent);
newHeight = Convert.ToInt32(originalHeight * percent);
} else {
newWidth = size.Width;
newHeight = size.Height;
}

_ResizedAndRotatedImage = new Bitmap(newWidth, newHeight);

using (Graphics graphicsHandle = Graphics.FromImage(_ResizedAndRotatedImage)) {
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(_ImageFromUser, 0, 0, newWidth, newHeight);
}
}

public byte[] StraightenedThumbnail(Size resizedDimensions)
{
byte[] result = null;
MemoryStream msPicture = new MemoryStream();
ResizeImage(resizedDimensions);
if (_ResizedAndRotatedImage != null) {
_ResizedAndRotatedImage.Save(msPicture, ImageFormat.Png);
result = msPicture.ToArray();
return result;
}

return null;
}
}

关于c# - 从手机上传的图片是横向显示的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com