| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:Utility.cs
- * 2.功能描述:系统各种常用函数和方法类
- * 3.函数/方法列表:
- * A.IsNull:判断空值
- * B.EnableFormCloseBox:设定窗体的关闭按钮是否有效
- * C.IsValidDate:判断是否是合法的日期
- * D.ChineseToPinyinCap:根据输入汉字,转化成汉字首字母串
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 欧阳涛 2012/06/07 1.00 新建
- * 王文君 2012/07/10 1.00 修改 添加IsUnique方法
- * 欧阳涛 2012/07/19 1.00 增加了GetBytesByFilePath()方法,
- * 根据文件路径返回文件字节流
- *******************************************************************************/
- using System;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Windows.Forms;
- using Dongke.IBOSS.PRD.Basics.BaseResources;
- namespace Dongke.IBOSS.PRD.Basics.Library
- {
- /// <summary>
- /// 系统各种常用函数和方法类
- /// </summary>
- public static class Utility
- {
- #region IsDirty
- public static bool IsDirty(DataSet dataSet)
- {
- if (dataSet == null)
- {
- return false;
- }
- foreach (DataTable table in dataSet.Tables)
- {
- foreach (DataRow row in table.Rows)
- {
- row.EndEdit();
- }
- }
- DataSet changes = dataSet.GetChanges();
- if (changes == null)
- {
- return false;
- }
- foreach (DataTable table in changes.Tables)
- {
- if (IsDirty(table))
- {
- return true;
- }
- }
- return false;
- }
- public static bool IsDirty(DataTable table)
- {
- if (table == null)
- {
- return false;
- }
- if (table.GetChanges(DataRowState.Added) != null)
- {
- return true;
- }
- if (table.GetChanges(DataRowState.Deleted) != null)
- {
- return true;
- }
- DataTable changes = table.GetChanges(DataRowState.Modified);
- if (changes != null)
- {
- foreach (DataRow row in changes.Rows)
- {
- if (IsDirty(row))
- {
- return true;
- }
- }
- }
- return false;
- }
- public static bool IsDirty(DataRow row)
- {
- if (row == null)
- {
- return false;
- }
- if (row.RowState == DataRowState.Added
- || row.RowState == DataRowState.Deleted)
- {
- return true;
- }
- if (row.RowState == DataRowState.Modified)
- {
- foreach (DataColumn column in row.Table.Columns)
- {
- if (column.ReadOnly)
- {
- continue;
- }
- if (!Utility.Compare(row[column, DataRowVersion.Original],
- row[column, DataRowVersion.Current]))
- {
- return true;
- }
- }
- }
- return false;
- }
- #endregion
- #region 申明API函数
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="section">节点名称[如[TypeName]]</param>
- /// <param name="key">键</param>
- /// <param name="val">值</param>
- /// <param name="filepath">文件路径</param>
- /// <returns></returns>
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
- /// <summary>
- /// 读取INI文件
- /// </summary>
- /// <param name="section">节点名称</param>
- /// <param name="key">键</param>
- /// <param name="def">值</param>
- /// <param name="retval">stringbulider对象</param>
- /// <param name="size">字节大小</param>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
- #endregion
- #region 成员变量
- // 获取屏幕DPI用
- private static readonly Control _control;
- // 图形
- private static readonly Graphics _graphics;
- /// <summary>
- /// 毫米和像素的转换系数
- /// </summary>
- private const float MILLIMETER_PER_INCH = 25.4f;
- /// <summary>
- /// 磅和像素的转换系数
- /// </summary>
- private const float POINT_PER_INCH = 72f;
- /// <summary>
- /// 屏幕的DPI
- /// </summary>
- private static float SCREEN_DPI;
- #endregion
- #region 构造函数
- static Utility()
- {
- _control = new Control();
- _graphics = _control.CreateGraphics();
- SCREEN_DPI = _graphics.DpiX;
- }
- #endregion
- #region FindWindow()
- [DllImport("user32.dll")]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- #endregion
- #region WakeupWindow()
- public static bool WakeupWindow(IntPtr hWnd)
- {
- if (hWnd == System.IntPtr.Zero)
- {
- return false;
- }
- if (Win32.IsIconic(hWnd) || !Win32.IsWindowVisible(hWnd))
- {
- Win32.ShowWindowAsync(hWnd, Win32.SW_RESTORE);
- }
- Win32.SetForegroundWindow(hWnd);
- return true;
- }
- #endregion
- #region 基本共通函数
- /// <summary>
- /// 空值判断
- /// </summary>
- /// <param name="value">判断对象</param>
- /// <returns>
- /// True:是空值
- /// False:不是空值
- /// </returns>
- public static bool IsNull(object value)
- {
- if (value == null || value == DBNull.Value)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 过滤sql语句中非法的字符,并替换
- /// </summary>
- /// <param name="value">替换前的sql语句</param>
- /// <returns>替换后的sql语句字符</returns>
- public static string SelectFilterLike(string value)
- {
- if (string.IsNullOrEmpty(value))
- {
- return string.Empty;
- }
- return value.Replace(@"'", @"''").Replace(@"[", @"[[]")
- .Replace(@"_", @"[_]").Replace(@"%", @"[%]").Replace(@"*", @"[*]");
- }
- /// <summary>
- /// 控制窗体的关闭按钮是否能用
- /// </summary>
- /// <param name="hWnd">窗体句柄</param>
- /// <param name="bEnable">True:可用 False:不可用</param>
- /// <returns>
- /// 设置成功与否
- /// </returns>
- public static bool EnableFormCloseBox(IntPtr hWnd, bool bEnable)
- {
- if (hWnd == System.IntPtr.Zero)
- {
- return false;
- }
- IntPtr hMenu = Win32.GetSystemMenu(hWnd, 0);
- if (hMenu == System.IntPtr.Zero)
- {
- return false;
- }
- if (bEnable)
- {
- Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE, Win32.MF_ENABLED);
- }
- else
- {
- Win32.EnableMenuItem(hMenu, Win32.SC_CLOSE,
- Win32.MF_DISABLED | Win32.MF_GRAYED);
- }
- return true;
- }
- /// <summary>
- /// 判断是否是正确的日期格式
- /// </summary>
- /// <param name="strDateTime">被判断的日期</param>
- /// <param name="dateTime">返回被格式化的日期</param>
- /// <returns>
- /// True:日期格式
- /// False:不是日期格式
- /// </returns>
- public static bool IsValidDate(string strDateTime, out DateTime? dateTime)
- {
- DateTime returnDatetime;
- if (DateTime.TryParse(strDateTime, out returnDatetime))
- {
- dateTime = returnDatetime;
- return true;
- }
- else
- {
- dateTime = null;
- return false;
- }
- }
- /// <summary>
- /// 取得显示顺序的最大值
- /// </summary>
- /// <param name="table">数据表</param>
- /// <param name="maxValName">列名</param>
- /// <returns>
- /// 该数据表里的最大显示顺序
- /// </returns>
- public static int GetMaxValue(DataTable table, string maxValName)
- {
- if (table == null)
- {
- return -1;
- }
- if (table.Columns.Contains(maxValName))
- {
- DataRow[] rows = table.Select("0 <= " + maxValName, maxValName + " DESC");
- if (rows == null || rows.Length == 0)
- {
- return -1;
- }
- object value = rows[0][maxValName];
- if (value == null)
- {
- return -1;
- }
- int maxValue = int.Parse(value.ToString());
- if (maxValue == short.MaxValue)
- {
- maxValue--;
- }
- return maxValue;
- }
- else
- {
- return -1;
- //DataRow[] rows = table.Select("0 <= " + maxValName,maxValName + "DESC");
- //object value = rows[0][maxValName];
- //int maxValue = int.Parse(value.ToString());
- //return maxValue;
- }
- }
- /// <summary>
- /// 取得数据表里的最大编码值
- /// </summary>
- /// <param name="table">数据表</param>
- /// <param name="maxValName">列名</param>
- /// <returns>该数据表里的最大编码值(string类型)</returns>
- /// <remarks>
- /// 2012.07.10 余再伟 创建
- /// </remarks>
- public static string GetStringMaxValue(DataTable table, string maxValName)
- {
- // 表为空时候直接返回空值
- if (table == null)
- {
- return string.Empty;
- }
- // 表中包含该列名的时候返回该数据表的最大编码值
- if (table.Columns.Contains(maxValName))
- {
- DataRow[] rows = table.Select("'0' <= " + maxValName, maxValName + " DESC");
- if (rows == null || rows.Length == 0)
- {
- return string.Empty;
- }
- object value = rows[0][maxValName];
- if (value == null)
- {
- return string.Empty;
- }
- string maxValue = value.ToString();
- return maxValue;
- }
- else
- {
- return string.Empty;
- }
- }
- /// <summary>
- /// 校验值是否重复
- /// </summary>
- /// <param name="curCellValue">当前Column的值</param>
- /// <param name="curRow">当前行的行号</param>
- /// <param name="dataGridView">所在的DataGridView</param>
- /// <param name="uniqueValue">比较值</param>
- /// <returns>是否存在</returns>
- public static bool IsUnique(string curCellValue, int curRow, DataGridView dataGridView, string uniqueValue)
- {
- foreach (DataGridViewRow rowItem in dataGridView.Rows)
- {
- if (rowItem.Index == curRow)
- {
- continue;
- }
- // 字典名称
- if (rowItem.Cells[uniqueValue].Value != null)
- {
- if (rowItem.Cells[uniqueValue].Value.ToString().Trim().
- Equals(curCellValue.Trim()))
- {
- return false;
- }
- }
- }
- return true;
- }
- /// <summary>
- /// 根据输入的汉字,返回所有汉字拼音的首字母串
- /// </summary>
- /// <param name="chineseString">输入的汉字</param>
- /// <returns>汉字拼音的首字母串</returns>
- public static string ChineseToPinyinCap(string chineseString)
- {
- string pinyinCapString = ""; // 转换后的拼音首字母字符串
- // 每个汉字存储需要两个字节
- byte[] ZW = new byte[2];
- long ChineseStr_int;
- string CharStr;
- string ChinaStr = "";
- chineseString = ToDBC(chineseString);
- for (int i = 0; i <= chineseString.Length - 1; i++)
- {
- // 取出一个汉字
- CharStr = chineseString.Substring(i, 1).ToString();
- // 得到汉字的字节表示
- ZW = System.Text.Encoding.Default.GetBytes(CharStr);
- // 得到汉字符的字节数组
- if (ZW.Length == 2)
- {
- int i1 = (short)(ZW[0]);
- int i2 = (short)(ZW[1]);
- ChineseStr_int = i1 * 256 + i2;
- #region 参数
- // table of the constant list
- // 'A'; //45217..45252
- // 'B'; //45253..45760
- // 'C'; //45761..46317
- // 'D'; //46318..46825
- // 'E'; //46826..47009
- // 'F'; //47010..47296
- // 'G'; //47297..47613
- // 'H'; //47614..48118
- // 'J'; //48119..49061
- // 'K'; //49062..49323
- // 'L'; //49324..49895
- // 'M'; //49896..50370
- // 'N'; //50371..50613
- // 'O'; //50614..50621
- // 'P'; //50622..50905
- // 'Q'; //50906..51386
- // 'R'; //51387..51445
- // 'S'; //51446..52217
- // 'T'; //52218..52697
- //没有U,V
- // 'W'; //52698..52979
- // 'X'; //52980..53640
- // 'Y'; //53689..54480
- // 'Z'; //54481..55289
- #endregion
- if ((ChineseStr_int >= 45217) && (ChineseStr_int <= 45252))
- {
- ChinaStr = "A";
- }
- else if ((ChineseStr_int >= 45253) && (ChineseStr_int <= 45760))
- {
- ChinaStr = "B";
- }
- else if ((ChineseStr_int >= 45761) && (ChineseStr_int <= 46317))
- {
- ChinaStr = "C";
- }
- else if ((ChineseStr_int >= 46318) && (ChineseStr_int <= 46825))
- {
- ChinaStr = "D";
- }
- else if ((ChineseStr_int >= 46826) && (ChineseStr_int <= 47009))
- {
- ChinaStr = "E";
- }
- else if ((ChineseStr_int >= 47010) && (ChineseStr_int <= 47296))
- {
- ChinaStr = "F";
- }
- else if ((ChineseStr_int >= 47297) && (ChineseStr_int <= 47613))
- {
- ChinaStr = "G";
- }
- else if ((ChineseStr_int >= 47614) && (ChineseStr_int <= 48118))
- {
- ChinaStr = "H";
- }
- else if ((ChineseStr_int >= 48119) && (ChineseStr_int <= 49061))
- {
- ChinaStr = "J";
- }
- else if ((ChineseStr_int >= 49062) && (ChineseStr_int <= 49323))
- {
- ChinaStr = "K";
- }
- else if ((ChineseStr_int >= 49324) && (ChineseStr_int <= 49895))
- {
- ChinaStr = "L";
- }
- else if ((ChineseStr_int >= 49896) && (ChineseStr_int <= 50370))
- {
- ChinaStr = "M";
- }
- else if ((ChineseStr_int >= 50371) && (ChineseStr_int <= 50613))
- {
- ChinaStr = "N";
- }
- else if ((ChineseStr_int >= 50614) && (ChineseStr_int <= 50621))
- {
- ChinaStr = "O";
- }
- else if ((ChineseStr_int >= 50622) && (ChineseStr_int <= 50905))
- {
- ChinaStr = "P";
- }
- else if ((ChineseStr_int >= 50906) && (ChineseStr_int <= 51386))
- {
- ChinaStr = "Q";
- }
- else if ((ChineseStr_int >= 51387) && (ChineseStr_int <= 51445))
- {
- ChinaStr = "R";
- }
- else if ((ChineseStr_int >= 51446) && (ChineseStr_int <= 52217))
- {
- ChinaStr = "S";
- }
- else if ((ChineseStr_int >= 52218) && (ChineseStr_int <= 52697))
- {
- ChinaStr = "T";
- }
- else if ((ChineseStr_int >= 52698) && (ChineseStr_int <= 52979))
- {
- ChinaStr = "W";
- }
- else if ((ChineseStr_int >= 52980) && (ChineseStr_int <= 53640))
- {
- ChinaStr = "X";
- }
- else if ((ChineseStr_int >= 53689) && (ChineseStr_int <= 54480))
- {
- ChinaStr = "Y";
- }
- else if ((ChineseStr_int >= 54481) && (ChineseStr_int <= 55289))
- {
- ChinaStr = "Z";
- }
- else
- {
- ChinaStr = CharStr;
- }
- pinyinCapString = pinyinCapString + ChinaStr;
- }
- else
- {
- pinyinCapString = pinyinCapString + CharStr.ToUpper();
- }
- }
- return pinyinCapString;
- }
- #endregion
- #region 半角全角转换
- /// <summary>
- /// 转半角的函数(DBC)
- /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// 全角空格为12288,半角空格为32
- /// </summary>
- /// <param name="inputString">任意字符串</param>
- /// <returns>半角字符串</returns>
- public static string ToDBC(string inputString)
- {
- char[] c = inputString.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- {
- c[i] = (char)(c[i] - 65248);
- }
- }
- return new String(c);
- }
- /// <summary>
- /// 转全角的函数(SBC)
- /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
- /// 全角空格为12288,半角空格为32
- /// </summary>
- /// <param name="inputString">任意字符串</param>
- /// <returns>全角字符串</returns>
- public static string ToSBC(string inputString)
- {
- // 半角转全角:
- char[] c = inputString.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 32)
- {
- c[i] = (char)12288;
- continue;
- }
- if (c[i] < 127)
- c[i] = (char)(c[i] + 65248);
- }
- return new String(c);
- }
- #endregion
- #region 正则判断数据合法性
- /// <summary>
- /// 验证邮箱格式
- /// </summary>
- /// <param name="email">要验证的邮箱地址</param>
- /// <returns>
- /// True:邮箱格式正确
- /// False:邮箱格式错误
- /// </returns>
- public static bool IsValidEmail(string email)
- {
- if (!Regex.IsMatch(email,
- @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- ///// <summary>
- ///// 验证身份证输入是否正确
- ///// </summary>
- ///// <param name="IdCard">要验证的身份证号</param>
- ///// <returns>
- ///// True:身份证格式正确
- ///// False:身份证格式错误
- ///// </returns>
- //public static bool IsIDcard(string IdCard)
- //{
- // bool result = false;
- // if (IdCard.Length <= 15)
- // {
- // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING15);
- // }
- // else
- // {
- // result = Regex.IsMatch(IdCard, Constant.IDCARD_STRING);
- // }
- // return result;
- //}
- /// <summary>
- /// 判断身份证号是否合法(对外部公开)
- /// </summary>
- /// <param name="strCardID">身份证号(旧/新)</param>
- /// <returns>false:不合法 true:合法</returns>
- public static bool IsIDcard(string strCardID)
- {
- strCardID = strCardID.ToLower();
- // 验证身份证的位数
- string strRegex = @"(\d{15}$)|(\d{17}(\d|x)$)";
- if (!Regex.IsMatch(strCardID, strRegex))
- {
- return false;
- }
- // 身份证数据有效性验证
- string strNewCardID = strCardID;
- if (strCardID.Length == 15)
- {
- strNewCardID = Convert15To18(strCardID);
- }
- if (!CheckCardIDInfo(strNewCardID))
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 判断身份证号是否合法
- /// </summary>
- /// <param name="strCardID">新身份证号</param>
- /// <returns>false:不合法 true:合法</returns>
- private static bool CheckCardIDInfo(string strCardID)
- {
- string[] aCity = new string[] { null, null, null, null, null, null, null, null, null,
- null, null, "北京", "天津 ", "河北", "山西", "内蒙古", null, null, null, null,
- null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海",
- "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南",
- "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川",
- "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃",
- "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null,
- null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null,
- null, null, null, "国外" };
- strCardID = strCardID.ToLower();
- strCardID = strCardID.Replace("x", "a");
- try
- {
- if (aCity[int.Parse(strCardID.Substring(0, 2))] == null)
- {
- return false;
- }
- DateTime.Parse(strCardID.Substring(6, 4) + "-"
- + strCardID.Substring(10, 2) + "-"
- + strCardID.Substring(12, 2));
- }
- catch
- {
- return false;
- }
- double iSum = 0;
- for (int i = 17; i >= 0; i--)
- {
- iSum += (System.Math.Pow(2, i) % 11) * int.Parse(strCardID[17 - i].ToString(),
- System.Globalization.NumberStyles.HexNumber);
- }
- if (iSum % 11 != 1)
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 从老身份证升位到新身份证
- /// </summary>
- /// <param name="strCardID">老身份证号码</param>
- /// <returns>新身份证号码</returns>
- private static string Convert15To18(string strCardID)
- {
- int intN = 0;
- // 加权因子常数
- int[] intW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
- // 校验码常数
- string strLastCode = "10X98765432";
- // 新身份证号
- string strNewCardID;
- strNewCardID = strCardID.Substring(0, 6);
- // 填在第6位及第7位上填上‘1’,‘9’两个数字
- strNewCardID += "19";
- strNewCardID += strCardID.Substring(6, 9);
- // 进行加权求和
- for (int i = 0; i < 17; i++)
- {
- intN += int.Parse(strNewCardID.Substring(i, 1)) * intW[i];
- }
- // 取模运算,得到模值
- int intY = intN % 11;
- // 从strLastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
- strNewCardID += strLastCode.Substring(intY, 1);
- return strNewCardID;
- }
- /// <summary>
- /// 验证车牌号输入是否正确
- /// </summary>
- /// <param name="PlateNumber">要验证的车牌号</param>
- /// <returns>
- /// True:车牌号格式正确
- /// False:车牌号格式错误
- /// </returns>
- public static bool IsPlateNumber(string PlateNumber)
- {
- return Regex.IsMatch(PlateNumber, Constant.REGEX_PLATENUMBER_STRING);
- }
- /// <summary>
- /// 验证车架号输入是否正确
- /// </summary>
- /// <param name="PlateNumber">要验证的车架号</param>
- /// <returns>
- /// True:车架号格式正确
- /// False:车架号格式错误
- /// </returns>
- public static bool IsVehicleNumber(string VehicleNumber)
- {
- return Regex.IsMatch(VehicleNumber, Constant.REGEX_VEHICLENUM_STRING);
- }
- /// <summary>
- /// 验证公司网址输入格式是否正确
- /// </summary>
- /// <param name="http">要验证的网址</param>
- /// True:网址格式正确
- /// False:网址格式错误
- /// <returns></returns>
- public static bool IsValidHttp(string http)
- {
- if (!Regex.IsMatch(http, Constant.REGEX_HTTP_STRING))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- /// <summary>
- /// 验证输入字符串是否为数字类型(包括带小数的数字)
- /// </summary>
- /// <param name="value">要验证的字符串</param>
- /// <returns>
- /// True:字符串是数字
- /// False:字符串不是数字
- /// </returns>
- public static bool IsNumeric(string value)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
- }
- /// <summary>
- /// 验证输入字符串是否为合法时间(HH:mm或者HH:mm:ss)
- /// </summary>
- /// <param name="value">要验证的字符串</param>
- /// <returns>
- /// True:字符串是合法时间(HH:mm或者HH:mm:ss)
- /// False:字符串不是合法时间(HH:mm或者HH:mm:ss)
- /// </returns>
- public static bool IsTime(string value)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(value, @"^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$");
- }
- /// <summary>
- /// 验证输入字符串是否为合法日期(yyyy-MM-dd)
- /// </summary>
- /// <param name="value">要验证的字符串</param>
- /// <returns>
- /// True:字符串是合法日期(yyyy-MM-dd)
- /// False:字符串不是合法日期(yyyy-MM-dd)
- /// </returns>
- public static bool IsDate(string value)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(value,
- @"^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
- }
- #endregion
- #region 二进制与文件之间转换
- /// <summary>
- /// 将文件转换成字节流
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- /// <returns>
- /// 文件的字节流
- /// </returns>
- public static byte[] GetBytesByFilePath(string filePath)
- {
- // 传入的值非法的情况下,返回NULL
- if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
- {
- return null;
- }
- // 读取文件到字节流中
- FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- BinaryReader binaryReader = new BinaryReader(fileStream);
- byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
- fileStream.Flush();
- fileStream.Close();
- return fileBytes;
- }
- /// <summary>
- /// 将二进制转换成文件
- /// </summary>
- /// <param name="filePath">文件存放的全路径</param>
- /// <param name="fileByte">二进制文件</param>
- /// <returns>
- /// True:保存成功
- /// False:保存失败
- /// </returns>
- public static bool BinaryToFile(string filePath, byte[] fileByte)
- {
- if (string.IsNullOrEmpty(filePath) || fileByte == null)
- {
- return false;
- }
- try
- {
- FileStream filestream = File.Create(filePath, fileByte.Length);
- filestream.Write(fileByte, 0, fileByte.Length);
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 图片与二进制之间转换
- /// <summary>
- /// 将图片文件序列化成二进制流
- /// </summary>
- /// <param name="imagePath">文件路径</param>
- /// <returns>二进制流文件</returns>
- public static MemoryStream ImageToBinary(string imagePath)
- {
- if (string.IsNullOrEmpty(imagePath) || !File.Exists(imagePath))
- {
- return null;
- }
- try
- {
- // 将图片文件序列化成二进制流文件
- Image image = new Bitmap(imagePath);
- MemoryStream memorystream = new MemoryStream();
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(memorystream, image);
- return memorystream;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 将二进制流反序列化成图片文件
- /// </summary>
- /// <param name="memoryStream">二进制流文件</param>
- /// <returns>图片文件</returns>
- public static Image StreamToImage(MemoryStream memoryStream)
- {
- if (memoryStream == null)
- {
- return null;
- }
- try
- {
- // 将流文件反序列化成图片文件
- memoryStream.Position = 0;
- BinaryFormatter formatter = new BinaryFormatter();
- return (Image)formatter.Deserialize(memoryStream);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 将二进制转换成图片文件
- /// </summary>
- /// <param name="imageBinary">二进制</param>
- /// <returns>image</returns>
- public static Image BinaryToImage(byte[] imageBinary)
- {
- if (imageBinary == null)
- {
- return null;
- }
- try
- {
- MemoryStream memoryStream = new MemoryStream(imageBinary);
- return Image.FromStream(memoryStream);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据Image对象转换成二进制
- /// </summary>
- /// <param name="image">image对象</param>
- /// <returns>
- /// 二进制数组
- /// </returns>
- public static byte[] ImageToByteArray(Image image)
- {
- if (image == null)
- {
- return null;
- }
- try
- {
- MemoryStream memoryStream = new MemoryStream();
- image.Save(memoryStream, ImageFormat.Jpeg);
- return memoryStream.ToArray();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region Compare比较函数
- [Flags]
- public enum CompareType
- {
- None = 0,
- // DBNull.Value值与null值视为同一值
- DBNullAsNull = 1,
- // string.Empty值null值视为同一值
- EmptyAsNull = 2,
- // 全部都视为同一值
- All = DBNullAsNull | EmptyAsNull
- }
- /// <summary>
- /// 两个值比较函数
- /// </summary>
- /// <param name="value1">值1</param>
- /// <param name="value2">值2</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- public static bool Compare(object value1, object value2)
- {
- return Compare(value1, value2, CompareType.None);
- }
- /// <summary>
- /// 两个值比较函数
- /// </summary>
- /// <param name="value1">值1</param>
- /// <param name="value2">值2</param>
- /// <param name="compareFlag">比较方式</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- public static bool Compare(object value1, object value2, CompareType compareFlag)
- {
- bool isNull1 = (value1 == null);
- bool isNull2 = (value2 == null);
- if ((compareFlag & CompareType.DBNullAsNull) == CompareType.DBNullAsNull)
- {
- if (value1 == DBNull.Value)
- {
- isNull1 = true;
- }
- if (value2 == DBNull.Value)
- {
- isNull2 = true;
- }
- }
- if ((compareFlag & CompareType.EmptyAsNull) == CompareType.EmptyAsNull)
- {
- if (string.Empty.Equals(value1))
- {
- isNull1 = true;
- }
- if (string.Empty.Equals(value2))
- {
- isNull2 = true;
- }
- }
- if (isNull1 && isNull2)
- {
- return true;
- }
- if (value1 == null || value2 == null)
- {
- return false;
- }
- else
- {
- return CompareSub(value1, value2);
- }
- }
- /// <summary>
- /// 两个值比较函数
- /// </summary>
- /// <param name="value1">值1</param>
- /// <param name="value2">值2</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- private static bool CompareSub(object value1, object value2)
- {
- // 数据类型不一样的情况下,返回false
- if (value1.GetType().Equals(value2.GetType()) == false)
- {
- return false;
- }
- // 枚举比较的情况,不能使用Equals,需单独写方法比较
- if (value1 is System.Collections.IEnumerable
- && value2 is System.Collections.IEnumerable)
- {
- return CompareSub((System.Collections.IEnumerable)value1,
- (System.Collections.IEnumerable)value2);
- }
- else
- {
- return value1.Equals(value2);
- }
- }
- /// <summary>
- /// 两个枚举类型的比较
- /// </summary>
- /// <param name="enumerable1">值1</param>
- /// <param name="enumerable2">值2</param>
- /// <returns>
- /// True:两个值相等的情况
- /// False:两个值相等的以外情况
- /// </returns>
- private static bool CompareSub(System.Collections.IEnumerable enumerable1,
- System.Collections.IEnumerable enumerable2)
- {
- System.Collections.IEnumerator e1 = enumerable1.GetEnumerator();
- System.Collections.IEnumerator e2 = enumerable2.GetEnumerator();
- while (e1.MoveNext())
- {
- if (!e2.MoveNext())
- {
- return false;
- }
- if (!e1.Current.Equals(e2.Current))
- {
- return false;
- }
- }
- return !e2.MoveNext();
- }
- #endregion
- #region 校验数字
- /// <summary>
- /// 校验数据是否符合要求
- /// </summary>
- /// <param name="number">待验证的参数</param>
- /// <param name="numberName">待验证的参数名称</param>
- /// <param name="type">校验的种类</param>
- /// <returns>错误信息</returns>
- /// <remarks>
- /// 2012.10.8 周兴 新建
- /// </remarks>
- public static string IsValidNumber(string number, string numberName, byte type)
- {
- try
- {
- // 如果第一位是-,表面可能是负号,去掉之后进行校验
- if (!string.IsNullOrEmpty(number))
- {
- if (number.IndexOf("-") == 0)
- {
- number = number.Substring(1);
- }
- }
- string errorMsg = string.Empty;
- string maxNumber = string.Empty;
- string regex = string.Empty;
- // 验证Numeric(16,6)
- if (type == 1)
- {
- regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,6})?$";
- maxNumber = "9999999999.999999";
- }
- // 验证Numeric(14,6)
- else if (type == 2)
- {
- regex = @"^(0|([1-9][0-9]{0,7}))(\.[0-9]{0,6})?$";
- maxNumber = "99999999.999999";
- }
- // 验证Numeric(12,6)
- else if (type == 3)
- {
- regex = @"^(0|([1-9][0-9]{0,5}))(\.[0-9]{0,6})?$";
- maxNumber = "999999.999999";
- }
- // 验证Numeric(5,2)
- else if (type == 4)
- {
- regex = @"^(0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?$";
- maxNumber = "999.99";
- }
- // 验证Numeric(5,2)(采购返利方式的返点率)
- else if (type == 5)
- {
- regex = @"^((0|([1-9][0-9]{0,1}))(\.[0-9]{0,2})?|(100(\.0{0,2})?))$";
- maxNumber = "100.00";
- }
- // 验证Numeric(6,2)(系统参数舍零金额限制最大值1000.00)
- else if (type == 6)
- {
- regex = @"^((0|([1-9][0-9]{0,2}))(\.[0-9]{0,2})?|(1000(\.0{0,2})?))$";
- maxNumber = "1000.00";
- }
- // 验证Numeric(12,2)(任务策略中任务金额限制)
- else if (type == 7)
- {
- regex = @"^(0|([1-9][0-9]{0,9}))(\.[0-9]{0,2})?$";
- maxNumber = "9999999999.99";
- }
- // 验证Numeric(6,2)(商品编码中费用分摊系数)
- else if (type == 8)
- {
- regex = @"^(0|([1-9][0-9]{0,3}))(\.[0-9]{0,2})?$";
- maxNumber = "9999.99";
- }
- // 验证Numeric(9,2)(CA员工业绩岗位考核工资基数)
- else if (type == 9)
- {
- regex = @"^(0|([1-9][0-9]{0,6}))(\.[0-9]{0,2})?$";
- maxNumber = "9999999.99";
- }
- // 验证Numeric(3,2)(CA员工业绩提成比率)
- else if (type == 10)
- {
- regex = @"^(0|([0-9]))(\.[0-9]{0,2})?$";
- maxNumber = "9.99";
- }
- // 验证Numeric(3,1)(CB员工业绩提成比率)
- else if (type == 11)
- {
- regex = @"^(0|([1-9][0-9]{0,1}))(\.[0-9]{0,1})?$";
- maxNumber = "99.9";
- }
- // 验证Numeric(7,2)(店长提成策略出库净额递增额)
- else if (type == 12)
- {
- regex = @"^(0|([1-9][0-9]{0,4}))(\.[0-9]{0,2})?$";
- maxNumber = "99999.99";
- }
- if (!Regex.IsMatch(number, regex))
- {
- //errorMsg = string.Format(Messages.MESSAGE_W004, numberName, maxNumber);
- }
- return errorMsg;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 校验电话格式
- /// <summary>
- /// 校验电话是否符合格式
- /// </summary>
- /// <param name="telephone"></param>
- /// <returns></returns>
- public static bool IsValidTelephone(string telephone)
- {
- try
- {
- /**
- * 手机号码
- * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
- * 联通:130,131,132,152,155,156,185,186
- * 电信:133,1349,153,180,181,189
- */
- string mobile = "^1(3[0-9]|5[0-35-9]|8[012345-9])\\d{8}$";
- /**
- * 大陆地区固话及小灵通 区号和电话之间没有-
- * 区号:010,020,021,022,023,024,025,027,028,029
- * 号码:七位或八位
- */
- string phs = "^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
- if (!Regex.IsMatch(telephone, mobile) && !Regex.IsMatch(telephone, phs))
- {
- return false;
- }
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 验证规格是否符合计算面积格式
- /// <summary>
- /// 验证规格是否符合计算面积格式
- /// </summary>
- /// <param name="specification">规格</param>
- /// <returns></returns>
- public static bool IsValidAcreage(string specification)
- {
- try
- {
- string regex = @"^\d+\*\d+$";
- if (Regex.IsMatch(specification, regex))
- {
- return true;
- }
- return false;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 验证规格是否符合计算体积格式
- /// <summary>
- /// 验证规格是否符合计算体积格式
- /// </summary>
- /// <param name="specification">规格</param>
- /// <returns></returns>
- public static bool IsValidVolume(string specification)
- {
- try
- {
- string regex = @"^\d+\*\d+\*\d+$";
- if (Regex.IsMatch(specification, regex))
- {
- return true;
- }
- return false;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 长度与像素之间转换
- /// <summary>
- /// 1/100 英寸转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">1/100 英寸</param>
- /// <returns>毫米</returns>
- public static float Inch100ToMillimeter(int inch100)
- {
- float mm = inch100 * MILLIMETER_PER_INCH / 100;
- return mm;
- }
- /// <summary>
- /// 1/100 英寸转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">1/100 英寸</param>
- /// <returns>毫米</returns>
- public static float Inch100ToMillimeter(float inch100)
- {
- float mm = inch100 * MILLIMETER_PER_INCH / 100;
- return mm;
- }
- /// <summary>
- /// 磅转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="point">磅</param>
- /// <returns>毫米</returns>
- public static float PointToMillimeter(float point)
- {
- float mm = point / POINT_PER_INCH * MILLIMETER_PER_INCH;
- return mm;
- }
- /// <summary>
- /// 磅转换为像素
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="point">磅</param>
- /// <returns>像素</returns>
- public static int PointToPixel(float point)
- {
- decimal pixel = System.Convert.ToDecimal(point) /
- System.Convert.ToDecimal(POINT_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- /// <summary>
- /// 毫米转换为1/100 英寸
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>1/100 英寸</returns>
- public static int MillimeterToInch100(float mm)
- {
- float inch100 = mm / MILLIMETER_PER_INCH * 100;
- return System.Convert.ToInt32(Math.Round(inch100, 0));
- }
- /// <summary>
- /// 毫米转换为1/100 英寸
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>1/100 英寸</returns>
- public static float MillimeterToInch100Single(float mm)
- {
- float inch100 = mm / MILLIMETER_PER_INCH * 100;
- return inch100;
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="mm">毫米</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI) *
- System.Convert.ToDecimal(paperZoom) / 100m;
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static int MillimeterToPixel(float mm, double paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI) *
- System.Convert.ToDecimal(paperZoom);
- return System.Convert.ToInt32(Math.Round(pixel, 0));
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// 毫米转换为像素
- /// </summary>
- /// <param name="mm">毫米</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>像素</returns>
- public static float MillimeterToPixelSingle(float mm, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal pixel = System.Convert.ToDecimal(mm) /
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) *
- System.Convert.ToDecimal(SCREEN_DPI) *
- System.Convert.ToDecimal(paperZoom) / 100m;
- return System.Convert.ToSingle(pixel);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <remarks>
- /// 表示倍率 100%
- /// </remarks>
- /// <param name="pixel">像素</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(SCREEN_DPI);
- return System.Convert.ToSingle(mm);
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, int paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(SCREEN_DPI) /
- System.Convert.ToDecimal(paperZoom) * 100m;
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <param name="dpi">DPI</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, int paperZoom, float dpi)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(dpi) /
- System.Convert.ToDecimal(paperZoom) * 100m;
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 像素转换为毫米
- /// </summary>
- /// <param name="pixel">像素</param>
- /// <param name="paperZoom">表示倍率%</param>
- /// <returns>毫米</returns>
- public static float PixelToMillimeter(int pixel, double paperZoom)
- {
- if (0 < paperZoom)
- {
- decimal mm = System.Convert.ToDecimal(pixel) *
- System.Convert.ToDecimal(MILLIMETER_PER_INCH) /
- System.Convert.ToDecimal(SCREEN_DPI) /
- System.Convert.ToDecimal(paperZoom);
- return System.Convert.ToSingle(mm);
- }
- else
- {
- return 0f;
- }
- }
- /// <summary>
- /// 截断到指定的精度。
- /// </summary>
- /// <param name="value">截断的目标</param>
- /// <param name="digits">精度</param>
- /// <returns>截断后的数值</returns>
- public static float Truncate(float value, int digits)
- {
- double v = Math.Pow(10, digits);
- return Convert.ToSingle(Math.Truncate((double)value * v) / v);
- }
- /// <summary>
- /// 截断到指定的精度。
- /// </summary>
- /// <param name="value">截断的目标</param>
- /// <param name="digits">精度</param>
- /// <returns>截断后的数值</returns>
- public static decimal Truncate(decimal value, int digits)
- {
- decimal v = (decimal)Math.Pow(10, digits);
- return Math.Truncate(value * v) / v;
- }
- #endregion
- #region 字符转成条码图片
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>条码图片的Stream</returns>
- public static Image GetCode39Image(string barcode)
- {
- try
- {
- byte[] bytes = GetCode39Byte(barcode);
- MemoryStream ms = new MemoryStream(bytes);
- Image returnImage = Image.FromStream(ms);
- return returnImage;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>条码图片的Stream</returns>
- public static Stream GetCode39Stream(string barcode)
- {
- try
- {
- byte[] bytes = GetCode39Byte(barcode);
- Stream stream = new MemoryStream(bytes);
- return stream;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>条码图片的byte[]</returns>
- public static byte[] GetCode39Byte(string barcode)
- {
- try
- {
- MemoryStream memoryStream = new MemoryStream();
- Bitmap barcodeBitmap = GetCode39Bitmap(barcode);
- barcodeBitmap.Save(memoryStream, ImageFormat.Png);
- byte[] byteImage = new Byte[memoryStream.Length];
- byteImage = memoryStream.ToArray();
- return byteImage;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 根据条码生成条码图片
- /// </summary>
- /// <param name="barcode">条码字符</param>
- /// <returns>
- /// Bitmap条码图片
- /// </returns>
- public static Bitmap GetCode39Bitmap(string barcode)
- {
- try
- {
- int x = 5; // 左边界
- int y = 0; // 上边界
- int WidLength = 2; // 粗BarCode长度
- int NarrowLength = 1; // 细BarCode长度
- int BarCodeHeight = 50; // BarCode高度
- int intSourceLength = barcode.Length;
- string strEncode = "010010100"; // 编码字符串·初始值为 起始符号 *
- string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; // Code39的字母
- string[] Code39 = //Code39的各字母对应码
- {
- /**//* 0 */ "000110100",
- /**//* 1 */ "100100001",
- /**//* 2 */ "001100001",
- /**//* 3 */ "101100000",
- /**//* 4 */ "000110001",
- /**//* 5 */ "100110000",
- /**//* 6 */ "001110000",
- /**//* 7 */ "000100101",
- /**//* 8 */ "100100100",
- /**//* 9 */ "001100100",
- /**//* A */ "100001001",
- /**//* B */ "001001001",
- /**//* C */ "101001000",
- /**//* D */ "000011001",
- /**//* E */ "100011000",
- /**//* F */ "001011000",
- /**//* G */ "000001101",
- /**//* H */ "100001100",
- /**//* I */ "001001100",
- /**//* J */ "000011100",
- /**//* K */ "100000011",
- /**//* L */ "001000011",
- /**//* M */ "101000010",
- /**//* N */ "000010011",
- /**//* O */ "100010010",
- /**//* P */ "001010010",
- /**//* Q */ "000000111",
- /**//* R */ "100000110",
- /**//* S */ "001000110",
- /**//* T */ "000010110",
- /**//* U */ "110000001",
- /**//* V */ "011000001",
- /**//* W */ "111000000",
- /**//* X */ "010010001",
- /**//* Y */ "110010000",
- /**//* Z */ "011010000",
- /**//* - */ "010000101",
- /**//* . */ "110000100",
- /**//*' '*/ "011000100",
- /**//* $ */ "010101000",
- /**//* / */ "010100010",
- /**//* + */ "010001010",
- /**//* % */ "000101010",
- /**//* * */ "010010100"
- };
- // 条码转成大写
- barcode = barcode.ToUpper();
- // 做成图片
- Bitmap objBitmap = new Bitmap(
- ((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2),
- BarCodeHeight + (y * 2));
- Graphics objGraphics = Graphics.FromImage(objBitmap);
- // 底色
- objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
- for (int i = 0; i < intSourceLength; i++)
- {
- // 检查是否有非法字符
- if (AlphaBet.IndexOf(barcode[i]) == -1 || barcode[i] == '*')
- {
- objGraphics.DrawString("含有非法字符",
- SystemFonts.DefaultFont, Brushes.Red, x, y);
- return objBitmap;
- }
- // 查表编码
- strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(barcode[i])]);
- }
- strEncode = string.Format("{0}0010010100", strEncode); // 补充结束符号 *
- int intEncodeLength = strEncode.Length; // 编码后边的长度
- int intBarWidth;
- for (int i = 0; i < intEncodeLength; i++) // 根据码画出Code39 BarCode
- {
- intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
- objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,
- x, y, intBarWidth, BarCodeHeight);
- x += intBarWidth;
- }
- return objBitmap;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region INI文件读写
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="section">节点名称[如[TypeName]]</param>
- /// <param name="key">键</param>
- /// <param name="value">值</param>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public static long WriteIniFile(string section, string key, string value, string filePath)
- {
- try
- {
- return WritePrivateProfileString(section, key, value, filePath);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 读取INI文件
- /// </summary>
- /// <param name="section">节点名称</param>
- /// <param name="key">键</param>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public static string ReadIniFile(string section, string key, string filePath)
- {
- try
- {
- StringBuilder returnValue = new StringBuilder(1024);
- GetPrivateProfileString(section, key, "", returnValue, 1024, filePath);
- return returnValue.ToString();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region DataGridView限制
- /// <summary>
- /// 设定DataGridView的排序方式
- /// </summary>
- /// <param name="dataGridView">需要设置排序的DataGridView</param>
- /// <param name="sortMode">排序方式</param>
- public static void SetDataGridViewColumnsSortMode(DataGridView dataGridView,
- DataGridViewColumnSortMode sortMode)
- {
- if (dataGridView == null)
- {
- return;
- }
- for (int i = 0; i < dataGridView.Columns.Count; i++)
- {
- dataGridView.Columns[i].SortMode = sortMode;
- }
- }
- #endregion
- #region 时间字符串转换成DateTime类型
- /// <summary>
- /// 时间字符串转换成DateTime类型
- /// </summary>
- /// <param name="serverTime">服务器时间</param>
- /// <param name="timeStr">时间格式字符串</param>
- /// <returns></returns>
- public static DateTime ConvertTimeStr(DateTime serverTime, string timeStr)
- {
- try
- {
- DateTime dateTime = serverTime;
- string[] times = timeStr.Split(':');
- if (times.Length == 3)
- {
- dateTime = new DateTime(dateTime.Year, dateTime.Month,
- dateTime.Day, Convert.ToInt16(times[0]), Convert.ToInt16(times[1]),
- Convert.ToInt16(times[2]));
- }
- return dateTime;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- public static int? ToNullableInt32(this object obj)
- {
- if (obj == null || obj == DBNull.Value)
- {
- return null;
- }
- return Convert.ToInt32(obj);
- }
- }
- }
|