Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Keep Your InkJet Print Head Clean

Rate me:
Please Sign up or sign in to vote.
4.91/5 (28 votes)
4 Jan 2009CPOL14 min read 92.7K   2.1K   53  
A utility that you can use to "exercise" your inkjet printer without wasting a lot of ink or paper
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace AutoPrint
{
	/// <summary>
	/// This form displays the help in HTML format. It's nothign fancy, but it gets the 
	/// job done, and even better, allows screenshots.
	/// </summary>
	public partial class FormHelp : Form
	{

		//--------------------------------------------------------------------------------
		/// <summary>
		/// Constructor
		/// </summary>
		public FormHelp()
		{
			InitializeComponent();
		}

		//--------------------------------------------------------------------------------
		/// <summary>
		/// Loads the html, makes adjustments to the img tags (fully qualifies the 
		/// filename - this is absol;utely necessary in the WebBrowser control), and 
		/// displays the help page.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void FormHelp_Load(object sender, EventArgs e)
		{
			// get our data file path (the parent form saved the help files to the program 
			// data folder (definitely necessary for Vista, and maybe for XP).
			string appDataFolder = System.IO.Path.GetDirectoryName(ProfileBitmap.GetDataFileName());

			bool valid = false;
			string htmlFile = System.IO.Path.Combine(appDataFolder, "AutoPrintHelp.html");
			StringBuilder htmlText = new StringBuilder("");

			// make sure the file exists before trying to load/modify it.
			if (File.Exists(htmlFile))
			{
				try
				{
					// read the file
					string rawHtml = "";
					using (StreamReader reader = new StreamReader(htmlFile))
					{
						rawHtml = reader.ReadToEnd();
					}

					// Replace the filenames specified in the image tags with fully 
					// qualified paths. The .Net  WebBrowser control will NOT display an 
					// image unless the filename is fully qualified.
					int start = -1;
					int stop = -1;
					do
					{
						// find the start of the next tag
					    start = rawHtml.IndexOf("<img ");
					    if (start >= 0)
					    {
							// while we're here, move everything BEFORE the start position 
							// into the stringbuilder.
							htmlText.Append(rawHtml.Substring(0, start));
							// find the end of the img tag
					        stop = rawHtml.IndexOf("\">", start);
							// extract it
					        string imageTag = rawHtml.Substring(start, stop+2-start);
							// replace the original file name with the fully qualified 
							// path/name
							string temp = imageTag;
							temp = temp.Replace("<img src=\"", "");
							temp = temp.Replace("\">", "");
							temp = System.IO.Path.Combine(appDataFolder, temp);
							imageTag = string.Format("<img src=\"{0}\">", temp);
							// append the newly constructed img tag to the string builder 
							// object
							htmlText.Append(imageTag);
							// shorten our raw text string 
							rawHtml = rawHtml.Substring(stop+2);
					    }
					}
					while (start >= 0);

					// add any remaining text from the raw html string to our string 
					// builder object
					htmlText.Append(rawHtml);

					// load up the WebBrowser control
					this.webBrowser1.Navigate("about:blank");
					HtmlDocument doc = this.webBrowser1.Document;
					doc.Write(string.Empty);
					this.webBrowser1.DocumentText = htmlText.ToString();

					// and we're out
					valid = true;
				}
				catch (Exception ex)
				{
					if (ex != null) {}
				}
			}
			// if we experienced a problem of some kind, display an error message.
			if (!valid)
			{
				MessageBox.Show("Could not find (and/or read) the help page file.", "Help File Error");
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions