将文本文件的内容或者文字保存成图片的方法分享
更新时间:2014年02月07日 15:54:47 作者:
这篇文章主要介绍了将文本文件的内容或者文字保存成图片的方法,需要的朋友可以参考下
调用方法:
复制代码 代码如下:
ConvertTextFileToImage(Server.MapPath("~/Log.txt"),Server.MapPath("~/Log.png"));
实现代码:
复制代码 代码如下:
void ConvertTextFileToImage(String textFile,String imageFile)
{
System.Drawing.Font drawFont = new System.Drawing.Font("宋体", 12);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(1, 1);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
String text = System.IO.File.ReadAllText(textFile, Encoding.GetEncoding("GB2312"));
System.Drawing.SizeF sf = g.MeasureString(text, drawFont, 1024); //设置一个显示的宽度
image = new System.Drawing.Bitmap(image, new System.Drawing.Size(Convert.ToInt32(sf.Width), Convert.ToInt32(sf.Height)));
g = System.Drawing.Graphics.FromImage(image);
g.Clear(System.Drawing.Color.White);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.DrawString(text, drawFont, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(new System.Drawing.PointF(0, 0), sf));
image.Save(imageFile, System.Drawing.Imaging.ImageFormat.Png);
g.Dispose();
image.Dispose();
}
相关文章
asp.net结合aspnetpager使用SQL2005的存储过程分页
项目中用到了,同事阿春写了例子,并在实际项目中使用了,记录下。感谢春哥的无私奉献。2009-07-07Asp.net FileUpload+Image制作头像效果示例代码
个人信息中通常需要自己的头像或者照片,今天主要介绍一下使用FileUpload+img控件上传照片,感兴趣的朋友可以参考下2013-08-08AjaxControlToolKit 显示浏览者本地语言的方法
使用最新版的AjaxControlToolKit控件2008-12-12asp.net BasePage类+Session通用用户登录权限控制
判断用户是否登录,常用的方法就是通过Session来控制。2010-05-05
最新评论