Convert text to image

I got code to convert text to image using C #. The code is below. My question now is that this function returns a bitmap. How to show this on my asp.net page. I want to show an image that is returned by this function.

private Bitmap CreateBitmapImage(string sImageText)
{
    Bitmap objBmpImage = new Bitmap(1, 1);

    int intWidth = 0;
    int intHeight = 0;

    // Create the Font object for the image text drawing.
    Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);

    // Create a graphics object to measure the text width and height.
    Graphics objGraphics = Graphics.FromImage(objBmpImage);

    // This is where the bitmap size is determined.
    intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
    intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;

    // Create the bmpImage again with the correct size for the text and font.
    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));

    // Add the colors to the new bitmap.
    objGraphics = Graphics.FromImage(objBmpImage);

    // Set Background color
    objGraphics.Clear(Color.White);
    objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
    objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
    objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
    objGraphics.Flush();
    return (objBmpImage);
}  
+3
source share
4 answers

there is an example of this with the following function

public Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
    {
        Bitmap bmp = new Bitmap(width, Height);
        using (Graphics graphics = Graphics.FromImage(bmp))
        {

            Font font = new Font(fontname, fontsize);
            graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
            graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
            graphics.Flush();
            font.Dispose();
            graphics.Dispose();


        }
        return bmp;
    }

and use this function:

ConvertTextToImage(txtvalue.Text, "Bookman Old Style", 10, Color.Yellow, Color.Red, txtvalue.Width, txtvalue.Height);
+2
source

Create HttpHandler(for example image.ashx) and use something like this code:

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/png";
        var image = CreateBitmapImage("Hello world");
        var ms = new MemoryStream();
        image.Save(ms, ImageFormat.Png);

        context.Response.BinaryWrite(ms.ToArray());
    }

And where you need your image to add a link.

<img src="image.ashx" alt="image" />

Updated [based on comments]:

Http handler:

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/png";

        var text = context.Request.Params["text"];
        if (text == null) text = string.Empty;
        var image = CreateBitmapImage(text);

        image.Save(context.Response.OutputStream, ImageFormat.Png);
    }

page layout: -

    <asp:TextBox runat="server" ID="MyTextBox"></asp:TextBox>
    <asp:Button runat="server" OnClick="RenderImageButtonClicked" Text="Change text"/>
    <asp:Image runat="server" ID="MyTextImage" />

Page event: -

    protected void RenderImageButtonClicked(object sender, EventArgs e)
    {
        MyTextImage.ImageUrl = "CreateImageText.ashx?text=" + HttpContext.Current.Server.UrlEncode(MyTextBox.Text);
    }
+2
source

, IMG HTML. Image-control ASP.NET. IMG src. - - , . HTML , IMG , src.
- , . (, Page_Load) - Bitmap, :

var bm = CreateBitmapImage("MyText");
bm.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.ContentType = "image/jpeg";
Response.Flush();
Response.End();

, CreateBitmapImage codebehind - , . , ( ).

IMG , . , , IMG :

<img src="dynamicimage.aspx?text=Mytext" />

, @Anton . .

0
string text = txtText.Text.Trim();
Bitmap bitmap = new Bitmap(1, 1);
Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(text, font).Width;
int height = (int)graphics.MeasureString(text, font).Height;
bitmap = new Bitmap(bitmap, new Size(width, height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
graphics.Flush();
graphics.Dispose();
string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
bitmap.Save(Server.MapPath("~/images/") + fileName, ImageFormat.Jpeg);
imgText.ImageUrl = "~/images/" + fileName;
imgText.Visible = true;
0

All Articles