引言
基于生成图片实现了一个手机号转图片的需求。 内容也很简单,直接用手机号生成一个png图片。就是为了背景透明以便其他地方调用。 有无锯齿主要依靠一句代码:g.textrenderinghint= textrenderinghint.antialias;
生成图片
1、有锯齿
2、无锯齿
生成方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
string color = "#ff6633" ; system.drawing.bitmap image = new system.drawing.bitmap(170, 35); graphics g = graphics.fromimage(image); try { g.textrenderinghint= textrenderinghint.antialias; //消除锯齿 //生成随机生成器 random random = new random(); //清空图片背景色 //g.clear(color.transparent); //画图片的背景噪音线 /*for (int i = 0; i < 2; 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.black), x1, y1, x2, y2); } */ system.drawing.colorconverter colconvert = new system.drawing.colorconverter(); color fontcolor =(system.drawing.color)colconvert.convertfromstring(color); font font = new system.drawing.font( "arial" , 18, system.drawing.fontstyle.bold); lineargradientbrush brush = new lineargradientbrush( new rectangle(0, 0, image.width, image.height), fontcolor, fontcolor,lineargradientmode.horizontal); g.drawstring(phone, font, brush, 2, 2); //画图片的前景噪音点 //for (int i = 0; i < 50; i++) //{ // int x = random.next(image.width); // int y = random.next(image.height); // image.setpixel(x, y, color.fromargb(random.next())); //} //画图片的边框线 //g.drawrectangle(new pen(color.white), 0, 0, image.width - 1, image.height - 1); system.io.memorystream ms = new system.io.memorystream(); color backcolor = image.getpixel(1, 1); image.maketransparent(backcolor); image.save(ms, system.drawing.imaging.imageformat.png); context.response.clearcontent(); context.response.contenttype = "image/x-png" ; context.response.binarywrite(ms.toarray()); } finally { g.dispose(); image.dispose(); } |
参考资料
http://www.blue1000.com/bkhtml/c17/2013-03/71115.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/sword-successful/archive/2016/09/18/5880866.html