Click here to Skip to main content
15,867,895 members
Articles / Database Development / SQL Server

Flexible and Plug-in-based .NET Application using Provider Pattern

,
Rate me:
Please Sign up or sign in to vote.
4.67/5 (26 votes)
1 Dec 200511 min read 111.4K   1.2K   161  
This paper demonstrates creating a flexible, extensible, plug-in-based .NET application (JOM - Smart Job Manager). JOM is an asynchronous job processing engine built using the MS Provider design pattern and the .NET technology available today.
// =====================================================================================
// Copyright � 2005 by . All rights are reserved.
// 
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
// 
// website , email shahed.khan@gmail.com
// =====================================================================================


using System;
using System.Collections.Specialized;
using System.Data.SqlClient;
using SmartJobManager.Providers;
using SmartJobManager;
using System.IO;
using System.Drawing;
using System.Drawing.Text;


namespace SmartJobManager {

	public class TextToImageJobManagerProvider : JobManagerProvider {

		public TextToImageJobManagerProvider  () 
		{
		}

		#region JobManager specific behaviors
		string OutputFilePath = @"c:\tmp\";
		public override bool DoJob(Job job) 
		{
			//DoJob			
			//Write  to file
			try
			{			
				foreach(JobDetail jobDetail in job.JobDetailCollection)
				{
					if (jobDetail.ElementDesc == "TextToWrite")
					GenerateImage(jobDetail.ElementData.ToString());				
				}
				
				return true;
			}
			catch{}
			return false;
		}

		
		#endregion

		#region Provider specific behaviors
		public override void Initialize(string name, NameValueCollection configValue) {

		}

		public override string Name {
			get {
				return null;
			}
		}
		#endregion

		#region Image Generation from Text
		//Code originally written by http://www.thecodebehind.com/code/dotnet/aspnet/creating-an-image-from-text-with-c-and.aspx
		//I modified it to fit my purpose
		protected void GenerateImage(string imageText)
		{
			// we initialise these here because it's needed to calculate the width
			// and height of the provided text.  the 1,1 is only a temporary 
			// measurement
			Bitmap bmp = new Bitmap(1, 1);
			Graphics graphic = System.Drawing.Graphics.FromImage(bmp);

			// our font for the writing
			Font font = new Font("Arial", 14, FontStyle.Bold);

			// measure the text
			StringFormat stringformat = new StringFormat(StringFormat.GenericTypographic);
			int height = Convert.ToInt32(graphic.MeasureString(imageText, font, new PointF(0,0), stringformat).Height) + 6;
			int width = Convert.ToInt32(graphic.MeasureString(imageText, font, new PointF(0,0), stringformat).Width);

			// add some padding for the width - i think the kerning is throwing
			// it off by a little.  5% seems to work well, and the +10 is necessary
			// if the string is only a few letters
			width += Convert.ToInt32(0.05 * width) + 10;

			// recreate our bmp and graphic objects with the new measurements
			bmp = new Bitmap(width, height);

			graphic = System.Drawing.Graphics.FromImage(bmp);

			// our background colour
			graphic.Clear(Color.FromArgb(153, 204, 229));

			// aliasing mode
			graphic.TextRenderingHint = TextRenderingHint.SystemDefault;

			/* 
			the different aliasing modes:
			---------------------------------------------------------------------------------
			graphic.TextRenderingHint = TextRenderingHint.SystemDefault;
			- each character is drawn using its glyph bitmap, with the system default rendering hint

			graphic.TextRenderingHint = TextRenderingHint.AntiAlias;
			- each character is drawn using its antialiased glyph bitmap without hinting

			graphic.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
			- each character is drawn using its antialiased glyph bitmap with hinting

			graphic.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
			- each character is drawn using its glyph CT bitmap with hinting

			graphic.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
			- each character is drawn using its glyph bitmap

			graphic.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
			- each character is drawn using its glyph bitmap
			*/

			// our brush which is the writing colour
			SolidBrush brush = new SolidBrush(Color.White);

			/*
			alternate brushes
			-------------------------------------------------------------------------------
			System.Drawing.Drawing2D.HatchBrush
			System.Drawing.Drawing2D.LinearGradientBrush
			System.Drawing.Drawing2D.PathGradientBrush
			System.Drawing.SolidBrush
			System.Drawing.TextureBrush
			*/

			// create our graphic
			graphic.DrawString(imageText, font, brush, new Rectangle(0, 0, width, height));

			// Set the content type and return the image
			
			bmp.Save(string.Format("{0}{1}{2}{3}", OutputFilePath, "TransferImageJob",DateTime.Now.Ticks.ToString(), ".jpg"),System.Drawing.Imaging.ImageFormat.Jpeg);

			// dispose of our objects
			font.Dispose();
			stringformat.Dispose();
			graphic.Dispose();
			bmp.Dispose();

		}
		#endregion Image Generation from Text

	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
I have been awarded MVP (Visual C#) for year 2007, 2008, 2009. I am a Microsoft Certified Application Developer (C# .Net). I currently live in Melbourne, Australia. I am a co-founder and core developer of Pageflakes www.pageflakes.com and Founder of Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Simplexhub.
My BLOG http://www.geekswithblogs.net/shahed
http://msmvps.com/blogs/shahed/Default.aspx.

Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions