C#判断字符串是否base64,及base64转换为Bitmap
2017-11-23 16:27
549 查看
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> /// 是否base64字符串 /// </summary> /// <param name="base64Str">要判断的字符串</param> /// <returns></returns> public static bool IsBase64(string base64Str) { byte[] bytes = null; return IsBase64(base64Str, out bytes); } /// <summary> /// 是否base64字符串 /// </summary> /// <param name="base64Str">要判断的字符串</param> /// <param name="bytes">字符串转换成的字节数组</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> /// 把base64字符串转换成Bitmap /// </summary> /// <param name="base64Str">要转换的base64字符串</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);//为了避免有时候流指针定位错误,显式定义一下指针位置 bitmap = new Bitmap(stream); } } } catch (Exception) { bitmap = null; } return bitmap; } /// <summary> /// 根据base64字符串获取文件后缀(图片格式) /// </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); } return suffix; } } }
相关文章推荐
- 判断C#中的字符串是否是数字,如果是转换成int类型
- 判断C#中的字符串是否是数字,如果是转换成int类型
- c#下怎么判断一个字符串是否可以转换为double类型
- C#先把数值类型转换为带千分位的字符串,再判断是否带小数,如果带小数,保存两位小数,不带,不保留。
- 判断C#中的字符串是否是数字,如果是转换成int类型
- C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决
- 校验出生日期,校验身份证格式,校验会员账户:9开头数字,校验手机号码是否合法,判断字符串中的url是否都是图片格式字符串,通过图片url读取图片并将图片转换成base64编码
- C#判断字符串是否可以转换为长整型值
- C#判断字符串是否为数字字符串
- c# 字符串 中查找字符,判断是否包含字符串
- C#中判断字符串A中是否包含字符串B
- 判断字符串是否可以转换成日期类型
- C# 判断字符串是否为数字
- C# 16进制转换为Base64字符串
- C#判断字符串是否存在字母及字符串中字符的替换实例
- c# 判断字符串是否为IP地址
- 如何判断字符串是否为空串?[C#]
- C#实现判断字符串中是否包含中文的方法
- C# 判断一个字符串是否是时间格式
- 如何判断字符串是否为空串?[C#]