[英]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.