public void Print(PrintPageEventArgs e, Brush brush, Point moveAll) { using (var bmp = BarcodeTools.BinaryWritePicture(Text, Height, Size)) { e.Graphics.DrawImage(bmp, X + moveAll.X, Y + moveAll.Y); if (BarText == null) { throw new ApplicationException("BarText empty"); } SizeF s = e.Graphics.MeasureString(BarText.Text, BarText.Font.Value); e.Graphics.DrawString(BarText.Text, BarText.Font.Value, brush, X + ((bmp.Width - s.Width) / 2 + BarText.X) + moveAll.X, // Middle Y + (bmp.Height + BarText.Y) + moveAll.Y); //Bottom } }
static void Main(string[] args) { string input_data_file = ""; string output_image_file = "hanxincode.png"; int version = 1; int ECL = 2; int ii; if ((args.Length < 1) || (Array.IndexOf <string>(args, "-h") >= 0) || (Array.IndexOf <string>(args, "--help") >= 0)) { print_help_message(); return; } input_data_file = args[0]; //parse input parameters for (ii = 1; ii < args.Length; ++ii) { if (args[ii].StartsWith("--out=")) { output_image_file = args[ii].Substring("--out=".Length); } else if (args[ii].StartsWith("--ecl=")) { if (!int.TryParse(args[ii].Substring("--ecl=".Length), out ECL)) { ECL = 2; } } else if (args[ii].StartsWith("--version=")) { if (!int.TryParse(args[ii].Substring("--version=".Length), out version)) { version = 1; } } } //check input file exists if (!File.Exists(input_data_file)) { Console.WriteLine("The input file \"" + input_data_file + "\" does not exist."); return; } //and output is a support image file type FileInfo outInfo = new FileInfo(output_image_file); switch (outInfo.Extension.ToLower()) { case ".bmp": case "jpg": case ".jpeg": case ".png": case ".tif": case ".tiff": break; default: Console.WriteLine("The output file \"" + output_image_file + "\" is not a supported image file."); return; } //Read input Data string data = File.ReadAllText(input_data_file); //Han Xin encode byte[,] symbol_matrix = HanXinCode.EncodeFromCommonData(data, ref version, ref ECL); if (null == symbol_matrix) { Console.WriteLine("The encoding process of Han Xin failed!"); return; } Console.WriteLine("The encoding process of Han Xin succeed:"); Console.WriteLine("\tversion={0:D}", version); Console.WriteLine("\tECL={0:D}", ECL); //BarcodeTools to bitmap Bitmap bitmap_result = BarcodeTools.barcode_bitmap(symbol_matrix); if (null == bitmap_result) { Console.WriteLine("The image construction process failed!"); return; } bitmap_result.Save(output_image_file); Console.WriteLine("Succeed."); }
private void buttonEncode_Click(object sender, EventArgs e) { //reset result view pictureBoxResult.Image = null; labelResult.Text = ""; labelResult.ForeColor = Color.Black; labelResult.BackColor = Color.WhiteSmoke; try { int ecl = comboBoxECL.SelectedIndex + 1; int version = comboBoxVersion.SelectedIndex + 1; string strData = textBoxData.Text; //Get the transmit data from orignal data. //54936 is the codepage of GB18030. //Since Han Xin is more efficient in Chinese information compression, I use this Encoding to show the example. //You can change it to any other Encoding based on your application. //But, please note the encoding here must match the barcode scanner's encoding in actual situation. byte[] transmit_data = BarcodeTools.format_transmit_data(strData, Encoding.GetEncoding(54936)); //Han Xin Encoding byte[,] symbol_matrix = HanXinCode.Encode(transmit_data, ref version, ref ecl, HanXinCode.DEFAULT_ECI, 0); if (null == symbol_matrix) { throw new Exception("There is some error in Han Xin encoding process."); } //symbol matrix to bitmap Bitmap bitmap_result = BarcodeTools.barcode_bitmap(symbol_matrix, 5); if (null == bitmap_result) { throw new Exception("There is some error in symbol matrix 2 bitmap process."); } //show result pictureBoxResult.Image = bitmap_result; if ((bitmap_result.Width >= pictureBoxResult.Width) || (bitmap_result.Height >= pictureBoxResult.Height)) { pictureBoxResult.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureBoxResult.SizeMode = PictureBoxSizeMode.CenterImage; } labelResult.Text = "Succeed.\r\n"; labelResult.Text += "Version:" + version.ToString() + "\r\n"; labelResult.Text += "ECL:" + ecl.ToString(); labelResult.ForeColor = Color.White; labelResult.BackColor = Color.SeaGreen; } catch (Exception ex) { //Encoding Failed for some reason pictureBoxResult.Image = null; labelResult.Text = "Error\r\n" + ex.Message; labelResult.ForeColor = Color.White; labelResult.BackColor = Color.Red; } }