I have the following code that converts text in querystring to an image. This is giving image with white background. Need help to get a transparent background.
<%@ Page Language="VB" ContentType="image/gif" Debug="true" Explicit="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
'-------------------------------------------
' CAPTURE + SANITIZE QUERYSTRING PARAMETERS
'-------------------------------------------
Dim qText As String
qText = Request.QueryString("text")
If qText = String.Empty Then
qText = "-"
End If
Dim qSize As Integer
Try
qSize = Request.QueryString("size")
Catch
qSize = 0
End Try
If qSize < 8 Then
qSize = 8
End If
Dim qColor As String
qColor = Request.QueryString("color")
If qColor = String.Empty OrElse Regex.IsMatch(qColor, "^[0-9A-F]{6}$", RegexOptions.IgnoreCase) = False Then
qColor = "000000"
End If
'-------------------------------------------
' CALL FUNCTION
'-------------------------------------------
RenderGraphic(qText, qSize, qColor)
End Sub
Private Sub RenderGraphic(ByVal pText As String, ByVal pSize As Integer, ByVal pColor As String)
'-------------------------------------------
' DECLARE VARIABLES + PRE-CALCULATE
'-------------------------------------------
Dim b As Bitmap = New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(b)
Dim f As Font = New Font("Arial", pSize)
Dim w As Integer = g.MeasureString(pText, f).Width
Dim h As Integer = g.MeasureString(pText, f).Height
'-------------------------------------------
' RENDER DRAWING
'-------------------------------------------
b = New Bitmap(w, h, PixelFormat.Format64bppArgb)
g = Graphics.FromImage(b)
'g.Clear(Color.FromArgb(&HFF, &HFF, &HFF, &HCC))
g.Clear(Color.FromArgb(&HFF, &HFF, &HFF, &HFF))
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
g.DrawString(pText, f, New SolidBrush(ColorTranslator.FromHtml("#" & pColor)), 0, 0)
g.Flush()
'-------------------------------------------
' SAVE IN MEMORY
'-------------------------------------------
Dim m As New MemoryStream
b.Save(m, ImageFormat.Gif)
'-------------------------------------------
' APPLY TRANSPARENCY HACK
'-------------------------------------------
Dim n As Byte()
n = m.ToArray()
n(787) = 254
'-------------------------------------------
' SEND TO BROWSER
'-------------------------------------------
Dim o As New BinaryWriter(Response.OutputStream)
o.Write(n)
o.Close()
End Sub
</script>