88 lines
2.6 KiB
C#
88 lines
2.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Chat.Framework.Utils
|
|
{
|
|
/// <summary>
|
|
/// 常用的操作类
|
|
/// </summary>
|
|
public static class Common
|
|
{
|
|
#region Base64 与 Image互转
|
|
/// <summary>
|
|
/// 图片转Base64
|
|
/// </summary>
|
|
/// <param name="file">图片</param>
|
|
/// <returns></returns>
|
|
public static string ConvertImageToBase64(Image file)
|
|
{
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
file.Save(memoryStream, file.RawFormat);
|
|
byte[] imageBytes = memoryStream.ToArray();
|
|
return Convert.ToBase64String(imageBytes);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base64转图片
|
|
/// </summary>
|
|
/// <param name="base64String">base64</param>
|
|
/// <returns></returns>
|
|
public static Image ConvertBase64ToImage(string base64String)
|
|
{
|
|
byte[] imageBytes = Convert.FromBase64String(base64String);
|
|
using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
|
|
{
|
|
ms.Write(imageBytes, 0, imageBytes.Length);
|
|
return Image.FromStream(ms, true);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
private static Random random = new Random();
|
|
|
|
/// <summary>
|
|
/// 变量分隔,随机返回
|
|
/// </summary>
|
|
/// <param name="mess">消息</param>
|
|
/// <param name="separator">分隔标识</param>
|
|
/// <returns></returns>
|
|
public static string RandomMess(this string mess, string separator = "[随机]")
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrWhiteSpace(mess)) return string.Empty;
|
|
var messList = mess.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries).Where(f => !string.IsNullOrWhiteSpace(f)).ToList();
|
|
if (messList != null)
|
|
{
|
|
if (messList.Count == 0 || messList.Count == 1)
|
|
return mess;
|
|
return messList[random.Next(0, messList.Count)];
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
return mess;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 朋友圈类型
|
|
/// </summary>
|
|
public enum CircleType
|
|
{
|
|
文字朋友圈 = 0,
|
|
视频朋友圈 = 1,
|
|
链接朋友圈 = 2,
|
|
图文朋友圈 = 3,
|
|
}
|
|
|
|
}
|
|
}
|