您的位置:首页 > 编程语言 > ASP

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息