[英]How can I convert javascript jpeg string image to C# image bytes?
I am reading the image content in React Front-end App using javascript FileReader() as我正在使用 javascript FileReader() 在 React Front-end App 中读取图像内容作为
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => resolve(e.target.result);
reader.onerror = reject;
reader.readAsDataURL(file); // file is a jpeg file
});
Now, I want to pass this image string C# SDK, which accepts image data bytes of the form现在,我想传递这个图像字符串 C# SDK,它接受表单的图像数据字节
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
fileStream.Close();
How do I convert the image string?如何转换图像字符串? I have tried by just removing the initial image format padding as
我已经尝试通过删除初始图像格式填充为
Face= Face.Replace("data:image/jpeg;base64,", string.Empty);
And then passed Face to bytes buffer as然后将 Face 传递给字节缓冲区作为
byte[] buffer = Encoding.UTF8.GetBytes(Face); //also tried Convert.FromBase64String(Face);
First you need to decode the Base64 string by using Convert.FromBase64String
.首先,您需要使用
Convert.FromBase64String
解码 Base64 字符串。 Then you can load your image from the resulting byte array using some sort of image library.然后,您可以使用某种图像库从生成的字节数组中加载图像。
Example (haven't tested it):示例(尚未测试):
Face = Face.Replace("data:image/jpeg;base64,", string.Empty);
byte[] FaceBytes = Convert.FromBase64String(Face);
using (var ms = new MemoryStream(FaceBytes ))
{
var image = Image.FromStream(ms);
// Do something with image
}
Or you can just simply use the FaceBytes
array for whatever you need.或者您可以简单地将
FaceBytes
数组用于您需要的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.