如何将.txt和.docx文件转换为图像文件
问题描述:
Hi, is there any way to convert whole .txt and .doc and .docx file to images file using c# for mvc web application? please help
答
看看这个解决方案: http://stackoverflow.com/questions/21675067/convert-text-to-image [ ^ ]
创建httphandler(如image.ashx)并使用类似这样的代码:
Take a look at this solution: http://stackoverflow.com/questions/21675067/convert-text-to-image[^]
Create httphandler (like image.ashx) and use something like this code:
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);
}