using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing;
 
namespace XTools
{
    public class Base64Utility
    {
        private static char[] base64CodeArray = new char[]
        {
            'A'.'B'.'C'.'D'.'E'.'F'.'G'.'H'.'I'.'J'.'K'.'L'.'M'.'N'.'O'.'P'.'Q'.'R'.'S'.'T'.'U'.'V'.'W'.'X'.'Y'.'Z'.'a'.'b'.'c'.'d'.'e'.'f'.'g'.'h'.'i'.'j'.'k'.'l'.'m'.'n'.'o'.'p'.'q'.'r'.'s'.'t'.'u'.'v'.'w'.'x'.'y'.'z'.'0'.'1'.'2'.'3'.'4'.'5'.'6'.'7'.'8'.'9'.'+'.'/'.'='
        };
 
        /// <summary>
        ///Whether base64 The value is a character string
        /// </summary>
        /// <param name="base64Str">The string to judge</param>
        /// <returns></returns>
        public static bool IsBase64(string base64Str)
        {
            byte[] bytes = null;
            return IsBase64(base64Str, out bytes);
        }
 
        /// <summary>
        ///Whether base64 The value is a character string
        /// </summary>
        /// <param name="base64Str">The string to judge</param>
        /// <param name="bytes">An array of bytes converted from a string</param>
        /// <returns></returns>
        public static bool IsBase64(string base64Str, out byte[] bytes)
        {
            //string strRegex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
            bytes = null;
            if (string.IsNullOrEmpty(base64Str))
                return false;
            else
            {
                if (base64Str.Contains(","))
                    base64Str = base64Str.Split(', ') [1];
                if (base64Str.Length % 4! =0)
                    return false;
                if(base64Str.Any(c => ! base64CodeArray.Contains(c)))return false;
            }
            try
            {
                bytes = Convert.FromBase64String(base64Str);
                return true;
            }
            catch (FormatException)
            {
                return false; }}/// <summary>
        ///Convert base64 strings into bitmaps
        /// </summary>
        /// <param name="base64Str">The Base64 string to convert</param>
        /// <returns></returns>
        public static Bitmap Base64ToBitmap(string base64Str)
        {
            Bitmap bitmap = null;
            byte[] bytes = null;
            try
            {
                if (IsBase64(base64Str, out bytes))
                {
                    using (MemoryStream stream = new MemoryStream(bytes))
                    {
                        stream.Seek(0, SeekOrigin.Begin);// In order to avoid the sometimes incorrect location of the stream pointer, explicitly define the pointer position
                        bitmap = new Bitmap(stream);
                    }
                }
            }
            catch (Exception)
            {
                bitmap = null;
            }
            return bitmap;
        }
 
        /// <summary>
        ///Get file suffixes from base64 strings (image format)
        /// </summary>
        /// <param name="base64Str">base64</param>
        /// <returns></returns>
        public static string GetSuffixFromBase64Str(string base64Str)
        {
            string suffix = string.Empty;
            string prefix = "data:image/";
            if (base64Str.StartsWith(prefix) && base64Str.Contains(";") && base64Str.Contains(","))
            {
                base64Str = base64Str.Split('; ') [0];
                suffix = base64Str.Substring(prefix.Length);
            }
            returnsuffix; }}}Copy the code