需要这样身份证号+姓名的背景图吗?下边是我造的现成的,肯定不完美,但是复制粘贴就能使:)
使用方法:img.aspx?sfz=3700001151545185X&name=李大霄
<%@ Page Language="C#"%> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.Drawing.Drawing2D" %> <% /* * 身份证水印背景图 V1.0; * * GuYang * 2016/11/25 */ #region fun Func<int, int, int, int[]> f1 = null; { f1 = (w, h, angle) => { var an = (Math.PI / 180) * angle; var w1 = w - (h - w * Math.Tan(an)) * (Math.Tan(an) / (1 - Math.Pow(Math.Tan(an), 2))); var h1 = w1 * Math.Tan(an); var a = Math.Sqrt((Math.Pow(w1, 2) + Math.Pow(h1, 2))); var b = (h - h1) * a / w1; return new int[] { (int)a, (int)b }; }; } Func<Graphics, string, FontFamily, int, float[]> f2 = null; { f2 = (g, sfz, fontFamily, width) => { if (g == null || fontFamily == null || string.IsNullOrWhiteSpace(sfz) || sfz.Length < 15 || width < 50) return new float[] { 12F, 12F }; var font_em_size = 9F; var font_height = 1F; while (true) { var size = g.MeasureString(sfz, new Font(fontFamily, font_em_size++)); font_height = size.Height; if (size.Width >= width) break; } return new float[] { font_em_size, font_height }; }; } Func<Bitmap, float, Color, Bitmap> f3 = null; { f3 = (bmp, angle, bkColor) => { int w = bmp.Width + 2; int h = bmp.Height + 2; PixelFormat pf; if (bkColor == Color.Transparent) pf = PixelFormat.Format32bppArgb; else pf = bmp.PixelFormat; Bitmap tmp = new Bitmap(w, h, pf); Graphics g = Graphics.FromImage(tmp); g.Clear(bkColor); g.DrawImageUnscaled(bmp, 1, 1); g.Dispose(); GraphicsPath path = new GraphicsPath(); path.AddRectangle(new RectangleF(0f, 0f, w, h)); Matrix mtrx = new Matrix(); mtrx.Rotate(-angle); RectangleF rct = path.GetBounds(mtrx); Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf); g = Graphics.FromImage(dst); g.Clear(bkColor); g.TranslateTransform(-rct.X, -rct.Y); g.RotateTransform(-angle); g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawImageUnscaled(tmp, 0, 0); g.Dispose(); tmp.Dispose(); return dst; }; } var fun = new { f1 = f1, f2 = f2, f3 = f3,}; #endregion { //水印图片宽度 var width = 400; //try { if(!string.IsNullOrWhiteSpace(Request["width"])) width = Convert.ToInt32(Request["width"]); } //catch { } //水印图片高度 var height =300; //try { if(!string.IsNullOrWhiteSpace(Request["height"])) height = Convert.ToInt32(Request["height"]); } //catch { } //水印字体倾斜角度 var angle = 8; //身份证号 var sfz = Request["sfz"];//"3700001151545185X"; //姓名 var name = Request["name"];//"李大霄"; var bk_color = Color.White; var bk_bursh = Brushes.White; var font_color = Brushes.LightGray; var font_family = FontFamily.GenericSansSerif; var core_size = fun.f1(width, height, angle); var core_width = Math.Max(core_size[0],20); var core_height =Math.Max(core_size[1],8); var bmp = new Bitmap(core_width, core_height, PixelFormat.Format24bppRgb); var g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.AntiAlias; g.FillRectangle(bk_bursh, 0, 0, core_width, core_height); g.DrawRectangle(new Pen(bk_bursh), 0, 0, core_width-1, core_height-1); var em_size__font_height = fun.f2(g, sfz, font_family, core_width); var em_size = em_size__font_height[0]; var font = new Font(font_family, em_size); var font_height = em_size__font_height[1]; var line1y = (float)Math.Max(((core_height - 2 * font_height) * 0.5), 0); g.DrawString(sfz,font, font_color, 0, line1y); var line2y = (float)(line1y + font_height); var line2x = (float)Math.Max((core_width - g.MeasureString(name, font).Width) * 0.5, 0); g.DrawString(name, font, font_color, line2x, line2y); bmp = fun.f3(bmp, angle, bk_color); var ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Jpeg); Response.ContentType = "image/jpeg"; Response.BinaryWrite(ms.ToArray()); } %>