private void btnEncode_Click(object sender, EventArgs e) { errorProvider1.Clear(); int W = Convert.ToInt32(txtWidth.Text.Trim()); int H = Convert.ToInt32(txtHeight.Text.Trim()); BarcodeLib.AlignmentPositions alignment = BarcodeLib.AlignmentPositions.CENTER; BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED; type = BarcodeLib.TYPE.CODE39; btnSave.Enabled = true; btn_Save_Info.Enabled = true; try { if (type != BarcodeLib.TYPE.UNSPECIFIED) { RotateFlipType rotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), cbRotateFlip.SelectedItem.ToString(), true); //label alignment and position BarcodeLib.LabelPositions labelPosition = (BarcodeLib.LabelPositions)Enum.Parse(typeof(BarcodeLib.LabelPositions), cbLabelLocation.SelectedItem.ToString(), true); barcode.Image = b.Encode(type, txtData.Text.Trim(), b.ForeColor, b.BackColor, W, H, alignment, rotateFlipType, chkGenerateLabel.Checked, labelPosition); txtEncoded.Text = b.EncodedValue; } barcode.Width = barcode.Image.Width; barcode.Height = barcode.Image.Height; //reposition the barcode image to the middle barcode.Location = new Point((groupBox2.Location.X + groupBox2.Width / 2) - barcode.Width / 2, (groupBox2.Location.Y + groupBox2.Height / 2) - barcode.Height / 2); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void convertBarcodeFromString(string str) { int W = Convert.ToInt32(203); int H = Convert.ToInt32(35); BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED; type = BarcodeLib.TYPE.CODE93; try { if (type != BarcodeLib.TYPE.UNSPECIFIED) { RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone; //(RotateFlipType)Enum.Parse(typeof(RotateFlipType), "RotateNoneFlipNone".ToString(), true); // dau can phai ranh vay dau BarcodeLib.LabelPositions labelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; //(BarcodeLib.LabelPositions)Enum.Parse(typeof(BarcodeLib.LabelPositions), "BottomCenter".ToString(), true); barcode.Image = b.Encode(type, str, Color.FromArgb(0, 0, 0), Color.FromArgb(255, 255, 255), W, H, BarcodeLib.AlignmentPositions.CENTER, rotateFlipType, false, labelPosition); } } catch (Exception ex) { MessageBox.Show(ex.Message); } var barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new EncodingOptions { Height = 50, Width = 50, Margin = 0 } }; using (var bitmap = barcodeWriter.Write(str)) { using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Png); var image = Image.FromStream(stream); pictureBox.Image = image; } } PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); pd.Print(); txtData.Clear(); }
public static bool GenerateBarcode( int barcodeEncoding , int width , int height , BarcodeLib.AlignmentPositions alignmentPosition , System.Drawing.RotateFlipType rotateFlipType , BarcodeLib.LabelPositions labelPosition , string stringToEncode , string imagePathAndName , BarcodeLib.SaveTypes saveType , System.Windows.Forms.GroupBox barcode , ref string errorMessage ) { errorMessage = string.Empty; BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.Alignment = alignmentPosition; BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED; if (barcodeEncoding > 1) { barcodeEncoding = 1; } switch (barcodeEncoding) { ////case 0: type = BarcodeLib.TYPE.UPCA; break; //"UPC-A" ////case 1: type = BarcodeLib.TYPE.UPCE; break; //"UPC-E" ////case 2: type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break; //"UPC 2 Digit Ext." ////case 3: type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break; //"UPC 5 Digit Ext". ////case 4: type = BarcodeLib.TYPE.EAN13; break; //"EAN-13" ////case 5: type = BarcodeLib.TYPE.JAN13; break; //"JAN-13" ////case 6: type = BarcodeLib.TYPE.EAN8; break; //"EAN-8" ////case 7: type = BarcodeLib.TYPE.ITF14; break; //"ITF-14" //case 0: type = BarcodeLib.TYPE.Interleaved2of5; break; //"Interleaved 2 of 5" //case 1: type = BarcodeLib.TYPE.Standard2of5; break; //"Standard 2 of 5" ////case 10: type = BarcodeLib.TYPE.Codabar; break; //"Codabar" ////case 11: type = BarcodeLib.TYPE.PostNet; break; //"PostNet" //case 2: type = BarcodeLib.TYPE.BOOKLAND; break; //"Bookland/ISBN" //case 3: type = BarcodeLib.TYPE.CODE11; break; //"Code 11" case 0: type = BarcodeLib.TYPE.CODE39; break; //"Code 39" //case 5: type = BarcodeLib.TYPE.CODE39Extended; break; //"Code 39 Extended" //case 6: type = BarcodeLib.TYPE.CODE39_Mod43; break; //"Code 39 Mod 43" //case 7: type = BarcodeLib.TYPE.CODE93; break; //"Code 93" case 1: type = BarcodeLib.TYPE.CODE128; break; //"Code 128" //case 9: type = BarcodeLib.TYPE.CODE128A; break; //"Code 128-A" //case 10: type = BarcodeLib.TYPE.CODE128B; break; //"Code 128-B" //case 11: type = BarcodeLib.TYPE.CODE128C; break; //"Code 128-C" //case 12: type = BarcodeLib.TYPE.LOGMARS; break; //"LOGMARS" //case 13: type = BarcodeLib.TYPE.MSI_Mod10; break; //"MSI" //case 14: type = BarcodeLib.TYPE.TELEPEN; break; //"Telepen" ////case 25: type = BarcodeLib.TYPE.FIM; break; //"FIM" ////case 26: type = BarcodeLib.TYPE.PHARMACODE; break; //"Pharmacode" default: type = BarcodeLib.TYPE.CODE128; break; //"Code 128" }//switch try { if (type != BarcodeLib.TYPE.UNSPECIFIED) { b.IncludeLabel = true; b.RotateFlipType = rotateFlipType; b.LabelPosition = labelPosition; if (barcode == null) { barcode = new System.Windows.Forms.GroupBox(); // // barcode // barcode.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; barcode.Dock = System.Windows.Forms.DockStyle.Fill; barcode.Location = new System.Drawing.Point(0, 0); barcode.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); barcode.Name = "barcode"; barcode.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4); barcode.Size = new System.Drawing.Size(754, 414); barcode.TabIndex = 36; barcode.TabStop = false; barcode.Text = "Barcode Image"; barcode.Visible = false; } //===== Encoding performed here ===== barcode.BackgroundImage = b.Encode(type, stringToEncode, System.Drawing.Color.Black, System.Drawing.Color.White, width, height); //=================================== //reposition the barcode image to the middle barcode.Location = new System.Drawing.Point((barcode.Location.X + barcode.Width / 2) - barcode.Width / 2, (barcode.Location.Y + barcode.Height / 2) - barcode.Height / 2); if (!string.IsNullOrEmpty(imagePathAndName)) { b.SaveImage(imagePathAndName, saveType); } } //if } //try catch (Exception ex) { errorMessage = ex.Message; return(false); }//catch return(true); }
}//Form1_Load //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private void btnEncode_Click(object sender, EventArgs e) { errorProvider1.Clear(); int W = Convert.ToInt32(txtWidth.Text.Trim()); int H = Convert.ToInt32(txtHeight.Text.Trim()); BarcodeLib.AlignmentPositions alignment; //barcode alignment switch (cbBarcodeAlign.SelectedItem.ToString().Trim().ToLower()) { case "left": alignment = BarcodeLib.AlignmentPositions.LEFT; break; case "right": alignment = BarcodeLib.AlignmentPositions.RIGHT; break; default: alignment = BarcodeLib.AlignmentPositions.CENTER; break; }//switch BarcodeLib.TYPE type = BarcodeLib.TYPE.UNSPECIFIED; switch (cbEncodeType.SelectedItem.ToString().Trim()) { case "UPC-A": type = BarcodeLib.TYPE.UPCA; break; case "UPC-E": type = BarcodeLib.TYPE.UPCE; break; case "UPC 2 Digit Ext.": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_2DIGIT; break; case "UPC 5 Digit Ext.": type = BarcodeLib.TYPE.UPC_SUPPLEMENTAL_5DIGIT; break; case "EAN-13": type = BarcodeLib.TYPE.EAN13; break; case "JAN-13": type = BarcodeLib.TYPE.JAN13; break; case "EAN-8": type = BarcodeLib.TYPE.EAN8; break; case "ITF-14": type = BarcodeLib.TYPE.ITF14; break; case "Codabar": type = BarcodeLib.TYPE.Codabar; break; case "PostNet": type = BarcodeLib.TYPE.PostNet; break; case "Bookland/ISBN": type = BarcodeLib.TYPE.BOOKLAND; break; case "Code 11": type = BarcodeLib.TYPE.CODE11; break; case "Code 39": type = BarcodeLib.TYPE.CODE39; break; case "Code 39 Extended": type = BarcodeLib.TYPE.CODE39Extended; break; case "Code 39 Mod 43": type = BarcodeLib.TYPE.CODE39_Mod43; break; case "Code 93": type = BarcodeLib.TYPE.CODE93; break; case "LOGMARS": type = BarcodeLib.TYPE.LOGMARS; break; case "MSI": type = BarcodeLib.TYPE.MSI_Mod10; break; case "Interleaved 2 of 5": type = BarcodeLib.TYPE.Interleaved2of5; break; case "Standard 2 of 5": type = BarcodeLib.TYPE.Standard2of5; break; case "Code 128": type = BarcodeLib.TYPE.CODE128; break; case "Code 128-A": type = BarcodeLib.TYPE.CODE128A; break; case "Code 128-B": type = BarcodeLib.TYPE.CODE128B; break; case "Code 128-C": type = BarcodeLib.TYPE.CODE128C; break; case "Telepen": type = BarcodeLib.TYPE.TELEPEN; break; case "FIM": type = BarcodeLib.TYPE.FIM; break; case "Pharmacode": type = BarcodeLib.TYPE.PHARMACODE; break; default: MessageBox.Show("Please specify the encoding type."); break; }//switch try { if (type != BarcodeLib.TYPE.UNSPECIFIED) { RotateFlipType rotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), cbRotateFlip.SelectedItem.ToString(), true); //label alignment and position BarcodeLib.LabelPositions labelPosition = (BarcodeLib.LabelPositions)Enum.Parse(typeof(BarcodeLib.LabelPositions), cbLabelLocation.SelectedItem.ToString(), true); //===== Encoding performed here ===== barcode.Image = b.Encode(type, txtData.Text.Trim(), btnForeColor.BackColor, btnBackColor.BackColor, W, H, alignment, rotateFlipType, chkGenerateLabel.Checked, labelPosition); //=================================== //show the encoding time lblEncodingTime.Text = "(" + Math.Round(b.EncodingTime, 0, MidpointRounding.AwayFromZero).ToString() + "ms)"; txtEncoded.Text = b.EncodedValue; tsslEncodedType.Text = "Encoding Type: " + b.EncodedType.ToString(); }//if barcode.Width = barcode.Image.Width; barcode.Height = barcode.Image.Height; //reposition the barcode image to the middle barcode.Location = new Point((groupBox2.Location.X + groupBox2.Width / 2) - barcode.Width / 2, (groupBox2.Location.Y + groupBox2.Height / 2) - barcode.Height / 2); }//try catch (Exception ex) { MessageBox.Show(ex.Message); }//catch }//btnEncode_Click