Create A Transparent Image

kleptos

Well-known member
Joined
Jul 16, 2003
Messages
47
Location
Boca Raton, FL
I create an image with an aspx page and i want it to be transparent. I am pretty much just making a small rectangle image with some text on it. But the background needs to be transparent so all you see is the text, no background color. Any ideas or tutorials? Thanks!

Code:
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>

<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    Bitmap bmp = new Bitmap(175, 60);
    Graphics g = Graphics.FromImage(bmp);

    Font f = new Font("Verdana", 9);
    SolidBrush b = new SolidBrush(Color.Black);

    float x = 5;
    float y = 5;

    string line1 = "My String Of Text";

    g.DrawString(line1, f, b, x, y);
 
    Response.ContentType = "image/gif";
    bmp.Save(Response.OutputStream, ImageFormat.Gif);
    Response.End();

    f.Dispose();
    b.Dispose();
    g.Dispose();
    bmp.Dispose();
}
</script>
 
Sure. Fill the entire background with some color that wouldnt
actually be used in the picture (I like to use lime green). Then
do all your painting and call the MakeTransparent() method of the
Bitmap, passing the color you filled the background with.
 
I have tried that but i guess that doesnt work with a aspx page? I am sorta using this aspx page as an image so to speak. When i implement the code you spoke of above,it turns the background black. This should work with a aspx page right?

Code:
<img src="image.aspx">

Code:
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>

<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    Bitmap bmp = new Bitmap(175, 60);
    Graphics g = Graphics.FromImage(bmp);

    Rectangle r = new Rectangle(0,0,175,60);
    Font f = new Font("Verdana", 9, FontStyle.Bold);
    SolidBrush b = new SolidBrush(txtColor);
    SolidBrush bg = new SolidBrush(Color.Brown);

    float x = 5;
    float y = 5;

    g.Clear(Color.Transparent);
    g.SmoothingMode = SmoothingMode.AntiAlias;

    string line1 = "String Goes Here";

    g.FillRectangle(bg, r);
    g.DrawString(line1, f, b, x, y);

    bmp.MakeTransparent(Color.Brown);

    Response.ContentType = "image/gif";
    bmp.Save(Response.OutputStream, ImageFormat.Gif);
    Response.End();

    f.Dispose();
    b.Dispose();
    g.Dispose();
    bmp.Dispose();
}
</script>
 
Back
Top