根據用戶端輸入的文字產生圖片,再傳回給用戶端的webservice

來源:互聯網
上載者:User
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 %>  


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。