将文本转换为图像

将文本转换为图像

本文介绍了将文本转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#转换了文本的code为图像。的code为如下。
现在我quetion是,该函数返回一个位图图像。如何显示在我的asp.net页面。我想表明这是由该函数返回的图像。

 私人位图CreateBitmapImage(字符串sImageText)
{
    位图objBmpImage =新位图(1,1);    INT intWidth = 0;
    INT intHeight = 0;    //创建图像的文字绘制的字体对象。
    字体objFont =新的字体(宋体,20,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Pixel);    //创建一个图形对象要测量的文本的宽度和高度。
    图形objGraphics = Graphics.FromImage(objBmpImage);    //这是被确定的位图的大小。
    intWidth =(INT)objGraphics.MeasureString(sImageText,objFont).WIDTH;
    intHeight =(INT)objGraphics.MeasureString(sImageText,objFont).Height;    //与文本和字体大小正确再次创建bmpImage。
    objBmpImage =新位图(objBmpImage,新的大小(intWidth,intHeight));    //颜色添加到新位图。
    objGraphics = Graphics.FromImage(objBmpImage);    //设置背景颜色
    objGraphics.Clear(Color.White);
    objGraphics.Smoothi​​ngMode = Smoothi​​ngMode.AntiAlias​​;
    objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias​​;
    objGraphics.DrawString(sImageText,objFont,新SolidBrush(Color.FromArgb(102,102,102)),0,0);
    objGraphics.Flush();
    返回(objBmpImage);
}


解决方案

创建的HttpHandler (如 image.ashx ),并使用类似这样code:

 公共无效的ProcessRequest(HttpContext的背景下)
    {
        context.Response.ContentType =图像/ PNG
        VAR图像= CreateBitmapImage(世界,你好);
        VAR毫秒=新的MemoryStream();
        image.Save(MS,ImageFormat.Png);        context.Response.BinaryWrite(ms.ToArray());
    }

和,你需要有你的图像添加的链接。

 < IMG SRC =image.ashxALT =图片/>

更新[基于评论]:

HTTP处理程序:

 公共无效的ProcessRequest(HttpContext的背景下)
    {
        context.Response.ContentType =图像/ PNG        VAR文本= context.Request.Params [文字];
        如果(文字== NULL)文本=的String.Empty;
        VAR图像= CreateBitmapImage(文本);        image.Save(context.Response.OutputStream,ImageFormat.Png);
    }

页面布局: -

 < ASP:文本框=服务器ID =MyTextBox>< / ASP:文本框>
    < ASP:按钮=服务器的OnClick =RenderImageButtonClicked文本=更改文本/>
    < ASP:图像=服务器ID =MyTextImage/>

页面事件: -

 保护无效RenderImageButtonClicked(对象发件人,EventArgs的发送)
    {
        MyTextImage.ImageUrl =?CreateImageText.ashx文本=+ HttpContext.Current.Server.UrlEn code(MyTextBox.Text);
    }

I got a code of convert text into image using C#. The code is given below.Now my quetion is that this function return a bitmap image. How to show it in my asp.net page. I want show the image which 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's 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);
}
解决方案

Create HttpHandler (like 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 to have your image add the 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);
    }

这篇关于将文本转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:53
查看更多