將文本文件的內(nèi)容或者文字保存成圖片的方法分享
更新時間:2014年02月07日 15:54:47 作者:
這篇文章主要介紹了將文本文件的內(nèi)容或者文字保存成圖片的方法,需要的朋友可以參考下
調用方法:
復制代碼 代碼如下:
ConvertTextFileToImage(Server.MapPath("~/Log.txt"),Server.MapPath("~/Log.png"));
實現(xiàn)代碼:
復制代碼 代碼如下:
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-12輕量級ORM框架Dapper應用之實現(xiàn)CURD操作
這篇文章介紹了使用Dapper實現(xiàn)CURD操作的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03asp.net BasePage類+Session通用用戶登錄權限控制
判斷用戶是否登錄,常用的方法就是通過Session來控制。2010-05-05