| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace IPADDemo.Util
- {
- public static class BaseClass
- {
- /// <summary>
- /// 判断是否为空,为空返回true
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static bool IsNull(this object data)
- {
- //如果为null
- if (data == null)
- {
- return true;
- }
- //如果为""
- if (data.GetType() == typeof(String))
- {
- if (string.IsNullOrEmpty(data.ToString().Trim()))
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 判断是否为空,为空返回true
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static bool IsNull(string data)
- {
- //如果为null
- if (data == null)
- {
- return true;
- }
- //如果为""
- if (data.GetType() == typeof(String))
- {
- if (string.IsNullOrEmpty(data.ToString().Trim()))
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 将obj类型转换为string
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- public static string ConvertToString(this object s)
- {
- if (s == null)
- {
- return "";
- }
- else
- {
- return Convert.ToString(s);
- }
- }
- /// <summary>
- /// 将字符串转int32
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- public static Int32 ConvertToInt32(this string s)
- {
- int i = 0;
- if (s == null)
- {
- return 0;
- }
- else
- {
- int.TryParse(s, out i);
- }
- return i;
- }
- /// <summary>
- /// 将字符串转double
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- public static double ConvertToDouble(this object s)
- {
- double i = 0;
- if (s == null)
- {
- return 0;
- }
- else
- {
- double.TryParse(s.ToString(), out i);
- }
- return i;
- }
- /// <summary>
- /// 转换为datetime类型
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- public static DateTime ConvertToDateTime(this string s)
- {
- DateTime dt = new DateTime();
- if (s == null || s == "")
- {
- return DateTime.Now;
- }
- DateTime.TryParse(s, out dt);
- return dt;
- }
- /// <summary>
- /// 转换为datetime类型的格式字符串
- /// </summary>
- /// <param name="s">要转换的对象</param>
- /// <param name="y">格式化字符串</param>
- /// <returns></returns>
- public static string ConvertToDateTime(this string s, string y)
- {
- DateTime dt = new DateTime();
- DateTime.TryParse(s, out dt);
- return dt.ToString(y);
- }
- /// <summary>
- /// 将字符串转换成decimal
- /// </summary>
- /// <param name="s"></param>
- /// <returns></returns>
- public static decimal ConvertToDecimal(this object s)
- {
- decimal d = 0;
- if (s == null || s == "")
- {
- return 0;
- }
- Decimal.TryParse(s.ToString(), out d);
- return d;
- }
- public static string ReplaceHtml(this string s)
- {
- return s.Replace("<", "<").Replace(">", ">").Replace("&", "&").Replace(""", "\"");
- }
- public static string ImageToBase64String(this Image image)
- {
- return Convert.ToBase64String(image.ImageToBytes());
- }
- public static Image Base64StringToImage(this string base64)
- {
- byte[] buffer = Convert.FromBase64String(base64);
- return buffer.BytesToImage();
- }
- public static byte[] ImageToBytes(this Image image)
- {
- ImageFormat format = image.RawFormat;
- using (MemoryStream ms = new MemoryStream())
- {
- image.Save(ms, ImageFormat.Png);
- byte[] buffer = new byte[ms.Length];
- //Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
- ms.Seek(0, SeekOrigin.Begin);
- ms.Read(buffer, 0, buffer.Length);
- return buffer;
- }
- }
- public static Image BytesToImage(this byte[] buffer)
- {
- MemoryStream ms = new MemoryStream(buffer);
- Image image = Image.FromStream(ms);
- return image;
- }
- /// <summary>
- /// utf8转ansi
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string Utf8ToAnsi(this string str) {
- return Cover.MByteToWChar(str, 936);
- }
- }
- }
|