Dynamic icon numbers .NET
It's my turn to give something back to this group. I wanted to have
100+ icons showing, each with a different coloured marker. Instead of
going the long way round and creating all png images and linking to
them I tried using .NET to draw the counter on each icon.
Add the following to a aspx page called Marker.aspx
** start VB.NET code **
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.IO
Public Class Marker
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ICount As Integer = CInt(Request.Params("i"))
Try
BuildImage(ICount)
Catch ex As Exception
'Response.Write(ex.ToString)
End Try
End Sub
Private Sub BuildImage(ByVal ICount As Integer)
Dim Imagepath As String
Dim stmMemory As New MemoryStream
Imagepath = "../images/markers/marker.png"
Dim bmp As New Bitmap(Server.MapPath(Imagepath))
'Create the graphic object
Dim g As Graphics = Graphics.FromImage(bmp)
'Smooth the resulting image
g.SmoothingMode = SmoothingMode.AntiAlias
'Set the image quality mode
g.CompositingQuality = CompositingQuality.HighQuality
'Draw the text onto the image
Dim x As Integer
Dim y As Integer
Dim FontSize As Integer
Select Case ICount
Case Is < 9
x = 5
y = 3
FontSize = 10
Case Is < 99
If ICount >= 10 Then
x = 1
y = 4
FontSize = 10
End If
Case Is > 99
x = 1
y = 4
FontSize = 8
End Select
g.DrawString(ICount, New Font("Tahoma", FontSize,
FontStyle.Bold), Brushes.Black, x, y)
g.Dispose()
Response.Clear()
Response.ContentType = "image/png"
bmp.Save(stmMemory, ImageFormat.Png)
bmp.Dispose()
stmMemory.WriteTo(Response.OutputStream)
Response.End()
End Sub
End Class
** end VB.NET code **
Then you call the page for each marker in JS:
** start JS code **
icon.image = "Marker.aspx?i=" + index;
** end JS code **
This means that you can assign each group of markers to one coloured
icon image and just dynamically write the counter onto the image and
serve it back.
Let me know if you can refine this.
Steve
0 Comments:
Yorum Gönder
<< Home