using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.Serialization.Json; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Web.Services.Protocols; using System.Security.Cryptography; using System.Text; using System.Web.SessionState; using System.Data; using System.Drawing.Imaging; using System.Drawing; using System.IO; using System.Data.SqlClient; using System.Configuration; using System.Net; using System.Text.RegularExpressions; /// /// globalClass 的摘要描述 /// public static class globalClass { public static bool isURL(string url) { return Uri.IsWellFormedUriString(url, UriKind.Absolute); } public static string GetIPAddress() { System.Web.HttpContext context = System.Web.HttpContext.Current; string sIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(sIPAddress)) { return context.Request.ServerVariables["REMOTE_ADDR"]; } else { string[] ipArray = sIPAddress.Split(new Char[] { ',' }); return ipArray[0]; } } public static Boolean Base64ToFile(string base64String, string filePath) { Boolean ret = false; try { byte[] imageBytes = Convert.FromBase64String(base64String.Replace("data:image/png;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "").Replace("data:application/pdf;base64,", "").Replace("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,", "")); File.WriteAllBytes(filePath, imageBytes); ret = true; } catch { ret = false; } return ret; } public static System.Drawing.Image Base64ToImage(string base64String) { // Convert Base64 String to byte[] byte[] imageBytes = Convert.FromBase64String(base64String.Replace("data:image/png;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "")); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); // Convert byte[] to Image ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image__1 = System.Drawing.Image.FromStream(ms, true); return image__1; } public static Stream Base64ToStream(string base64String) { Stream newReturn; try { byte[] imageBytes = Convert.FromBase64String(base64String.Replace("data:image/png;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "").Replace("data:application/pdf;base64,", "").Replace("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,", "")); //File.WriteAllBytes(filePath, imageBytes); newReturn = new MemoryStream(imageBytes); } catch { newReturn = null; } return newReturn; } // 取得網址圖片 public static System.Drawing.Image DownloadImageFromUrl(string imageUrl) { System.Drawing.Image image = null/* TODO Change to default(_) if this is not a reference type */; try { System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl); webRequest.AllowWriteStreamBuffering = true; webRequest.Timeout = 30000; System.Net.WebResponse webResponse = webRequest.GetResponse(); System.IO.Stream stream = webResponse.GetResponseStream(); image = System.Drawing.Image.FromStream(stream); webResponse.Close(); } catch (Exception ex) { return null/* TODO Change to default(_) if this is not a reference type */; } return image; } public static System.Drawing.Image fileToImage(System.IO.Stream fileContent) { try { System.Drawing.Image imgObj; long fileLength = fileContent.Length; byte[] imageBuffer = new byte[fileLength - 1 + 1]; fileContent.Read(imageBuffer, 0, System.Convert.ToInt32(fileLength)); // 將圖片塞進MemoryStream MemoryStream inputMS = new MemoryStream(imageBuffer); imgObj = System.Drawing.Image.FromStream(inputMS); return imgObj; } catch { return null/* TODO Change to default(_) if this is not a reference type */; } } public static System.Drawing.Image markImage(System.Drawing.Image backImg, System.Drawing.Image waterImg) { // 設定背景圖片 Graphics gr = System.Drawing.Graphics.FromImage(backImg); float newP = float.Parse(backImg.Width.ToString()) / 720f; float newWH = 72f * newP; int newWidthHeight = Convert.ToInt32(newWH); Bitmap waterLogo = new Bitmap(waterImg); //新建logo浮水印圖片 Bitmap Logo = ScaleImage(waterLogo, newWidthHeight, newWidthHeight); gr.DrawImage(Logo, (backImg.Width - Logo.Width) / 2, (backImg.Height - Logo.Height) / 2); gr.Dispose(); return backImg; } public static System.Drawing.Image resizeImage(System.Drawing.Image img, int newWidth, int newHeight) { Bitmap imageOutput = new Bitmap(img, newWidth, newHeight); Image oimg = (Image)imageOutput; return oimg; } public static Bitmap ScaleImage(Bitmap pBmp, int pWidth, int pHeight) { try { Bitmap tmpBmp = new Bitmap(pWidth, pHeight); Graphics tmpG = Graphics.FromImage(tmpBmp); //tmpG.InterpolationMode = InterpolationMode.HighQualityBicubic; tmpG.DrawImage(pBmp, new Rectangle(0, 0, pWidth, pHeight), new Rectangle(0, 0, pBmp.Width, pBmp.Height), GraphicsUnit.Pixel); tmpG.Dispose(); return tmpBmp; } catch { return null; } } public static Stream ToStream(this Image image, ImageFormat format) { var stream = new System.IO.MemoryStream(); image.Save(stream, format); stream.Position = 0; return stream; } public static string CreateRandomCode(int Number) { string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z"; string[] allCharArray = allChar.Split(','); string randomCode = ""; Random rand = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i <= Number - 1; i++) { int t = rand.Next(allCharArray.Length); randomCode += allCharArray[t]; } return randomCode; } public static string CreateCaseRandomCode(int Number) { string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; string[] allCharArray = allChar.Split(','); string randomCode = ""; Random rand = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i <= Number - 1; i++) { int t = rand.Next(allCharArray.Length); randomCode += allCharArray[t]; } return randomCode; } public static bool IsDate(string strDate) { try { DateTime.Parse(strDate); return true; } catch { return false; } } public static string getPageData(string url) { if (url == null || url.Trim() == "") return ""; try { WebClient wc = new WebClient(); wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;"); wc.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = wc.DownloadData(url); return Encoding.UTF8.GetString(pageData);//.ASCII.GetString } catch (Exception ex) { return ""; } } public static Image getUrlPic(string url) { if (url == null || url.Trim() == "") return null; try { WebClient wc = new WebClient(); wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;"); wc.Credentials = CredentialCache.DefaultCredentials; System.IO.MemoryStream Ms = new System.IO.MemoryStream(wc.DownloadData(url)); Image img = Image.FromStream(Ms); return img; } catch (Exception ex) { return null; } } public static bool IsNumeric(string anyString) { if (anyString == null) { anyString = ""; } if (anyString.Length > 0) { double dummyOut = new double(); System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-US", true); return Double.TryParse(anyString, System.Globalization.NumberStyles.Any, cultureInfo.NumberFormat, out dummyOut); } else { return false; } } public static string getLocalBaseDomain() { return HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority; } public static string getLocalDomain() { return HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/'); } public static void AddScriptToHead(this System.Web.UI.HtmlControls.HtmlHead h, string script, bool AddScriptTags) { System.Web.UI.WebControls.Literal l = new System.Web.UI.WebControls.Literal(); if (AddScriptTags) { l.Text = @""; } else { l.Text = script; } h.Controls.Add(l); } }