C# 實現將文本txt產生圖片

來源:互聯網
上載者:User

有時候儲存文本,容易丟失,相對儲存到圖片上,不容易被修改.

今天在做xml匯入txt想到了,把產生txt的內容.導成一張圖片.

就說一下匯出圖片的方法

 1  void ConvertTextFileToImage(String textFile, String imageFile)
2 {
3 //設定畫布字型
4 System.Drawing.Font drawFont = new System.Drawing.Font("宋體", 12);
5 //執行個體一個畫布起始位置為1.1
6 System.Drawing.Bitmap image = new System.Drawing.Bitmap(1, 1);
7 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
8 //讀取常值內容 原理就跟浮水印字型一樣.o(∩_∩)o 哈哈....
9 String text = System.IO.File.ReadAllText(textFile, Encoding.GetEncoding("GB2312"));
10 System.Drawing.SizeF sf = g.MeasureString(text, drawFont, 1024); //設定一個顯示的寬度
11 image = new System.Drawing.Bitmap(image, new System.Drawing.Size(Convert.ToInt32(sf.Width), Convert.ToInt32(sf.Height)));
12 g = System.Drawing.Graphics.FromImage(image);
13 g.Clear(System.Drawing.Color.White);
14 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
15 g.DrawString(text, drawFont, System.Drawing.Brushes.Black, new System.Drawing.RectangleF(new System.Drawing.PointF(0, 0), sf));
16 image.Save(imageFile, System.Drawing.Imaging.ImageFormat.Png);
17 g.Dispose();
18 image.Dispose();
19 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.