[英]Rotate byte array Image and encode to Base64 string
我很难弄清楚这一点:
我正在使用前端
The back-end is only slated to store images as Base64 strings, as it later embeds the image as a Base64 string to an email for the user whose email application will only show Base64-embedded images. 问题是图像在 email 中逆时针嵌入 90 度,所以我试图在编码 base64 字符串之前将图像旋转 90 度,该字符串插入 Z139C4883EB01E5D9DD23C9FF0E4 存储过程中。 我觉得这段代码应该可以解决问题:
HttpPostedFile img = imageUpload.PostedFile;
Stream stream = img.InputStream;
BinaryReader binaryReader = new BinaryReader(stream);
byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
// Rotate Image Code (help):
using (var memoryStream = new MemoryStream(bytes))
{
var rotateImage = System.Drawing.Image.FromStream(memoryStream); rotateImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
rotateImage.Save(memoryStream, rotateImage.RawFormat);
bytes = memoryStream.ToArray();
}
// End Rotate Image Code
insert.Parameters.AddWithValue("@Image",Convert.ToBase64String(bytes));
但是当我尝试运行它时收到一个错误:System.ArgumentNullException: Value cannot be null。 参数名称:“bytes = memoryStream.ToArray();”行的编码器
当我尝试将其保存为 jpeg 时,图像在顶部被切掉,但没有其他格式有效。
我认为该行存在问题:
rotateImage.Save(memoryStream, rotateImage.RawFormat);
也许你应该指定格式。 尝试:
rotateImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
甚至更好的是,您可以仅在出现异常时捕获并强制转换:
try
{
rotateImage.Save(memoryStream, rotateImage.RawFormat);
}
catch (System.ArgumentNullException)
{
rotateImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
我认为这个问题与这个答案有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.