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>
public static void ConvertDescriptionText(string imageName, string imageText, string fontName, int fontSize, string saveLocation)
    {

        try
        {
            string[] txt = imageText.Split(' ');

            int i = 0;

            foreach (string s in txt)
            {

                Bitmap objBmpImage = new Bitmap(1, 1);

                int intWidth = 0;
                int intHeight = 0;

                Font objFont = new Font(fontName, fontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
                Graphics objGraphics = Graphics.FromImage(objBmpImage);

                intWidth = (int)objGraphics.MeasureString(s, objFont).Width;
                intHeight = (int)objGraphics.MeasureString(s, objFont).Height;

                objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
                objGraphics = Graphics.FromImage(objBmpImage);
                objGraphics.Clear(Color.Transparent);
                objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
                objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                objGraphics.DrawString(s, objFont, new SolidBrush(Color.FromArgb(00, 00, 00)), 0, 0);
                //objGraphics.DrawString(imageText, objFont, Brushes.Black, 0, 0);
                objGraphics.Flush();
                objBmpImage.Save(saveLocation + imageName + "_" + i + ".gif");

                i++;
            }

        }
        catch (Exception ex)
        {

        }
    }

Will graphics object work for you

You can use the graphics objects and see the below code snippet

Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
// Draw an image 
Image curImage = Image.FromFile(@"d:\x.jpg");
g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height);
// Create pens and a rectangle
Rectangle rect = new Rectangle(220, 30, 100, 50); 
Pen opqPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
Pen transPen = new Pen(Color.FromArgb(128, 255, 255, 255), 10);
Pen totTransPen = new Pen(Color.FromArgb(40, 0, 255, 0), 10); 
// Draw lines, rectangle, ellipse and string
g.DrawLine(opqPen, 10, 10, 200, 10);
g.DrawLine(transPen, 10, 30, 200, 30);
g.DrawLine(totTransPen, 10, 50, 200, 50);
g.FillRectangle(new SolidBrush(Color.FromArgb(40, 0, 0, 255)), rect);
rect.Y += 60;
g.FillEllipse(new SolidBrush(Color.FromArgb(50, 255, 255, 255)), rect);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(90, 255, 255, 50));
g.DrawString("Some Photo \nDate: 04/09/2001", 
new Font("Verdana", 14), semiTransBrush,
new RectangleF(20, 100, 300, 100) );
// Dispose
g.Dispose();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.