第五章 绘划和打字Chapter 5: Drawing & Typing
5.1 Canvas对象Canvas Object
AspJpeg 1.2以上版本提供了Canvas属性,它返回一个与图像交互的Canvas实例。Canvas实例提供了一系列方法,比如说PrintText和DrawCircle,它们能够让你编程式地在一张图片上写一些字符或者画一些几何图形。AspJpeg 1.2+ offers the Canvas property, which returns an instance of the Canvas object associated with the image. The Canvas object offers a variety of methods, such as PrintText and DrawCircle, that enable you to programmatically write text and draw geometric figures over an image.
许多绘划参数例如文字大小、字体、颜色、笔头大小、笔刷形状等等,都有能够在Canvas的二级对象Font、Pen和Brush中一一指定。Various drawing parameters such as text sizes, fonts, colors, pens, brushes, etc., are specified via Canvas's 2nd-level objects Font, Pen, and Brush.
以下代码片断阐明了这些顶级对象的用法。The following code snippet demonstrates the usage of this hierarchy of objects:
<%
Set Jpeg = Server.CreateObject("Persits.Jpeg")
Jpeg.Canvas.Font.Color = &HFF0000
Jpeg.Canvas.Font.Family = "Courier New"
Jpeg.Canvas.Pen.Color = &H000000
Jpeg.Canvas.Pen.Width = 2
Jpeg.Canvas.Brush.Solid = False
Jpeg.Canvas.PrintText 10, 10, "Some Text"
Jpeg.Canvas.DrawBar 1, 1, 100, 100
...
%>
5.2 绘制图形和文字Drawing Graphics and Text

