//[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "V", Justification = "V stands for value")] //[SuppressMessage("Microsoft.Naming", "CA1715:IdentifiersShouldHaveCorrectPrefix", MessageId = "T", Justification = "V stands for value")] /// <summary>获取属性值</summary> /// <typeparam name="V"></typeparam> /// <param name="propertyName"></param> /// <param name="nullValue"></param> /// <returns></returns> protected V GetPropertyValue <V>(string propertyName, V nullValue) { if (ViewState[propertyName] == null) { //+ 控件状态没有设置时,使用配置文件的全局设置 String name = this.GetType().FullName + "." + propertyName; if (typeof(V) == typeof(String) || typeof(V) == typeof(Int32) || typeof(V) == typeof(Boolean)) { return(Config.GetConfig <V>(name, nullValue)); } if (typeof(V) == typeof(Color)) { String str = Config.GetConfig <String>(name, null); if (!String.IsNullOrEmpty(str)) { TypeConverter convert = new WebColorConverter(); try { return((V)convert.ConvertFromString(str)); } catch { } } } return(nullValue); } return((V)ViewState[propertyName]); }
/// <summary> /// 获取图片 /// </summary> public byte[] GetImage() { Bitmap image = new Bitmap(validateCode.Length * 20, 35); Graphics g = Graphics.FromImage(image); try { WebColorConverter ww = new WebColorConverter(); g.Clear((Color)ww.ConvertFromString("#" + validateColor)); Random random = new Random(); //画图片的噪音线 for (int i = 0; i < 12; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2); } Font font = new Font("Georgia", 20, FontStyle.Bold | FontStyle.Italic); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true); g.DrawString(validateCode, font, brush, 0, 0); //画图片的噪音点 for (int i = 0; i < 12; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.White); } //画图片的噪音线 for (int i = 0; i < 12; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.FromArgb(random.Next())), x1, y1, x2, y2); } //画图片的边框线 //g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); MemoryStream ms = new MemoryStream(); image.Save(ms, ImageFormat.Gif); return(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }
protected void HideLabels(Label lblToShow, string color) { lblDeleteStatus.Visible = false; lblFileUploaded.Visible = false; lblMessage.Visible = false; lblToShow.Visible = true; WebColorConverter converter = new WebColorConverter(); lblToShow.ForeColor = (System.Drawing.Color)converter.ConvertFromString(color); }
/// <summary> /// #ff00ff, #f0f => color /// </summary> /// <param name="text"></param> /// <returns></returns> private Color ConvertStringToColor(string text) { if (!string.IsNullOrEmpty(text)) { Color r = Color.White; WebColorConverter wcc = new WebColorConverter(); text.Trim(); text = text.Replace("#", ""); if (text.Length == 6) { r = (Color)wcc.ConvertFromString("#" + text); } if (text.Length == 3) { text = string.Format("{0}{0}{1}{1}{2}{2}", text[0], text[1], text[2]); r = (Color)wcc.ConvertFromString("#" + text); } return(r); } return(Color.White); }
protected void OpenInIFrame(ExcelWorksheetCollection worksheets, string currentSheet) { #region Set Active Worksheet if (currentSheet != null) { worksheets.ActiveWorksheet = worksheets[currentSheet]; } else { worksheets.ActiveWorksheet = worksheets[0]; } #endregion #region Save Html file of Excel file WebColorConverter converter = new WebColorConverter(); lblSheet.ForeColor = (Color)converter.ConvertFromString("ThreeDDarkShadow"); lblSheet.Text = "You are watching sheet <b>" + worksheets.ActiveWorksheet.Name + "</b> of total <b>" + worksheets.Count + "</b> sheets in excel file <b>" + nameOfFile + "</b>"; string file = nameOfFile + "_" + worksheets.ActiveWorksheet.Name; string uplPath = Server.MapPath(@"~/Documents/Excel_Html/" + file + ".html"); string virPath = HttpRuntime.AppDomainAppVirtualPath + @"/Documents/Excel_Html/" + file + ".html"; xcel.SaveHtml(uplPath, null, true); #endregion #region Set Iframe src path excelFrame.Visible = true; excelFrame.Attributes["src"] = virPath; #endregion #region Show error if there is no data in the sheet CellRange range = worksheets.ActiveWorksheet.GetUsedCellRange(true); if (range == null) { lblSheet.ForeColor = System.Drawing.Color.Red; lblSheet.Text = "<b>ERROR</b> - There is no data in the selected sheet <b>" + worksheets.ActiveWorksheet.Name + "</b>"; } #endregion }
/*产生验证图片*/ public byte[] CreateImages(string code) { Bitmap image = new Bitmap(60, 20); Graphics g = Graphics.FromImage(image); WebColorConverter ww = new WebColorConverter(); g.Clear((Color)ww.ConvertFromString("#FAE264")); Random random = new Random(); //画图片的背景噪音线 for (int i = 0; i < 12; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2); } Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true); g.DrawString(code, font, brush, 0, 0); //画图片的前景噪音点 for (int i = 0; i < 10; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.White); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); byte[] bytes = ms.GetBuffer(); ms.Close(); g.Dispose(); image.Dispose(); return(bytes); }
/*产生验证图片*/ public void CreateImage(string code) { Bitmap image = new Bitmap(60, 25); Graphics g = Graphics.FromImage(image); WebColorConverter ww = new WebColorConverter(); g.Clear((Color)ww.ConvertFromString("#FAE264")); Random random = new Random(); //画图片的背景噪音线 for (int i = 0; i < 12; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2); } Font font = new Font("宋体", 14, FontStyle.Bold); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true); g.DrawString(code, font, brush, 0, 0); //画图片的前景噪音点 for (int i = 0; i < 10; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.White); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); }
public void CreateImages(string code) { Bitmap image = new Bitmap(60, 20); Graphics g = Graphics.FromImage(image); WebColorConverter ww = new WebColorConverter(); g.Clear((Color)ww.ConvertFromString("#AAE264")); Random random = new Random(); for (int i = 0; i < 12; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2); } Font font = new Font("Arial", 15, FontStyle.Bold | FontStyle.Italic); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Gray, 1.2f, true); g.DrawString(code, font, brush, 0, 0); for (int i = 0; i < 10; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.White); } g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); }
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.IO; public partial class ImgVode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string checkCode = CreateCode(4); Session["CheckCode"] = checkCode; CreateImage(checkCode); } /*产生验证码*/ public string CreateCode(int codeLength) { string so = "2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,T,U,V,W,X,Y,Z"; string[] strArr=so.Split(','); string code = ""; Random rand=new Random(); for (int i = 0; i < codeLength; i++) { code+=strArr[rand.Next(0, strArr.Length)]; } return code; } /*产生验证图片*/ public void CreateImage(string code) { Bitmap image = new Bitmap(80, 28); Graphics g = Graphics.FromImage(image); WebColorConverter ww=new WebColorConverter(); g.Clear((Color)ww.ConvertFromString("#ADC0D4")); Random random = new Random(); //画图片的背景噪音线 for (int i = 0; i < 50; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2); } Font font = new Font("Arial", 18, FontStyle.Bold | FontStyle.Italic); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.Gray,50,true); g.DrawString(code, font, brush, 4, 0); //画图片的前景噪音点 for (int i = 0; i < 60; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.Blue); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Black), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } }
public static Image CreateKitProductImage(LifeStyleColors color, Image productImage, Size imageSize, bool transparent, Color backgroundColor, bool antiAlias) { Bitmap result = new Bitmap(imageSize.Width, imageSize.Height); using (Graphics g = Graphics.FromImage(result)) { g.Clear(backgroundColor); if (antiAlias) { g.SmoothingMode = SmoothingMode.HighQuality; } else { g.SmoothingMode = SmoothingMode.HighSpeed; } g.InterpolationMode = InterpolationMode.HighQualityBicubic; // background // circle #region Draw Circle Color _colorOuter; Color _colorInner; WebColorConverter cc = new WebColorConverter(); switch (color) { case LifeStyleColors.LightBlue: _colorOuter = (Color)cc.ConvertFromString("#A8BADE"); _colorInner = (Color)cc.ConvertFromString("#89A5D5"); break; case LifeStyleColors.Orange: _colorOuter = (Color)cc.ConvertFromString("#F7C272"); _colorInner = (Color)cc.ConvertFromString("#F2B248"); break; case LifeStyleColors.Gray: _colorInner = (Color)cc.ConvertFromString("#616d8d"); _colorOuter = (Color)cc.ConvertFromString("#3d5076"); break; case LifeStyleColors.Blue: default: _colorOuter = (Color)cc.ConvertFromString("#425790"); _colorInner = (Color)cc.ConvertFromString("#2C4985"); break; } Rectangle outerRect = new Rectangle(1, 1, imageSize.Width - 2, imageSize.Height - 2); float borderWidth = 0.1f; g.FillEllipse(new SolidBrush(_colorOuter), outerRect); outerRect.Inflate((int)(-borderWidth * outerRect.Width), (int)(-borderWidth * outerRect.Height)); g.FillEllipse(new SolidBrush(_colorInner), outerRect); #endregion double maxWidth = Math.Sin(0.25 * Math.PI) * (double)imageSize.Width; double maxHeight = Math.Cos(0.25 * Math.PI) * (double)imageSize.Height; #region Product int avX = (int)maxWidth; // - (int)(2 * (_padding * imageSize.Width)); int avY = (int)maxHeight; // -(int)(2 * (_padding * imageSize.Height)); Rectangle resizedSize = GetFixedSize(productImage, avX, avY, true); int x = (imageSize.Width - resizedSize.Width) / 2; int y = (imageSize.Height - resizedSize.Height) / 2; g.DrawImage(productImage, new Rectangle(x, y, resizedSize.Width, resizedSize.Height)); #endregion // product } result.MakeTransparent(backgroundColor); return(result); }