0

My Code:

<asp:Image ID="myImage" ImageUrl="~/GetImage.ashx?id=Chiken" runat="server" /> 

Code Behind:

public Image DrawText(String text, Font font, Color textColor, Color backColor)
{
    //first, create a dummy bitmap just to get a graphics object
    Image img = new Bitmap(1, 1);
    Graphics drawing = Graphics.FromImage(img);
    //measure the string to see how big the image needs to be
    SizeF textSize = drawing.MeasureString(text, font);
    //free up the dummy image and old graphics object
    img.Dispose();
    drawing.Dispose();
    //create a new image of the right size
    img = new Bitmap((int)textSize.Width, 80);

    drawing = Graphics.FromImage(img);

    //paint the background
    drawing.Clear(backColor);

    //create a brush for the text
    Brush textBrush = new SolidBrush(textColor);

    drawing.DrawString(text, font, textBrush, 0, 0);

    drawing.Save();

    textBrush.Dispose();
    drawing.Dispose();

    return img;
}

And Call Back in ashx file

    public void ProcessRequest(HttpContext context)
    {
        Font font = new Font("Revalo Modern Regular", 44, FontStyle.Bold);
        Color color = Color.FromArgb(242, 139, 0);
        Color background = Color.FromArgb(255, 255, 255);
        Image img = DrawText(HttpContext.Current.Request.QueryString["id"].ToString(), font, color, background);
        img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }

Tried:

img.SaveAs(Server.MapPath("../Images/"+context.Response.OutputStream.ToString()))

I', tried How to generate an image from text on fly at runtime

Photo after bitmap creation

Photo after bitmap creation

HOÀNG LONG
  • 391
  • 2
  • 12

0 Answers0