验证 base64 编码图像 .NET 核心
Validating a base64 encoded image .NET core
我正在构建一个 API,除其他外,它允许用户更改他们的照片。这张照片作为 base64 字符串发送,我希望能够验证它是实际的 .jpeg 或 .png 格式。
由于 .NET Core 中缺少 System.Drawing,我不确定该怎么做。在我可以简单地使用
之类的东西之前
public Image Base64ToImage(string base64String)
{
// Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Image image = Image.FromStream(ms, true);
return image;
}
}
然后从那里去检查我需要的图像。
如有任何帮助,我们将不胜感激
我正在构建一个 API,除其他外,它允许用户更改他们的照片。这张照片作为 base64 字符串发送,我希望能够验证它是实际的 .jpeg 或 .png 格式。 由于 .NET Core 中缺少 System.Drawing,我不确定该怎么做。在我可以简单地使用
之类的东西之前public Image Base64ToImage(string base64String)
{
// Convert base 64 string to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
// Convert byte[] to Image
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
Image image = Image.FromStream(ms, true);
return image;
}
}
然后从那里去检查我需要的图像。
如有任何帮助,我们将不胜感激