[原创]GDI+生成准考证(新改版)
Random random = new Random();
//创建画布
Bitmap bt = new Bitmap(353, 404, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bt);
//定义文字样式
Font fn1 = new Font("Tahoma", 10, FontStyle.Bold);
Font fn = new Font("Tahoma", 9, FontStyle.Bold);
//填充背景色
g.Clear(Color.LightBlue);
//填文字
g.DrawString("中国电子学会电子设计工程师认证考试", fn1, Brushes.Black, new PointF(46, 20));
g.DrawString("准 考 证", fn1, Brushes.Black, new PointF(140, 50));
g.DrawString("准考证号", fn, Brushes.Black, new PointF(14, 85));
g.DrawString("EDP200903060001", fn, Brushes.Black, new PointF(90, 85));
g.DrawString("考生姓名", fn, Brushes.Black, new PointF(14, 115));
g.DrawString("杜润龙", fn, Brushes.Black, new PointF(90, 115));
g.DrawString("考生性别", fn, Brushes.Black, new PointF(14, 145));
g.DrawString("男", fn, Brushes.Black, new PointF(90, 145));
g.DrawString("身份证号", fn, Brushes.Black, new PointF(14, 175));
g.DrawString("411024198806021230", fn, Brushes.Black, new PointF(90, 175));
g.DrawString("考试级别", fn, Brushes.Black, new PointF(14, 205));
g.DrawString("电子设计助理工程师", fn, Brushes.Black, new PointF(90, 205));
g.DrawString("考试科目", fn, Brushes.Black, new PointF(14, 235));
g.DrawString("综合知识", fn, Brushes.Black, new PointF(90, 235));
g.DrawString("实际操作", fn, Brushes.Black, new PointF(220, 235));
g.DrawString("考试时间", fn, Brushes.Black, new PointF(14, 265));
g.DrawString("09-05-25 PM 8:00", fn, Brushes.Black, new PointF(90, 265));
g.DrawString("09-05-25 PM 8:00", fn, Brushes.Black, new PointF(220, 265));
g.DrawString("学校名称", fn, Brushes.Black, new PointF(14, 295));
g.DrawString("北京工业大学", fn, Brushes.Black, new PointF(90, 295));
g.DrawString("北京师范大学", fn, Brushes.Black, new PointF(220, 295));
g.DrawString("考场编号", fn, Brushes.Black, new PointF(14, 323));
g.DrawString("第一考场", fn, Brushes.Black, new PointF(90, 323));
g.DrawString("第九考场", fn, Brushes.Black, new PointF(220, 323));
g.DrawString("详细地址", fn, Brushes.Black, new PointF(14, 353));
string dizhi = "北京市北京工业大学地址北京工业";
int j = 335; //初始坐标
int j2 = 335; //初始坐标
for (int z = 0; z < 17; z += 8) //循环次数
{
int k = z + 10; // 第一次为0时
if (k > dizhi.Length) //k是否大于总字数
{
k = dizhi.Length - k; // 若大于比如k=45 dizhi.length=40 k=40-45 k=-5
int i = k + 10; //i = -5+20 这样就不至于截取超出范围
g.DrawString(dizhi.Substring(z, i), fn, Brushes.Black, new PointF(90, j += 18));
break;
}
g.DrawString(dizhi.Substring(z, 8), fn, Brushes.Black, new PointF(90, j += 18));
}
string dizhi2 = "北京市北京师范大学地址北京师范";
for (int z2 = 0; z2 < 17; z2 += 8) //循环次数
{
int k2 = z2 + 10; // 第一次为0时
if (k2 > dizhi2.Length) //k是否大于总字数
{
k2 = dizhi2.Length - k2; // 若大于比如k=45 dizhi.length=40 k=40-45 k=-5
int i2 = k2 + 10; //i = -5+20 这样就不至于截取超出范围
g.DrawString(dizhi2.Substring(z2, i2), fn, Brushes.Black, new PointF(220, j2 += 18));
break;
}
g.DrawString(dizhi2.Substring(z2, 8), fn, Brushes.Black, new PointF(220, j2 += 18));
}
//产生杂点
for (int i = 0; i < 500; i++)
{
int x1 = random.Next(bt.Width - 20);
int y1 = random.Next(bt.Height - 20);
bt.SetPixel(x1, y1, Color.FromArgb(random.Next()));
}
//产生随机曲线
for (int i = 0; i < 50; i++)
{
int x1 = random.Next(bt.Width - 20);
int y1 = random.Next(bt.Height - 20);
int x2 = random.Next(1, 30);
int y2 = random.Next(1, 20);
int x3 = random.Next(15, 45);
int y3 = random.Next(70, 270);
g.DrawArc(new Pen(Color.FromArgb(random.Next())), x1, y1, x2, y2, x3, y3);
}
//画横线
g.DrawLine(new Pen(Color.Black), 10, 75, 341, 75); //1
g.DrawLine(new Pen(Color.Black), 10, 105, 243, 105); //2
g.DrawLine(new Pen(Color.Black), 10, 135, 243, 135); //3
g.DrawLine(new Pen(Color.Black), 10, 165, 243, 165); //4
g.DrawLine(new Pen(Color.Black), 10, 195, 341, 195); //5
g.DrawLine(new Pen(Color.Black), 10, 197, 341, 197); //5
g.DrawLine(new Pen(Color.Black), 10, 225, 341, 225); //6
g.DrawLine(new Pen(Color.Black), 10, 255, 341, 255); //7
g.DrawLine(new Pen(Color.Black), 10, 285, 341, 285); //8
g.DrawLine(new Pen(Color.Black), 10, 315, 341, 315); //9
g.DrawLine(new Pen(Color.Black), 10, 345, 341, 345); //10
//画竖线
g.DrawLine(new Pen(Color.Black), 80, 75, 80, 392); //11
g.DrawLine(new Pen(Color.Black), 82, 75, 82, 392); //12
g.DrawLine(new Pen(Color.Black), 210, 225, 210, 392); //13
g.DrawLine(new Pen(Color.Black), 243, 75, 243, 195); //14
//画方框
g.DrawRectangle(new Pen(Color.Black), 10, 10, 331, 382);
g.DrawRectangle(new Pen(Color.Black), 7, 7, 338, 388);
//填充图像
System.Drawing.Image newimage = System.Drawing.Image.FromFile(Server.MapPath(@"~\image\308329547.gif"));
//图像定位
g.DrawImage(newimage, 248, 80, 89, 111);
//释放图像缓存
g.Dispose();
//定义产生图片格式
Response.ContentType = "image/Jpeg";
bt.Save(Response.OutputStream, ImageFormat.Jpeg);
//创建文件夹
string WenJanpath = Server.MapPath(@"~\zkz_image");
if (!Directory.Exists(WenJanpath))
{
Directory.CreateDirectory(WenJanpath);
}
//图片自动保存路径
bt.Save(Server.MapPath(@"~\zkz_image\杜润龙的准考证" + DateTime.Now.ToString("yyyy-MM-dd") + ".jpg"), ImageFormat.Jpeg);
//释放位图缓存
bt.Dispose();
string lujing = Server.MapPath(@"~\zkz_image\杜润龙的准考证" + DateTime.Now.ToString("yyyy-MM-dd") + ".jpg");
Image1.ImageUrl = lujing;
//创建画布
Bitmap bt = new Bitmap(353, 404, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bt);
//定义文字样式
Font fn1 = new Font("Tahoma", 10, FontStyle.Bold);
Font fn = new Font("Tahoma", 9, FontStyle.Bold);
//填充背景色
g.Clear(Color.LightBlue);
//填文字
g.DrawString("中国电子学会电子设计工程师认证考试", fn1, Brushes.Black, new PointF(46, 20));
g.DrawString("准 考 证", fn1, Brushes.Black, new PointF(140, 50));
g.DrawString("准考证号", fn, Brushes.Black, new PointF(14, 85));
g.DrawString("EDP200903060001", fn, Brushes.Black, new PointF(90, 85));
g.DrawString("考生姓名", fn, Brushes.Black, new PointF(14, 115));
g.DrawString("杜润龙", fn, Brushes.Black, new PointF(90, 115));
g.DrawString("考生性别", fn, Brushes.Black, new PointF(14, 145));
g.DrawString("男", fn, Brushes.Black, new PointF(90, 145));
g.DrawString("身份证号", fn, Brushes.Black, new PointF(14, 175));
g.DrawString("411024198806021230", fn, Brushes.Black, new PointF(90, 175));
g.DrawString("考试级别", fn, Brushes.Black, new PointF(14, 205));
g.DrawString("电子设计助理工程师", fn, Brushes.Black, new PointF(90, 205));
g.DrawString("考试科目", fn, Brushes.Black, new PointF(14, 235));
g.DrawString("综合知识", fn, Brushes.Black, new PointF(90, 235));
g.DrawString("实际操作", fn, Brushes.Black, new PointF(220, 235));
g.DrawString("考试时间", fn, Brushes.Black, new PointF(14, 265));
g.DrawString("09-05-25 PM 8:00", fn, Brushes.Black, new PointF(90, 265));
g.DrawString("09-05-25 PM 8:00", fn, Brushes.Black, new PointF(220, 265));
g.DrawString("学校名称", fn, Brushes.Black, new PointF(14, 295));
g.DrawString("北京工业大学", fn, Brushes.Black, new PointF(90, 295));
g.DrawString("北京师范大学", fn, Brushes.Black, new PointF(220, 295));
g.DrawString("考场编号", fn, Brushes.Black, new PointF(14, 323));
g.DrawString("第一考场", fn, Brushes.Black, new PointF(90, 323));
g.DrawString("第九考场", fn, Brushes.Black, new PointF(220, 323));
g.DrawString("详细地址", fn, Brushes.Black, new PointF(14, 353));
string dizhi = "北京市北京工业大学地址北京工业";
int j = 335; //初始坐标
int j2 = 335; //初始坐标
for (int z = 0; z < 17; z += 8) //循环次数
{
int k = z + 10; // 第一次为0时
if (k > dizhi.Length) //k是否大于总字数
{
k = dizhi.Length - k; // 若大于比如k=45 dizhi.length=40 k=40-45 k=-5
int i = k + 10; //i = -5+20 这样就不至于截取超出范围
g.DrawString(dizhi.Substring(z, i), fn, Brushes.Black, new PointF(90, j += 18));
break;
}
g.DrawString(dizhi.Substring(z, 8), fn, Brushes.Black, new PointF(90, j += 18));
}
string dizhi2 = "北京市北京师范大学地址北京师范";
for (int z2 = 0; z2 < 17; z2 += 8) //循环次数
{
int k2 = z2 + 10; // 第一次为0时
if (k2 > dizhi2.Length) //k是否大于总字数
{
k2 = dizhi2.Length - k2; // 若大于比如k=45 dizhi.length=40 k=40-45 k=-5
int i2 = k2 + 10; //i = -5+20 这样就不至于截取超出范围
g.DrawString(dizhi2.Substring(z2, i2), fn, Brushes.Black, new PointF(220, j2 += 18));
break;
}
g.DrawString(dizhi2.Substring(z2, 8), fn, Brushes.Black, new PointF(220, j2 += 18));
}
//产生杂点
for (int i = 0; i < 500; i++)
{
int x1 = random.Next(bt.Width - 20);
int y1 = random.Next(bt.Height - 20);
bt.SetPixel(x1, y1, Color.FromArgb(random.Next()));
}
//产生随机曲线
for (int i = 0; i < 50; i++)
{
int x1 = random.Next(bt.Width - 20);
int y1 = random.Next(bt.Height - 20);
int x2 = random.Next(1, 30);
int y2 = random.Next(1, 20);
int x3 = random.Next(15, 45);
int y3 = random.Next(70, 270);
g.DrawArc(new Pen(Color.FromArgb(random.Next())), x1, y1, x2, y2, x3, y3);
}
//画横线
g.DrawLine(new Pen(Color.Black), 10, 75, 341, 75); //1
g.DrawLine(new Pen(Color.Black), 10, 105, 243, 105); //2
g.DrawLine(new Pen(Color.Black), 10, 135, 243, 135); //3
g.DrawLine(new Pen(Color.Black), 10, 165, 243, 165); //4
g.DrawLine(new Pen(Color.Black), 10, 195, 341, 195); //5
g.DrawLine(new Pen(Color.Black), 10, 197, 341, 197); //5
g.DrawLine(new Pen(Color.Black), 10, 225, 341, 225); //6
g.DrawLine(new Pen(Color.Black), 10, 255, 341, 255); //7
g.DrawLine(new Pen(Color.Black), 10, 285, 341, 285); //8
g.DrawLine(new Pen(Color.Black), 10, 315, 341, 315); //9
g.DrawLine(new Pen(Color.Black), 10, 345, 341, 345); //10
//画竖线
g.DrawLine(new Pen(Color.Black), 80, 75, 80, 392); //11
g.DrawLine(new Pen(Color.Black), 82, 75, 82, 392); //12
g.DrawLine(new Pen(Color.Black), 210, 225, 210, 392); //13
g.DrawLine(new Pen(Color.Black), 243, 75, 243, 195); //14
//画方框
g.DrawRectangle(new Pen(Color.Black), 10, 10, 331, 382);
g.DrawRectangle(new Pen(Color.Black), 7, 7, 338, 388);
//填充图像
System.Drawing.Image newimage = System.Drawing.Image.FromFile(Server.MapPath(@"~\image\308329547.gif"));
//图像定位
g.DrawImage(newimage, 248, 80, 89, 111);
//释放图像缓存
g.Dispose();
//定义产生图片格式
Response.ContentType = "image/Jpeg";
bt.Save(Response.OutputStream, ImageFormat.Jpeg);
//创建文件夹
string WenJanpath = Server.MapPath(@"~\zkz_image");
if (!Directory.Exists(WenJanpath))
{
Directory.CreateDirectory(WenJanpath);
}
//图片自动保存路径
bt.Save(Server.MapPath(@"~\zkz_image\杜润龙的准考证" + DateTime.Now.ToString("yyyy-MM-dd") + ".jpg"), ImageFormat.Jpeg);
//释放位图缓存
bt.Dispose();
string lujing = Server.MapPath(@"~\zkz_image\杜润龙的准考证" + DateTime.Now.ToString("yyyy-MM-dd") + ".jpg");
Image1.ImageUrl = lujing;