Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for barcode.cs

(view source code of barcode.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Windows.Forms;
  8.  
  9.  
  10. namespace RobvanderWoude
  11. {
  12. 	class Barcode
  13. 	{
  14. 		static string progver = "1.00";
  15.  
  16.  
  17. 		static int Main( string[] args )
  18. 		{
  19. 			#region Set Defaults
  20.  
  21. 			string imgfile = String.Empty;
  22. 			string text = String.Empty;
  23. 			Color bgcolor = Color.White;
  24. 			Brush fgcolor = Brushes.Black;
  25. 			RotateFlipType rotation = RotateFlipType.RotateNoneFlipNone;
  26. 			ImageFormat format = ImageFormat.Jpeg;
  27. 			int degrees = 0;
  28. 			int fontsize = 48;
  29.  
  30. 			#endregion Set Defaults
  31.  
  32.  
  33. 			#region Parse Command Line
  34.  
  35. 			if ( args.Length < 2 )
  36. 			{
  37. 				return ShowHelp( );
  38. 			}
  39.  
  40. 			foreach ( string arg in args )
  41. 			{
  42. 				if ( arg[0] == '/' )
  43. 				{
  44. 					switch ( arg.ToLower( )[1] )
  45. 					{
  46. 						case 'b':
  47. 							if ( bgcolor != Color.White )
  48. 							{
  49. 								return ShowHelp( "Duplicate command line switch /B" );
  50. 							}
  51. 							if ( arg.Length < 6 )
  52. 							{
  53. 								return ShowHelp( "Invalid background color specified: \"{0}\"", arg );
  54. 							}
  55. 							switch ( arg.Substring( 3 ).ToLower( ) )
  56. 							{
  57. 								case "black":
  58. 									bgcolor = Color.Black;
  59. 									break;
  60. 								case "blue":
  61. 									bgcolor = Color.Blue;
  62. 									break;
  63. 								case "brown":
  64. 									bgcolor = Color.Brown;
  65. 									break;
  66. 								case "cyan":
  67. 									bgcolor = Color.Cyan;
  68. 									break;
  69. 								case "darkblue":
  70. 									bgcolor = Color.DarkBlue;
  71. 									break;
  72. 								case "darkcyan":
  73. 									bgcolor = Color.DarkCyan;
  74. 									break;
  75. 								case "darkgray":
  76. 								case "darkgrey":
  77. 									bgcolor = Color.DarkGray;
  78. 									break;
  79. 								case "darkgreen":
  80. 									bgcolor = Color.DarkGreen;
  81. 									break;
  82. 								case "darkmagenta":
  83. 									bgcolor = Color.DarkMagenta;
  84. 									break;
  85. 								case "darkorange":
  86. 									bgcolor = Color.DarkOrange;
  87. 									break;
  88. 								case "dark":
  89. 									bgcolor = Color.DarkRed;
  90. 									break;
  91. 								case "gold":
  92. 									bgcolor = Color.Gold;
  93. 									break;
  94. 								case "gray":
  95. 								case "grey":
  96. 									bgcolor = Color.Gray;
  97. 									break;
  98. 								case "green":
  99. 									bgcolor = Color.Green;
  100. 									break;
  101. 								case "lightblue":
  102. 									bgcolor = Color.LightBlue;
  103. 									break;
  104. 								case "lightcyan":
  105. 									bgcolor = Color.LightCyan;
  106. 									break;
  107. 								case "lightgray":
  108. 								case "lightgrey":
  109. 									bgcolor = Color.LightGray;
  110. 									break;
  111. 								case "lightgreen":
  112. 									bgcolor = Color.LightGreen;
  113. 									break;
  114. 								case "lightyellow":
  115. 									bgcolor = Color.LightYellow;
  116. 									break;
  117. 								case "magenta":
  118. 									bgcolor = Color.Magenta;
  119. 									break;
  120. 								case "orange":
  121. 									bgcolor = Color.Orange;
  122. 									break;
  123. 								case "pink":
  124. 									bgcolor = Color.Pink;
  125. 									break;
  126. 								case "red":
  127. 									bgcolor = Color.Red;
  128. 									break;
  129. 								case "silver":
  130. 									bgcolor = Color.Silver;
  131. 									break;
  132. 								case "white":
  133. 									bgcolor = Color.White;
  134. 									break;
  135. 								case "yellow":
  136. 									bgcolor = Color.Yellow;
  137. 									break;
  138. 								case "yellowgreen":
  139. 									bgcolor = Color.YellowGreen;
  140. 									break;
  141. 								default:
  142. 									return ShowHelp( "Invalid foreground color specified: \"{0}\"", arg );
  143. 							}
  144. 							break;
  145. 						case 'f':
  146. 							if ( fgcolor != Brushes.Black )
  147. 							{
  148. 								return ShowHelp( "Duplicate command line switch /F" );
  149. 							}
  150. 							switch ( arg.Substring( 3 ).ToLower( ) )
  151. 							{
  152. 								case "black":
  153. 									fgcolor = Brushes.Black;
  154. 									break;
  155. 								case "blue":
  156. 									fgcolor = Brushes.Blue;
  157. 									break;
  158. 								case "brown":
  159. 									fgcolor = Brushes.Brown;
  160. 									break;
  161. 								case "cyan":
  162. 									fgcolor = Brushes.Cyan;
  163. 									break;
  164. 								case "darkblue":
  165. 									fgcolor = Brushes.DarkBlue;
  166. 									break;
  167. 								case "darkcyan":
  168. 									fgcolor = Brushes.DarkCyan;
  169. 									break;
  170. 								case "darkgray":
  171. 								case "darkgrey":
  172. 									fgcolor = Brushes.DarkGray;
  173. 									break;
  174. 								case "darkgreen":
  175. 									fgcolor = Brushes.DarkGreen;
  176. 									break;
  177. 								case "darkmagenta":
  178. 									fgcolor = Brushes.DarkMagenta;
  179. 									break;
  180. 								case "darkorange":
  181. 									fgcolor = Brushes.DarkOrange;
  182. 									break;
  183. 								case "dark":
  184. 									fgcolor = Brushes.DarkRed;
  185. 									break;
  186. 								case "gold":
  187. 									fgcolor = Brushes.Gold;
  188. 									break;
  189. 								case "gray":
  190. 								case "grey":
  191. 									fgcolor = Brushes.Gray;
  192. 									break;
  193. 								case "green":
  194. 									fgcolor = Brushes.Green;
  195. 									break;
  196. 								case "lightblue":
  197. 									fgcolor = Brushes.LightBlue;
  198. 									break;
  199. 								case "lightcyan":
  200. 									fgcolor = Brushes.LightCyan;
  201. 									break;
  202. 								case "lightgray":
  203. 								case "lightgrey":
  204. 									fgcolor = Brushes.LightGray;
  205. 									break;
  206. 								case "lightgreen":
  207. 									fgcolor = Brushes.LightGreen;
  208. 									break;
  209. 								case "lightyellow":
  210. 									fgcolor = Brushes.LightYellow;
  211. 									break;
  212. 								case "magenta":
  213. 									fgcolor = Brushes.Magenta;
  214. 									break;
  215. 								case "orange":
  216. 									fgcolor = Brushes.Orange;
  217. 									break;
  218. 								case "pink":
  219. 									fgcolor = Brushes.Pink;
  220. 									break;
  221. 								case "red":
  222. 									fgcolor = Brushes.Red;
  223. 									break;
  224. 								case "silver":
  225. 									fgcolor = Brushes.Silver;
  226. 									break;
  227. 								case "white":
  228. 									fgcolor = Brushes.White;
  229. 									break;
  230. 								case "yellow":
  231. 									fgcolor = Brushes.Yellow;
  232. 									break;
  233. 								case "yellowgreen":
  234. 									fgcolor = Brushes.YellowGreen;
  235. 									break;
  236. 								default:
  237. 									return ShowHelp( "Invalid foreground color specified: \"{0}\"", arg );
  238. 							}
  239. 							break;
  240. 						case 'r':
  241. 							if ( rotation != RotateFlipType.RotateNoneFlipNone )
  242. 							{
  243. 								return ShowHelp( "Duplicate command line switch /R" );
  244. 							}
  245. 							if ( arg.Length < 5 || arg.Length > 7 )
  246. 							{
  247. 								return ShowHelp( "Invalid rotation specified: \"{0}\"", arg );
  248. 							}
  249. 							try
  250. 							{
  251. 								degrees = Convert.ToInt32( arg.Substring( 3 ) );
  252. 								if ( degrees % 90 != 0 )
  253. 								{
  254. 									return ShowHelp( "Invalid rotation specified: \"{0}\"", arg );
  255. 								}
  256. 								degrees = degrees % 360;
  257. 							}
  258. 							catch
  259. 							{
  260. 								return ShowHelp( "Invalid rotation specified: \"{0}\"", arg );
  261. 							}
  262. 							switch ( degrees )
  263. 							{
  264. 								case 0:
  265. 									rotation = RotateFlipType.Rotate90FlipNone;
  266. 									break;
  267. 								case 90:
  268. 									rotation = RotateFlipType.Rotate90FlipNone;
  269. 									break;
  270. 								case 180:
  271. 									rotation = RotateFlipType.Rotate180FlipNone;
  272. 									break;
  273. 								case 270:
  274. 									rotation = RotateFlipType.Rotate270FlipNone;
  275. 									break;
  276. 								default:
  277. 									return ShowHelp( "Invalid rotation specified: \"{0}\"", arg );
  278. 							}
  279. 							break;
  280. 						case 's':
  281. 							if ( fontsize != 48 )
  282. 							{
  283. 								return ShowHelp( "Duplicate command line switch /S" );
  284. 							}
  285. 							try
  286. 							{
  287. 								fontsize = Convert.ToInt32( arg.Substring( 3 ) );
  288. 							}
  289. 							catch
  290. 							{
  291. 								return ShowHelp( "Invalid rotation specified: \"{0}\"", arg );
  292. 							}
  293. 							break;
  294. 						default:
  295. 							return ShowHelp( "Invalid command line argument: \"{0}\"", arg );
  296. 					}
  297. 				}
  298. 				else
  299. 				{
  300. 					if ( String.IsNullOrWhiteSpace( imgfile ) )
  301. 					{
  302. 						if ( Directory.Exists( Directory.GetParent( arg ).FullName ) )
  303. 						{
  304. 							imgfile = Path.GetFullPath( arg );
  305. 							string ext = Path.GetExtension( imgfile ).ToLower( );
  306. 							switch ( ext )
  307. 							{
  308. 								case ".bmp":
  309. 									format = ImageFormat.Bmp;
  310. 									break;
  311. 								case ".gif":
  312. 									format = ImageFormat.Gif;
  313. 									break;
  314. 								case ".jpg":
  315. 								case ".jpeg":
  316. 									format = ImageFormat.Jpeg;
  317. 									break;
  318. 								case ".png":
  319. 									format = ImageFormat.Png;
  320. 									break;
  321. 								case ".tif":
  322. 								case ".tiff":
  323. 									format = ImageFormat.Tiff;
  324. 									break;
  325. 								default:
  326. 									return ShowHelp( "Invalid file type: \"{0}\"", imgfile );
  327. 							}
  328. 						}
  329. 						else
  330. 						{
  331. 							return ShowHelp( "Invalid path for outputfile: \"{0}\"", arg );
  332. 						}
  333. 					}
  334. 					else if ( String.IsNullOrWhiteSpace( text ) )
  335. 					{
  336. 						text = arg;
  337. 					}
  338. 					else
  339. 					{
  340. 						return ShowHelp( );
  341. 					}
  342. 				}
  343. 			}
  344.  
  345. 			#endregion Parse Command Line
  346.  
  347. 			if ( !CheckFont( fontsize ) )
  348. 			{
  349. 				if ( CheckFont( ) )
  350. 				{
  351. 					return ShowHelp( "{0} is not a valid font size for the EAN-13 TrueType font.", fontsize.ToString( ) );
  352. 				}
  353. 				else
  354. 				{
  355. 					string url = "http://www.fontpalace.com/font-download/EAN-13/";
  356. 					string msg = String.Format( "This program uses the EAN-13 TrueType Font, available at\n\n{0}\n\nDo you want to download this font?", url );
  357. 					string title = "Download Missing Font";
  358. 					if ( MessageBox.Show( msg, title, MessageBoxButtons.YesNo ) == DialogResult.Yes )
  359. 					{
  360. 						Process browser = new Process( );
  361. 						browser.StartInfo = new ProcessStartInfo( url );
  362. 						browser.Start( );
  363. 					}
  364. 					return ShowHelp( );
  365. 				}
  366. 			}
  367.  
  368. 			Bitmap bitmap = new Bitmap( 1, 1 );
  369. 			Graphics graphics = Graphics.FromImage( bitmap );
  370. 			Font font = new Font( "EAN-13", fontsize );
  371. 			// Instantiating object of bitmap image again with the correct size for the text and font.
  372. 			SizeF stringsize = graphics.MeasureString( text, font );
  373. 			bitmap = new Bitmap( bitmap, (int) stringsize.Width, (int) stringsize.Height );
  374. 			graphics = Graphics.FromImage( bitmap );
  375. 			// Set background color
  376. 			graphics.Clear( bgcolor );
  377. 			graphics.DrawString( text, font, fgcolor, 0, 0 );
  378. 			font.Dispose( );
  379. 			graphics.Flush( );
  380. 			graphics.Dispose( );
  381. 			// Rotate bitmap image
  382. 			bitmap.RotateFlip( rotation );
  383. 			// Save bitmap image 
  384. 			bitmap.Save( imgfile, format );
  385. 			int rc = (int) stringsize.Width;
  386. 			return rc;
  387. 		}
  388.  
  389.  
  390. 		public static bool CheckFont( int size = 48 )
  391. 		{
  392. 			// Font test by Jeff Hillman
  393. 			// https://stackoverflow.com/a/114003
  394. 			using ( Font fonttest = new Font( "EAN-13", size, FontStyle.Regular, GraphicsUnit.Pixel ) )
  395. 			{
  396. 				return ( fonttest.Name == "EAN-13" );
  397. 			}
  398. 		}
  399.  
  400.  
  401. 		#region Error handling
  402.  
  403. 		public static int ShowHelp( params string[] errmsg )
  404. 		{
  405. 			#region Error Message
  406.  
  407. 			if ( errmsg.Length > 0 )
  408. 			{
  409. 				List<string> errargs = new List<string>( errmsg );
  410. 				errargs.RemoveAt( 0 );
  411. 				Console.Error.WriteLine( );
  412. 				Console.ForegroundColor = ConsoleColor.Red;
  413. 				Console.Error.Write( "ERROR:\t" );
  414. 				Console.ForegroundColor = ConsoleColor.White;
  415. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  416. 				Console.ResetColor( );
  417. 			}
  418.  
  419. 			#endregion Error Message
  420.  
  421. 			#region Help Text
  422.  
  423. 			/*
  424. 			Barcode.exe,  Version 1.00
  425. 			Generate barcode bitmaps using EAN-13 TrueType font
  426.  
  427. 			Usage:    BARCODE   outfile   text   [ options ]
  428.  
  429. 			Where:    outfile   is the output file path (type: bmp, gif, jpg, png or tif)
  430. 			          text      is the text to be converted to barcode
  431.  
  432. 			Options:  /R:deg    Rotate by number of degrees (multiple of 90; default: 0)
  433. 			          /S:size   font Size in pt  (default: 48)
  434. 			          /B:color  Background color (default: white)
  435. 			          /F:color  Foreground color (default: black)
  436.  
  437. 			Credits:  Code to convert text to bitmap by RaviRanjanKr:
  438. 			          https://www.codeproject.com/Tips/184102/Convert-Text-to-Image
  439. 			          Font test by Jeff Hillman:
  440. 			          https://stackoverflow.com/a/114003
  441. 			          EAN-13 font made available by Fontpalace.com:
  442. 			          http://www.fontpalace.com/font-download/EAN-13/
  443.  
  444. 			Notes:    If the required EAN-13 TrueType font is not installed, you will be
  445. 			          prompted to download it.
  446. 			          Though the font name may suggest that the barcode conforms to the
  447. 			          EAN-13 standard, it does not!
  448. 			          Supported background and foreground colors are: Black, Blue *, Brown,
  449. 			          Cyan *, Gold, Gray *, Grey *, Green *, LightYellow, Magenta *,
  450. 			          Orange *, Pink, Red *, Silver, White, Yellow and YellowGreen (* means
  451. 			          Dark and Light variants are also supported, e.g. DarkBlue, LightGreen).
  452. 			          Return code ("errorlevel") equals the output image width in pixels,
  453. 			          or -1 in case of errors.
  454.  
  455. 			Written by Rob van der Woude
  456. 			http://www.robvanderwoude.com
  457. 			*/
  458.  
  459. 			Console.Error.WriteLine( );
  460.  
  461. 			Console.Error.WriteLine( "Barcode.exe,  Version {0}", progver );
  462.  
  463. 			Console.Error.WriteLine( "Generate barcode bitmaps using EAN-13 TrueType font" );
  464.  
  465. 			Console.Error.WriteLine( );
  466.  
  467. 			Console.Error.Write( "Usage:    " );
  468.  
  469. 			Console.ForegroundColor = ConsoleColor.White;
  470. 			Console.Error.WriteLine( "BARCODE   outfile   text   [ options ]" );
  471. 			Console.ResetColor( );
  472.  
  473. 			Console.Error.WriteLine( );
  474.  
  475. 			Console.Error.Write( "Where:    " );
  476. 			Console.ForegroundColor = ConsoleColor.White;
  477. 			Console.Error.Write( "outfile" );
  478. 			Console.ResetColor( );
  479. 			Console.Error.WriteLine( "   is the output file path (type: bmp, gif, jpg, png or tif)" );
  480.  
  481. 			Console.ForegroundColor = ConsoleColor.White;
  482. 			Console.Error.Write( "          text" );
  483. 			Console.ResetColor( );
  484. 			Console.Error.WriteLine( "      is the text to be converted to barcode" );
  485.  
  486. 			Console.Error.WriteLine( );
  487.  
  488. 			Console.Error.Write( "Options:" );
  489. 			Console.ForegroundColor = ConsoleColor.White;
  490. 			Console.Error.Write( "  /R:deg    R" );
  491. 			Console.ResetColor( );
  492. 			Console.Error.Write( "otate by number of " );
  493. 			Console.ForegroundColor = ConsoleColor.White;
  494. 			Console.Error.Write( "deg" );
  495. 			Console.ResetColor( );
  496. 			Console.Error.WriteLine( "rees (multiple of 90; default: 0)" );
  497.  
  498. 			Console.ForegroundColor = ConsoleColor.White;
  499. 			Console.Error.Write( "          /S:size" );
  500. 			Console.ResetColor( );
  501. 			Console.Error.Write( "   font " );
  502. 			Console.ForegroundColor = ConsoleColor.White;
  503. 			Console.Error.Write( "S" );
  504. 			Console.ResetColor( );
  505. 			Console.Error.WriteLine( "ize in pt  (default: 48)" );
  506.  
  507. 			Console.ForegroundColor = ConsoleColor.White;
  508. 			Console.Error.Write( "          /B:color  B" );
  509. 			Console.ResetColor( );
  510. 			Console.Error.Write( "ackground " );
  511. 			Console.ForegroundColor = ConsoleColor.White;
  512. 			Console.Error.Write( "color" );
  513. 			Console.ResetColor( );
  514. 			Console.Error.WriteLine( " (default: white)" );
  515.  
  516. 			Console.ForegroundColor = ConsoleColor.White;
  517. 			Console.Error.Write( "          /F:color  F" );
  518. 			Console.ResetColor( );
  519. 			Console.Error.Write( "oreground " );
  520. 			Console.ForegroundColor = ConsoleColor.White;
  521. 			Console.Error.Write( "color" );
  522. 			Console.ResetColor( );
  523. 			Console.Error.WriteLine( " (default: black)" );
  524.  
  525. 			Console.Error.WriteLine( );
  526.  
  527. 			Console.Error.WriteLine( "Credits:  Code to convert text to bitmap by RaviRanjanKr:" );
  528.  
  529. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  530. 			Console.Error.WriteLine( "          https://www.codeproject.com/Tips/184102/Convert-Text-to-Image" );
  531. 			Console.ResetColor( );
  532.  
  533. 			Console.Error.WriteLine( "          Font test by Jeff Hillman:" );
  534.  
  535. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  536. 			Console.Error.WriteLine( "          https://stackoverflow.com/a/114003" );
  537. 			Console.ResetColor( );
  538.  
  539. 			Console.Error.WriteLine( "          EAN-13 font made available by Fontpalace.com:" );
  540.  
  541. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  542. 			Console.Error.WriteLine( "          http://www.fontpalace.com/font-download/EAN-13/" );
  543. 			Console.ResetColor( );
  544.  
  545. 			Console.Error.WriteLine( );
  546.  
  547. 			Console.Error.WriteLine( "Notes:    If the required EAN-13 TrueType font is not installed, you will be" );
  548.  
  549. 			Console.Error.WriteLine( "          prompted to download it." );
  550.  
  551. 			Console.Error.WriteLine( "          Though the font name may suggest that the barcode conforms to the" );
  552.  
  553. 			Console.Error.WriteLine( "          EAN-13 standard, it does not!" );
  554.  
  555. 			Console.Error.WriteLine( "          Supported background and foreground colors are: Black, Blue *, Brown," );
  556.  
  557. 			Console.Error.WriteLine( "          Cyan *, Gold, Gray *, Grey *, Green *, LightYellow, Magenta *," );
  558.  
  559. 			Console.Error.WriteLine( "          Orange *, Pink, Red *, Silver, White, Yellow and YellowGreen (* means" );
  560.  
  561. 			Console.Error.WriteLine( "          Dark and Light variants are also supported, e.g. DarkBlue)." );
  562.  
  563. 			Console.Error.WriteLine( "          Return code (\"errorlevel\") equals the output image width in pixels," );
  564.  
  565. 			Console.Error.WriteLine( "          or -1 in case of errors." );
  566.  
  567. 			Console.Error.WriteLine( );
  568.  
  569. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  570.  
  571. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  572.  
  573. 			#endregion Help Text
  574.  
  575. 			return -1;
  576. 		}
  577.  
  578. 		#endregion Error handling
  579. 	}
  580. }
  581.  

page last uploaded: 2017-08-21, 14:26