BBS多变个性贴图的实现,用ASP.NET网页动态生成图片
2004-06-24 16:35
916 查看
作者:阿赖 (Email: A at Lai.com.cn 主页:http://www.9499.net Blog: http://blog.csdn.net/laily/ )
关键字:Asp.NET贴图程序 VB.NET编程
摘要:使用ASP.NET动态生成用于BBS贴图的动态图片。即ASPX页面通过改变querrystring参数的输出不同图片的示例程序。
某日在论坛上看到别人发言用的是搞笑的图片,发言的文字就在图片中,看了一下图片的地址原来是动态生成的,可以通过改地址中的一些参数就可以生成另外的图片,显示不同的文字。如这个地址:
http://gzx.zcat.net/isay.ashx?fontsize=24&index=02&what=HelloWorld
将显示下面的图片:
现在你试着修改一下fontsize、index、what这些参数的值看看,这样的图片用在论坛中贴图挺用趣的吧:)
这个动态生成图片的程序是如何实现的呢?经过一番研究,我发现使用.NET提供的强大的绘图功能实现这样的程序真是小菜一碟(a piece of cake)。
STEP:
建个ASP.NET WEB项目,将一些模板的图片(如果你象我这样懒,不想自己做图片那就直接用上面的吧,what参数为空,index分别等于1,2……,生成的图片保存下来就行了)文件名为1.jpg,2.jpg,3.jpg……,存在template目录下。
建个WEB FORM,页面上不必添加任何东东,Page_onload事件中写如下代码就搞定了:
Dim Size, Index, sColor, sFont, say As String
Request.ContentEncoding = System.Text.Encoding.Unicode
Size = Request.QueryString("Size")
Index = Request.QueryString("index")
sColor = Request.QueryString("color")
sFont = Request.QueryString("font")
say = Request.QueryString("say")
Response.Clear()
Response.ContentType = "image/jpeg"
Response.Charset = "gb-2312"
If say = "" Then
Size = 8
Index = 999
say = "地址后加参数:?index=&say=&font=&color=&size="
say &= "index:1-9 say:显示的文字 font:字体 color:颜色(如:red) size:文字大小"
End If
If Size = "" Or Not Microsoft.VisualBasic.IsNumeric(Size) Then Size = 12
If Index = "" Then Index = "1"
Dim oFont As Font
Try
ofont = New Font(sFont, Single.Parse(Size))
Catch
oFont = New Font(FontFamily.GenericSerif, 12)
End Try
Dim strFilePath As String = Server.MapPath("template" & Index & ".jpg")
If Not IO.File.Exists(strFilePath) Then
Index = "1"
strFilePath = Server.MapPath("template" & Index & ".jpg")
End If
Dim oBmp As Bitmap
If IO.File.Exists(strFilePath) Then
oBmp = New Bitmap(strFilePath)
Else
oBmp = New Bitmap(200, 100, PixelFormat.Format24bppRgb)
End If
Dim fColor, defColor As Brush
defColor = Brushes.Black
Dim x As Single = 10
Dim y As Single = 10
Dim w As Single = 50
Dim h As Single = 50
Select Case Index
Case "1"
defColor = Brushes.White
w = 280 : h = 70
Case "2"
defColor = Brushes.Red
x = 0 : y = 0
w = 175 : h = 80
Case "3"
defColor = Brushes.Blue
x = 15 : y = 310
w = 530 : h = 115
Case "4"
defColor = Brushes.Black
x = 155 : y = 5
w = 435 : h = 140
Case "5"
defColor = Brushes.Red
x = 5 : y = 140
w = 305 : h = 90
Case "6"
defColor = Brushes.Beige
x = 155 : y = 5
w = 435 : h = 140
Case "7"
defColor = Brushes.Gray
x = 0 : y = 0
w = 175 : h = 80
Case "8"
defColor = Brushes.Black
x = 155 : y = 5
w = 435 : h = 140
Case "9"
defColor = Brushes.DarkBlue
x = 155 : y = 5
w = 435 : h = 140
End Select
Select Case LCase(sColor)
Case "white" : fColor = Brushes.White
Case "black" : fColor = Brushes.Black
Case "red" : fColor = Brushes.Red
Case "yellow" : fColor = Brushes.Yellow
Case "green" : fColor = Brushes.Green
Case "gray" : fColor = Brushes.Gray
Case "blue" : fColor = Brushes.Blue
Case "brown" : fColor = Brushes.Brown
Case "coral" : fColor = Brushes.Coral
Case "darkblue" : fColor = Brushes.DarkBlue
Case "gold" : fColor = Brushes.Gold
Case "orange" : fColor = Brushes.Orange
Case Else : fColor = defColor
End Select
Dim oGrap As Graphics = Graphics.FromImage(oBmp)
oGrap.DrawString(say, oFont, fColor, New RectangleF(x, y, w, h), StringFormat.GenericTypographic)
oBmp.Save(Response.OutputStream, ImageFormat.Jpeg)
oGrap.Dispose()
oBmp.Dispose()
Response.End()
使用方法跟前面提到了差不多,增加了字体和颜色两个参数,用法是:
程序页面地址?index=&say=&font=&color=&size="
注意x,y,w,h是图片文字框的位置和大小,请根据相应的图片作相应的设置。其实更好的办法是,将这图片的相关参数放到Web.config中,将会使程序更灵活和方便布署。
关键字:Asp.NET贴图程序 VB.NET编程
摘要:使用ASP.NET动态生成用于BBS贴图的动态图片。即ASPX页面通过改变querrystring参数的输出不同图片的示例程序。
某日在论坛上看到别人发言用的是搞笑的图片,发言的文字就在图片中,看了一下图片的地址原来是动态生成的,可以通过改地址中的一些参数就可以生成另外的图片,显示不同的文字。如这个地址:
http://gzx.zcat.net/isay.ashx?fontsize=24&index=02&what=HelloWorld
将显示下面的图片:
现在你试着修改一下fontsize、index、what这些参数的值看看,这样的图片用在论坛中贴图挺用趣的吧:)
这个动态生成图片的程序是如何实现的呢?经过一番研究,我发现使用.NET提供的强大的绘图功能实现这样的程序真是小菜一碟(a piece of cake)。
STEP:
建个ASP.NET WEB项目,将一些模板的图片(如果你象我这样懒,不想自己做图片那就直接用上面的吧,what参数为空,index分别等于1,2……,生成的图片保存下来就行了)文件名为1.jpg,2.jpg,3.jpg……,存在template目录下。
建个WEB FORM,页面上不必添加任何东东,Page_onload事件中写如下代码就搞定了:
Dim Size, Index, sColor, sFont, say As String
Request.ContentEncoding = System.Text.Encoding.Unicode
Size = Request.QueryString("Size")
Index = Request.QueryString("index")
sColor = Request.QueryString("color")
sFont = Request.QueryString("font")
say = Request.QueryString("say")
Response.Clear()
Response.ContentType = "image/jpeg"
Response.Charset = "gb-2312"
If say = "" Then
Size = 8
Index = 999
say = "地址后加参数:?index=&say=&font=&color=&size="
say &= "index:1-9 say:显示的文字 font:字体 color:颜色(如:red) size:文字大小"
End If
If Size = "" Or Not Microsoft.VisualBasic.IsNumeric(Size) Then Size = 12
If Index = "" Then Index = "1"
Dim oFont As Font
Try
ofont = New Font(sFont, Single.Parse(Size))
Catch
oFont = New Font(FontFamily.GenericSerif, 12)
End Try
Dim strFilePath As String = Server.MapPath("template" & Index & ".jpg")
If Not IO.File.Exists(strFilePath) Then
Index = "1"
strFilePath = Server.MapPath("template" & Index & ".jpg")
End If
Dim oBmp As Bitmap
If IO.File.Exists(strFilePath) Then
oBmp = New Bitmap(strFilePath)
Else
oBmp = New Bitmap(200, 100, PixelFormat.Format24bppRgb)
End If
Dim fColor, defColor As Brush
defColor = Brushes.Black
Dim x As Single = 10
Dim y As Single = 10
Dim w As Single = 50
Dim h As Single = 50
Select Case Index
Case "1"
defColor = Brushes.White
w = 280 : h = 70
Case "2"
defColor = Brushes.Red
x = 0 : y = 0
w = 175 : h = 80
Case "3"
defColor = Brushes.Blue
x = 15 : y = 310
w = 530 : h = 115
Case "4"
defColor = Brushes.Black
x = 155 : y = 5
w = 435 : h = 140
Case "5"
defColor = Brushes.Red
x = 5 : y = 140
w = 305 : h = 90
Case "6"
defColor = Brushes.Beige
x = 155 : y = 5
w = 435 : h = 140
Case "7"
defColor = Brushes.Gray
x = 0 : y = 0
w = 175 : h = 80
Case "8"
defColor = Brushes.Black
x = 155 : y = 5
w = 435 : h = 140
Case "9"
defColor = Brushes.DarkBlue
x = 155 : y = 5
w = 435 : h = 140
End Select
Select Case LCase(sColor)
Case "white" : fColor = Brushes.White
Case "black" : fColor = Brushes.Black
Case "red" : fColor = Brushes.Red
Case "yellow" : fColor = Brushes.Yellow
Case "green" : fColor = Brushes.Green
Case "gray" : fColor = Brushes.Gray
Case "blue" : fColor = Brushes.Blue
Case "brown" : fColor = Brushes.Brown
Case "coral" : fColor = Brushes.Coral
Case "darkblue" : fColor = Brushes.DarkBlue
Case "gold" : fColor = Brushes.Gold
Case "orange" : fColor = Brushes.Orange
Case Else : fColor = defColor
End Select
Dim oGrap As Graphics = Graphics.FromImage(oBmp)
oGrap.DrawString(say, oFont, fColor, New RectangleF(x, y, w, h), StringFormat.GenericTypographic)
oBmp.Save(Response.OutputStream, ImageFormat.Jpeg)
oGrap.Dispose()
oBmp.Dispose()
Response.End()
使用方法跟前面提到了差不多,增加了字体和颜色两个参数,用法是:
程序页面地址?index=&say=&font=&color=&size="
注意x,y,w,h是图片文字框的位置和大小,请根据相应的图片作相应的设置。其实更好的办法是,将这图片的相关参数放到Web.config中,将会使程序更灵活和方便布署。
相关文章推荐
- 在 ASP.NET 网页中显示动态生成的图片
- asp.net 实现输入网址生成当前网页的图片
- 动态生成PNG格式的图片直接嵌在ASP.NET网页中
- 使用ASP.NET动态生成图片
- 使用ASP.NET动态生成图片
- Asp.Net完整实现网页保存为图片代码
- asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现
- [ASP.NET] HyperLink + Image 实现动态图片链接
- asp.net 网页动态查询条件的实现
- Asp.Net中动态实现图片水印
- asp.net 网页动态查询条件的实现
- ASP.NET动态生成图片并下载
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- asp.net下用Aspose.Words for .NET动态生成word文档中的图片或水印的方法(转)
- asp.net通过实现IHttpHandler接口方法给图片添加水印图片(动态添加,不破坏原图片,但是耗资源)
- Asp.net生成静态网页的实现代码
- 在ASP.NET中实现上传图片,并按比例生成缩略图
- ASP.NET MVC 使用总结(二)——扩展HtmlHelper实现动态生成title及meta
- ASP.NET(C#)实现一次性动态上传多张图片(多个文件)