下例代码样例在一张图片上左下角打印出了文字“Copyright (c) XYZ, Inc.”,用红色以及 Courier New字体(大小是默认的24号字),它还在图片四周画了一个2象素宽的黑色框。字体质量被设为4(抗锯齿),背景模式是不透明,从而使抗锯齿效果能够工作正常。The following code sample prints the text "Copyright (c) XYZ, Inc." on an image in red color and Courier New font (default size 24) in the lower-left corner. It also draws a 2-pixel-wide black frame around the image. The font quality is set to 4 (antialiased) and background mode to "Opaque" to make antialiasing work properly.
VB Script
<%
Set Jpeg = Server.CreateObject("Persits.Jpeg")
' Open source image
Jpeg.Open Server.MapPath("../images/dodge_viper.jpg")
' Resizing is optional. None in this code sample.
' Draw text
Jpeg.Canvas.Font.Color = &HFF0000 ' Red
Jpeg.Canvas.Font.Family = "Courier New"
Jpeg.Canvas.Font.Bold = True
Jpeg.Canvas.Font.Quality = 4 ' Antialiased
Jpeg.Canvas.Font.BkMode = "Opaque" ' to make antialiasing work
Jpeg.Canvas.Print 10, 572, "Copyright (c) XYZ, Inc."
' Draw frame: black, 2-pixel width
Jpeg.Canvas.Pen.Color = &H000000 ' Black
Jpeg.Canvas.Pen.Width = 2
Jpeg.Canvas.Brush.Solid = False ' or a solid bar would be drawn
Jpeg.Canvas.DrawBar 1, 1, Jpeg.Width, Jpeg.Height
Jpeg.Save Server.MapPath("dodge_viper_framed.jpg")
%>
C#
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
ASPJPEGLib.IASPJpeg objJpeg;
objJpeg = new ASPJPEGLib.ASPJpeg();
// Compute path to source image
String strPath = Server.MapPath("../images/dodge_viper.jpg");
// Open source image
objJpeg.Open( strPath );
// Resizing is optional. None in this code sample.
// Draw text
objJpeg.Canvas.Font.Color = 0xFF0000; // red
objJpeg.Canvas.Font.Family = "Courier New";
objJpeg.Canvas.Font.Bold = 1; // True
objJpeg.Canvas.Font.Quality = 4; // Antialiased
objJpeg.Canvas.Font.BkMode = "Opaque"; // For antialiasing work
objJpeg.Canvas.Print( 10, 572, "Copyright (c) XYZ, Inc.", Missing.Value );
// Draw frame: black, 2-pixel width
objJpeg.Canvas.Pen.Color = 0x000000; // Black
objJpeg.Canvas.Pen.Width = 2;
objJpeg.Canvas.Brush.Solid = 0; // false, to avoid solid bar
objJpeg.Canvas.DrawBar( 1, 1, objJpeg.Width, objJpeg.Height );
objJpeg.Save( Server.MapPath("dodge_viper_framed.jpg") );
FramedImage.Src = "dodge_viper_framed.jpg";
}
</script>
点击下例链接来运行这些样例代码:Click the links below to run this code sample:
http://localhost/aspjpeg/manual_05/05_canvas.asp
http://localhost/aspjpeg/manual_05/05_canvas.aspx
'File: Ip.asp
<!--#include file="conn.asp"-->
<!--#include file="inc/config.asp"-->
<%Response.ContentType = "image/gif"
ConnDatabase
Dim tempip,myipnumeber,sql,rs1
Dim country,city
tempip=ReqIP
tempip = Split(tempip,".")
if Ubound(tempip)=3 then
For i=0 To Ubound(tempip)
tempip(i)=left(tempip(i),3)
if isnumeric(tempip(i)) then
tempip(i)=cint(tempip(i))
else
tempip(i)=0
end if
next
myipnumeber=tempip(0)*256*256*256+tempip(1)*256*256+tempip(2)*256+tempip(3)
End If
sql="select country,city from DV_Address where IP1<="&myipnumeber&" and IP2>="&myipnumeber
set rs1=conn.execute(sql)
if not rs1.eof Then
country = rs1(0)
city = rs1(1)
Else
country = "51Tiao.Com"
city = ""
End If
rs1.close : Set rs1 = Nothing
CloseDatabase
Dim LocalFile,TargetFile
LocalFile = Server.MapPath("Ip.gif")
Dim Jpeg
Set Jpeg = Server.CreateObject("Persits.Jpeg")
If -2147221005=Err then
Response.write "没有这个组件,请安装!" '检查是否安装AspJpeg组件
Response.End()
End If
Jpeg.Open (LocalFile) '打开图片
If err.number then
Response.write"打开图片失败,请检查路径!"
Response.End()
End if
Dim aa
aa=Jpeg.Binary '将原始数据赋给aa
'=========加文字水印====http://www.devdao.com/=============
Jpeg.Canvas.Font.Color = &H000000 '水印文字颜色
Jpeg.Canvas.Font.Family = "宋体" '字体
Jpeg.Canvas.Font.Bold = False '是否加粗
Jpeg.Canvas.Font.Size = 12 '字体大小
Jpeg.Canvas.Font.ShadowColor = &Hffffff '阴影色彩
Jpeg.Canvas.Font.ShadowYOffset = 1
Jpeg.Canvas.Font.ShadowXOffset = 1
Jpeg.Canvas.Brush.Solid = False
Jpeg.Canvas.Font.Quality = 4 ' '输出质量
Jpeg.Canvas.PrintText 30,30,"-------------------------------------" '水印位置及文字
Jpeg.Canvas.PrintText 30,50," 你的IP: "& ReqIP
Jpeg.Canvas.PrintText 30,70," 你的位置: "&country&" "&city
Jpeg.Canvas.PrintText 30,90," 操作系统: "&ClientInfo(0)
Jpeg.Canvas.PrintText 30,110," 浏 览 器: "&RegExpFilter("Microsoft<sup>®</sup> ", ClientInfo(1), 0, "")
Jpeg.Canvas.PrintText 30,130,"-------------------------------------"
Jpeg.Canvas.PrintText 30,145,"个性签名来自风易在线 www.knowsky.com"
bb=Jpeg.Binary '将文字水印处理后的值赋给bb,这时,文字水印没有不透明度
'============调整文字透明度================
Set MyJpeg = Server.CreateObject("Persits.Jpeg")
MyJpeg.OpenBinary aa
Set Logo = Server.CreateObject("Persits.Jpeg")
Logo.OpenBinary bb
MyJpeg.DrawImage 0,0, Logo, 0.9 '0.3是透明度
cc=MyJpeg.Binary '将最终结果赋值给cc,这时也可以生成目标图片了
Response.BinaryWrite cc '将二进输出给浏览器
set aa=nothing
set bb=nothing
set cc=nothing
Jpeg.close : Set Jpeg = Nothing
MyJpeg.Close : Set MyJpeg = Nothing
Logo.Close : Set Logo = Nothing
%>
'--------------------------------------------------
'File: conn.asp
<%dim conn,dbpath,UserIP
sub ConnDatabase
On Error Resume next
set conn=server.createobject("adodb.connection")
DBPath = Server.MapPath("IP.MDB")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath
If Err Then
err.Clear
Set Conn = Nothing
Response.Write "数据库正在更新中,请稍后再试!"
Response.End
End If
End Sub
Sub CloseDatabase
Conn.close
Set Conn = Nothing
End Sub%>
'-------------------------------------------------
'File: config.asp
<%
Dim User_Agent
User_Agent = Request.ServerVariables("HTTP_USER_AGENT")
' ============================================
' 获取客户端配置
' ============================================
Public Function ClientInfo(sType)
If sType = 0 Then
If InStr(User_Agent, "Windows 98") Then
ClientInfo = "Windows 98"
ElseIf InStr(User_Agent, "Win 9x 4.90") Then
ClientInfo = "Windows ME"
ElseIf InStr(User_Agent, "Windows NT 5.0") Then
ClientInfo = "Windows 2000"
ElseIf InStr(User_Agent, "Windows NT 5.1") Then
ClientInfo = "Windows XP"
ElseIf InStr(User_Agent, "Windows NT 5.2") Then
ClientInfo = "Windows 2003"
ElseIf InStr(User_Agent, "Windows NT") Then
ClientInfo = "Windows NT"
ElseIf InStr(User_Agent, "unix") or InStr(User_Agent, "Linux") or InStr(User_Agent, "SunOS") or InStr(User_Agent, "BSD") Then
ClientInfo = "Unix & Linux"
Else
ClientInfo = "Other"
End If
ElseIf sType = 1 Then
If InStr(User_Agent, "MSIE 7") Then
ClientInfo = "Microsoft<sup>®</sup> Internet Explorer 7.0"
ElseIf InStr(User_Agent, "MSIE 6") Then
ClientInfo = "Microsoft<sup>®</sup> Internet Explorer 6.0"
ElseIf InStr(User_Agent, "MSIE 5") Then
ClientInfo = "Microsoft<sup>®</sup> Internet Explorer 5.0"
ElseIf InStr(User_Agent, "MSIE 4") Then
ClientInfo = "Microsoft<sup>®</sup> Internet Explorer 4.0"
ElseIf InStr(User_Agent, "Netscape") Then
ClientInfo = "Netscape<sup>®</sup>"
ElseIf InStr(User_Agent, "Opera") Then
ClientInfo = "Opera<sup>®</sup>"
Else
ClientInfo = "Other"
End If
End If
End Function
' ============================================
' 按照指定的正则表达式替换字符
' ============================================
Public Function RegExpFilter(Patrn, Str, sType, ReplaceWith)
Dim RegEx
Set RegEx = New RegExp
If sType = 1 Then
RegEx.Global = True
Else
RegEx.Global = False
End If
RegEx.Pattern = Patrn
RegEx.IgnoreCase = True
RegExpFilter = RegEx.Replace(Str, ReplaceWith)
End Function
Public Function ReqIP()
ReqIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If ReqIP = "" or IsNull(ReqIP) Then ReqIP = Request.ServerVariables("REMOTE_ADDR")
End Function
%>



上一篇
下一篇


文章来自:
Tags:
回复






刚才我还了解到其实jpeg.Canvas.font对象还有四个属性用来调整字体:size属性(字体大小)、ShadowColor属性(阴影色)、ShadowYOffset属性(阴影Y向偏移像素)、ShadowXOffset属性(阴影X向偏移像素)
另外,BkMode属性值不设置或者设置为"transparent"表示背景透明。也可以用类如&HFFFFFF这样的值表示背景色