根据客户端输入的文字生成图片,再传回给客户端的webservice

80酷酷网    80kuku.com

  web|客户端view-textgraphic.asmx
<% WebService Language="vb" Class="TextGraphic" %>  
2  
3 Imports System  
4 Imports System.Web.Services  
5 Imports System.IO  
6 Imports System.Drawing  
7 Imports System.Drawing.Imaging  
8 Imports System.Drawing.Drawing2D  
9  
10  
11 <WebService(Namespace:="http://www.aspalliance.com/chrisg/services")> _  
12 Public Class TextGraphic  
13  
14  
15 <WebMethod> Public Function DrawText( _  
16 text as String, _  
17 fontName as String, _  
18 fontSize as Integer, _  
19 fontColor as String, _  
20 alignment as String, _  
21 backcolor as String, _  
22 width as Integer, _  
23 height as Integer _  
24 ) as Byte()  
25  
26 ' create output stream  
27 dim outStream as System.IO.MemoryStream  
28 outStream = New System.IO.MemoryStream()  
29  
30  
31 ' create a New bitmap image  
32 dim b as Bitmap = New Bitmap(width, height, pixelformat.format24bpprgb)  
33 dim g as Graphics  
34 g = Graphics.FromImage(b)  
35 dim fBrush as SolidBrush  
36 fBrush = New SolidBrush(ColorTranslator.FromHTML(fontColor))  
37  
38 dim salign as New StringFormat  
39 dim coords as pointF  
40  
41 if alignment.ToUpper = "LEFT" then  
42 salign.alignment = stringalignment.near  
43 coords = New pointF(0,0)  
44 elseif alignment.ToUpper = "RIGHT" then  
45 salign.alignment = stringalignment.far  
46 coords = New pointF(0,0)  
47 else  
48 salign.alignment = stringalignment.center  
49 coords = New pointF(width/2,0)  
50 end if  
51  
52 ' blank the bitmap  
53 g.Clear(ColorTranslator.FromHTML(backcolor))  
54  
55 ' antialias  
56 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias  
57 g.smoothingMode = smoothingMode.antiAlias  
58  
59 g.drawString(text, New font(fontName,fontSize),fBrush,coords,salign)  
60  
61  
62 ' serve the image to the client  
63 b.Save(outStream, ImageFormat.Gif)  
64  
65 return outStream.ToArray()  
66  
67 ' clear up  
68 b.Dispose()  
69 g.Dispose()  
70  
71  
72  
73 End Function  
74  
75 End Class  
testtextgraphic.aspx
<% Page Language="vb" Debug="True" %>  
2 <% import namespace="System.Drawing" %>  
3 <% import namespace="System.IO" %>  
4 <%  
5  
6 ' initialise objects  
7 dim gText as New TextGraphic  
8 dim b as Bitmap  
9 dim bytes as Byte()  
10 dim inStream as System.IO.MemoryStream  
11  
12 ' get image data  
13 bytes = gText.DrawText("test2","tahoma",36, "#0000ff", "center", "#ffffff", 100,100)  
14 inStream = New System.IO.MemoryStream(bytes)  
15  
16 ' create a New image from stream  
17 b = New Bitmap(inStream)  
18  
19 ' Set the content type  
20 response.contenttype="image/gif"  
21  
22 ' send the image to the viewer  
23 b.save(response.outputstream, b.rawformat)  
24  
25 ' tidy up  
26 b.dispose()  
27  
28 %>  

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: