You can use the following code to check if a Base64 string is an image in C#:

using System;
using System.Drawing;
using System.IO;

public static bool IsBase64StringAnImage(string base64String)
{
    try
    {
        byte[] bytes = Convert.FromBase64String(base64String);
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            Image img = Image.FromStream(ms);
            return true;
        }
    }
    catch
    {
        return false;
    }
}

This code tries to convert the Base64 string to a byte array, and then creates a MemoryStream and an Image object from the byte array. If the conversion and creation are successful, the method returns true, indicating that the Base64 string is an image. If there is an exception thrown during the process, the method returns false, indicating that the Base64 string is not an image

How to check if Base64 string is an image in c#

原文地址: https://www.cveoy.top/t/topic/fQ72 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录