BBS多变个性贴图的实现,用ASP.NET网页动态生成图片

作者:阿赖 (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中,将会使程序更灵活和方便布署。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值