All pastes #18231 Raw Edit

Anonymous

public text v1 · immutable
#18231 ·published 2005-07-21 15:53 UTC
rendered paste body
using System;

namespace TextToTiffLibrary {
	public class ConvertTextToTiff {
		public ConvertTextToTiff() {
		}

		public static bool SaveTextToTiff(string Text, string TiffFile, string FontFamily, int FontSize, System.Drawing.Brush FontBrush, System.Drawing.Brush BackgroundBrush) {
			byte[] data = ConvertToArray(Text, FontFamily, FontSize,FontBrush,BackgroundBrush);
			SaveByteArrayToDisk(data, TiffFile);
			return true;
		}
		public static bool SaveByteArrayToDisk(byte[] Data, string Path) {
			System.IO.FileStream fs = new System.IO.FileStream(Path, System.IO.FileMode.Create);
			fs.Write(Data, 0, Data.Length);
			fs.Close();
			return true;
		}
		public static bool SaveStringBuilderToTiff(System.Text.StringBuilder Text, string TiffFile, string FontFamily, int FontSize, System.Drawing.Brush FontBrush, System.Drawing.Brush BackgroundBrush) {
			byte[] data = ConvertToArray(Text.ToString(), FontFamily, FontSize,FontBrush,BackgroundBrush);
			SaveByteArrayToDisk(data, TiffFile);
			return true;
		}
		public static byte[] ConvertStringBuilderToByteArray(System.Text.StringBuilder Text, string FontFamily, int FontSize, System.Drawing.Brush FontBrush, System.Drawing.Brush BackgroundBrush) {
			return ConvertToArray(Text.ToString(), FontFamily, FontSize,FontBrush,BackgroundBrush);
		}
		public static bool ConvertTextFileToTiffFile(string TextFile, string TiffFile) {
			System.IO.StreamReader sr = new System.IO.StreamReader(TextFile, System.Text.Encoding.Default);
			string t = sr.ReadToEnd();
			byte[] data = ConvertToArray(t, "Arial", 10, System.Drawing.Brushes.Black, System.Drawing.Brushes.White);
			SaveByteArrayToDisk(data, TiffFile);
			return true;
		}
		public static byte[] ConvertToArray(string Text, string FontFamily, int FontSize, System.Drawing.Brush FontBrush, System.Drawing.Brush BackgroundBrush) {

			byte[] imagearray=null;
			//calculate the width of the image, by the longest line
			string longestLine="";		
			string[] lines = System.Text.RegularExpressions.Regex.Split(Text, System.Environment.NewLine);

			foreach(string line in lines) {
				if(line.Length>longestLine.Length) longestLine=line;
			}
			System.Drawing.Font font = new System.Drawing.Font("Arial", 12);
			System.Drawing.Brush brush = System.Drawing.Brushes.Black;
			
			System.Drawing.Bitmap b = new System.Drawing.Bitmap(1,1);
			System.Drawing.Image i = System.Drawing.Image.FromHbitmap(b.GetHbitmap());
			System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(i);
			System.Drawing.SizeF size = g.MeasureString(longestLine,font);

			int textbuffer = 5;

			b = new System.Drawing.Bitmap((int)size.Width, (int) ((size.Height*lines.Length) + (lines.Length*textbuffer)));
			i = System.Drawing.Image.FromHbitmap(b.GetHbitmap());
			g = System.Drawing.Graphics.FromImage(i);
			int top = 0;
			g.FillRectangle(BackgroundBrush, 0, 0, b.Width, b.Height);

			foreach(string line in lines) {
				g.DrawString(line, font, brush, 0, top);
				top+=(int)size.Height+textbuffer;
			}
			System.IO.MemoryStream m = new System.IO.MemoryStream();
			i.Save(m, System.Drawing.Imaging.ImageFormat.Tiff);
			i.Dispose();
			b.Dispose();
			g.Dispose();
			if(m.CanSeek&&m.Position>0) m.Position=0;
			imagearray = new byte[(int)m.Length];
			m.Read(imagearray, 0, imagearray.Length);
			return imagearray;
		}
	}
}