如何将base64字符串转换为图像二进制文件并保存到服务器
How to convert base64 string to image binary file and save onto server
例如,我已经转换了一个 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);
}
例如,我已经转换了一个 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);
}