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 %>