我使用 ImageProccessor 对上传的图像应用一些更改,我想检查width
图像是否已上传少于150px
将其更改为200px
. 为此,我制作了这样的Image
类实例:
Image image = Image.FromStream(inStream);
当我添加这个我得到这个错误:
输入流不是受支持的格式
这是我的代码:
public ActionResult SaveFileToTemporaryFodler()
{
HttpPostedFileBase file = Request.Files[0];
string directoryPath = ImageService.GetImageTempDirectoryRelativePath();
byte[] photoBytes = file.InputStream.StreamToByteArray();
int quality = 70;
using (var inStream = new MemoryStream(photoBytes))
{
Image image = Image.FromStream(inStream);
using (var outStream = new MemoryStream())
{
Size size = new Size(500, 500);
using (var imageFactory = new ImageFactory(preserveExifData: true))
{
if (image.Width < 150)
{
size = new Size(200, 200);
}
// I get error here :
imageFactory.Load(inStream)
.RoundedCorners(new RoundedCornerLayer(190, true, true, true, true))
.Watermark(new TextLayer()
{
DropShadow = true,
Text = "Watermark",
Style = FontStyle.Bold,
})
.Resize(size)
.Quality(quality)
.Save(Server.MapPath(directoryPath + fileName));
}
}
}
return Json(new { message = Request.Files.Count, picName = fileName, fileExtention = fileName });
}
我该如何解决这个问题,或者有什么方法可以检查图像尺寸?
更新:
我用 Ajax 上传图片,这里是StreamToByteArray
函数:
public static byte[] StreamToByteArray(this Stream input)
{
input.Position = 0;
using (var ms = new MemoryStream())
{
int length = System.Convert.ToInt32(input.Length);
input.CopyTo(ms, length);
return ms.ToArray();
}
}