作为示例,我已使用调整大小的图像转换了 Canvas 元素,并将其发布到隐藏的输入字段中,该字段现在编码为
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...
然后将该值发布到同一页面,我需要将该字符串转换为图像并保存到服务器上。
代码隐藏文件(upload.aspx)
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile filePosted = Request.Files["newinput"];
string base64String = filePosted.ToString();
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
//I DONT KNOW HOW TO WRITE ABOVE INTO THE SaveAs CONDITION BELOW
if (filePosted != null && filePosted.ContentLength > 0)
{
string fileNameApplication = Path.GetFileName(filePosted.FileName);
string fileExtensionApplication = Path.GetExtension(fileNameApplication);
// generating a random guid for a new file at server for the uploaded file
string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
// getting a valid server path to save
string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile);
if (fileNameApplication != String.Empty)
{
filePosted.SaveAs(filePath);
}
}
我很确定在保存到服务器上之前我需要将图像数据转换为二进制文件,但我不太清楚需要如何修改上面的代码。有任何想法吗?保存到服务器的代码不起作用。
一旦我将其转换为图像并更改了上面的名称 - 我将通过 LINQ 将其存储回数据库 - 并附加一个 URL。
任何帮助将不胜感激。
最佳答案
希望以下功能对您有所帮助。
public string ImageToBase64(Image image,
System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
编辑 1 -
从评论中看来,您正在获取 Base64 字符串,并且需要将其保存为服务器上的图像,然后在需要时需要使用物理服务器路径显示该图像。
好的。 Base64ToImage 将为您提供 base64 字符串的图像。您可以使用
将其保存在服务器上image.Save("PATH", System.Drawing.Imaging.ImageFormat.Jpeg);
您提供或创建的这个“PATH”可以作为 URL 存储在数据库中,您可以在显示时使用它。
注意:确保您对保存图像的文件夹具有写入权限。
EDIT-2 您的函数应如下所示。请按要求填写验证码、错误处理。
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile filePosted = Request.Files["newinput"];
string base64String = filePosted.ToString();
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile);
image.Save(filepath,ImageFormat.Jpeg);
}
关于c# - 如何将base64字符串转换为图像二进制文件并保存到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30565687/