protected void Page_Load(object sender, EventArgs e) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); switch (Action) { case "text": { FontPack model = new FontPack().FromJson(Request["model"]); if (string.IsNullOrEmpty(model.text)) { throw new Exception("文字内容不能为空"); } int bmpLen = 0; int size = GetFontSize(model.text, ref bmpLen); //自动计算出文字大小 //------------------------------------ Bitmap empty = new Bitmap(bmpLen, (int)(size * 1.5)); //创建图片背景 Graphics g = Graphics.FromImage(empty); //g.Clear(model.ColorHx16toRGB(model.bkcolor));//清除画面,填充背景 g.Clear(Color.FromArgb(0)); Font mFont = new Font(model.family, size, FontStyle.Regular); g.DrawString(model.text, mFont, new SolidBrush(model.ColorHx16toRGB(model.color)),//Color.FromArgb(100,255,255,255) new Point(0, 0)); empty.Save(ms, System.Drawing.Imaging.ImageFormat.Png); } break; case "logo": default: { //int LogoID = DataConvert.CLng(Request.QueryString["LogoID"]); //string svgPath = Server.MapPath(iconBll.PlugDir + "assets/icons/" + LogoID + ".svg"); //SvgDocument svg = SvgDocument.Open(svgPath); //Bitmap bmp = svg.Draw(); //System.Drawing.Image empty = ImgHelper.ReadImgToMS(iconBll.PlugDir + "assets/empty.png"); //Graphics g = Graphics.FromImage(empty); ////SizeF textSize = g.MeasureString(CompName, new Font("")); //g.DrawImage(bmp, new Point() { X = 95, Y = 40 });//400,400,商标也固定大小 // //添加文字 //Font mFont = new Font(SelFont("m"), GetFontSize(CompName), FontStyle.Regular); //Font sFont = new Font(SelFont("s"), (GetFontSize(SubTitle) / 2), FontStyle.Regular); //g.DrawString(CompName, mFont, // new SolidBrush(Color.FromArgb(100, 0, 0, 0)), // GetPosition(empty, g.MeasureString(CompName, mFont), 265)); //g.DrawString(SubTitle, sFont, // new SolidBrush(Color.FromArgb(100, 102, 102, 102)), // GetPosition(empty, g.MeasureString(SubTitle, sFont), 305 + (int)mFont.Size)); //empty.Save(ms, System.Drawing.Imaging.ImageFormat.Png); } break; } //------------ Response.Cache.SetNoStore(); Response.ClearContent(); Response.ContentType = "image/gif"; Response.BinaryWrite(ms.ToArray()); }
protected void Page_Load(object sender, EventArgs e) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); switch (Action) { case "text": { FontPack model = new FontPack().FromJson(Request["model"]); if (string.IsNullOrEmpty(model.text)) { throw new Exception("文字内容不能为空"); } int bmpLen = 0; int maxWidth = 0; //用于list.aspx,不可超过原有的最大宽度 maxWidth = model.direction == "vertical" ? 0:DataConvert.CLng(Request["width"]); int size = GetFontSize(model.text, maxWidth, ref bmpLen); //自动计算出文字大小 //根据字体,进行一定程度的缩放0.8-1(特殊要求,不可超过原宽度) switch (model.family) { case "architech": size = (int)(size * 0.83); break; case "harlow solid italic": size = (int)(size * 0.90); break; } //"Aileron_Light|BPdotsDiamond|Bauhaus|balloon|architech|belshaw|bpreplay|noodle_script|harlow_solid_italic|arial|arial_narrow|Arial_Rounded_MT_bold" //------------------------------------ //根据方向生成不同的宽度的图片 Font mFont = new Font(model.family, size, FontStyle.Regular); Bitmap empty = null; //有的字体未定义小写字母 switch (model.direction) { case "vertical": { model.text = model.text.ToUpper(); //垂直下强制大写 empty = new Bitmap((int)(size * 1.5), (int)(bmpLen * 1.3)); Graphics g = Graphics.FromImage(empty); g.Clear(Color.FromArgb(0)); //每个字为一行 char[] chars = model.text.ToCharArray(); SolidBrush brush = new SolidBrush(model.ColorHx16toRGB(model.color)); //Color.FromArgb(100,255,255,255) int lastY = 0; //最近一次的Y轴定位 for (int i = 0; i < chars.Length; i++) { int x = 0; string text = chars[i] + ""; //空格不用占过多宽 if (text == " ") { lastY += 8; continue; } else if (i == 0) { } else { lastY = lastY + (int)(model.size * 2.5); } //Char.IsLower(chars[i]) || if (text == "I") { x = model.size / 2; } g.DrawString(text, mFont, brush, new Point(x, lastY)); } } break; default: // { //水平下做自动居中处理(即图片宽度不变,前后留空) int bmpWidth = bmpLen; if (maxWidth > 0 && maxWidth > bmpLen) { bmpWidth = maxWidth; } int bmpStart = (int)((bmpWidth - bmpLen) / 2); //throw new Exception(bmpStart + "|" + bmpWidth + "|" + bmpLen); empty = new Bitmap(bmpWidth, (int)(size * 1.5)); Graphics g = Graphics.FromImage(empty); //g.Clear(model.ColorHx16toRGB(model.bkcolor));//清除画面,填充背景 g.Clear(Color.FromArgb(0)); g.DrawString(model.text, mFont, new SolidBrush(model.ColorHx16toRGB(model.color)), //Color.FromArgb(100,255,255,255) new Point(bmpStart, 0)); } break; } empty.Save(ms, System.Drawing.Imaging.ImageFormat.Png); } break; case "logo": default: { //int LogoID = DataConvert.CLng(Request.QueryString["LogoID"]); //string svgPath = Server.MapPath(iconBll.PlugDir + "assets/icons/" + LogoID + ".svg"); //SvgDocument svg = SvgDocument.Open(svgPath); //Bitmap bmp = svg.Draw(); //System.Drawing.Image empty = ImgHelper.ReadImgToMS(iconBll.PlugDir + "assets/empty.png"); //Graphics g = Graphics.FromImage(empty); ////SizeF textSize = g.MeasureString(CompName, new Font("")); //g.DrawImage(bmp, new Point() { X = 95, Y = 40 });//400,400,商标也固定大小 // //添加文字 //Font mFont = new Font(SelFont("m"), GetFontSize(CompName), FontStyle.Regular); //Font sFont = new Font(SelFont("s"), (GetFontSize(SubTitle) / 2), FontStyle.Regular); //g.DrawString(CompName, mFont, // new SolidBrush(Color.FromArgb(100, 0, 0, 0)), // GetPosition(empty, g.MeasureString(CompName, mFont), 265)); //g.DrawString(SubTitle, sFont, // new SolidBrush(Color.FromArgb(100, 102, 102, 102)), // GetPosition(empty, g.MeasureString(SubTitle, sFont), 305 + (int)mFont.Size)); //empty.Save(ms, System.Drawing.Imaging.ImageFormat.Png); } break; } //------------ Response.Cache.SetNoStore(); Response.ClearContent(); Response.ContentType = "image/gif"; Response.BinaryWrite(ms.ToArray()); }