public static string GetBarcodeString(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image) { image = null; BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.BackColor = System.Drawing.Color.White; //图片背景颜色 b.ForeColor = System.Drawing.Color.Black; //条码颜色 b.IncludeLabel = true; b.Alignment = BarcodeLib.AlignmentPositions.CENTER; b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg; //图片格式 System.Drawing.Font font = new System.Drawing.Font("verdana", 10f); //字体设置 b.LabelFont = font; b.Height = height; //图片高度设置(px单位) b.Width = width; //图片宽度设置(px单位) image = b.Encode(type, code); //生成图片 byte[] buffer = b.GetImageData(SaveTypes.GIF); //转换byte格式 //byte转换图片格式 MemoryStream oMemoryStream = new MemoryStream(buffer); //設定資料流位置 oMemoryStream.Position = 0; //return buffer; //return image; return(System.Convert.ToBase64String(buffer, 0, buffer.Length)); }
//public static byte[] GenBarCodeToImage(string code) //{ // return GenBarCodeToImage(code, 1); //} //public static byte[] GenBarCodeToImage(string code, int weight) //{ // try // { // Image img = Code128Rendering.MakeBarcodeImage(code, weight, true); // MemoryStream ms = new MemoryStream(); // img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); // return ms.ToArray(); // } // catch (Exception ex) // { // throw new ArgumentException(ex.Message); // } //} #region GenerateBarCode public static Byte[] GetBarCodeData(string argStrData, string argStrFontName, int argIntWidth, int argIntHeight) { BarcodeLib.Barcode bc = new BarcodeLib.Barcode(); bc.BackColor = System.Drawing.Color.White; bc.ForeColor = System.Drawing.Color.Black; bc.IncludeLabel = false; bc.LabelFont = new System.Drawing.Font(argStrFontName, 10); System.Drawing.Image img = bc.Encode(BarcodeLib.TYPE.CODE128, argStrData, argIntWidth, argIntHeight);//350,80); byte[] bytes = bc.GetImageData(BarcodeLib.SaveTypes.PNG); return(bytes); }
/// <summary> /// 获取显示图片 /// </summary> /// <param name="printContent">打印内容</param> /// <returns>条码头像</returns> public BitmapImage GetBitmapImage(string printContent) { BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.BackColor = System.Drawing.Color.White; b.ForeColor = System.Drawing.Color.Black; b.IncludeLabel = true; b.Alignment = BarcodeLib.AlignmentPositions.CENTER; b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg; System.Drawing.Font font = new System.Drawing.Font("verdana", FontSize); b.LabelFont = font; b.Encode(BarcodeLib.TYPE.CODE128, printContent); byte[] buffer = b.GetImageData(SaveTypes.PNG); Image bitmap; using (MemoryStream ms = new MemoryStream(buffer)) { bitmap = (Image)Image.FromStream(ms); ms.Dispose(); } if (File.Exists("1.png")) { File.Delete("1.png"); } bitmap.Save("1.png"); bitmap.Dispose(); BitmapImage bitmapImage; using (Image image = Image.FromFile("1.png")) { using (Graphics graphics = Graphics.FromImage(image)) { Font fonts = new Font("宋体", FontSize, FontStyle.Bold); SizeF sizeF = graphics.MeasureString(Header, fonts); using (Pen pen = new Pen(Color.White, FontSize * 2)) { graphics.DrawRectangle(pen, new Rectangle(0, 0, image.Width, sizeF.Height.ToInt() + 10));//加椭圆边框 } graphics.DrawString(Header, fonts, System.Drawing.Brushes.Black, new PointF(image.Width / 2 - sizeF.Width / 2, 0)); } bitmapImage = ImageToBitmapImage(image); return(bitmapImage); } }
public BitmapImage GetCell(string content) { BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.BackColor = System.Drawing.Color.White; b.ForeColor = System.Drawing.Color.Black; b.IncludeLabel = true; b.Alignment = BarcodeLib.AlignmentPositions.CENTER; b.ImageFormat = System.Drawing.Imaging.ImageFormat.Png; System.Drawing.Font font = new System.Drawing.Font("utf-8", 15); b.LabelFont = font; #region 限制字号和长度 double screenWidth = SystemParameters.PrimaryScreenWidth; // 屏幕整体宽度 double screenHeight = SystemParameters.PrimaryScreenHeight; double rPower = screenWidth / screenHeight; double cPower = (content.Length * 11 + 35) * 0.01693; #endregion try { b.Encode(BarcodeLib.TYPE.CODE128, content); error.Visibility = Visibility.Hidden; } catch { error.Visibility = Visibility.Visible; return(null); } byte[] buffer = b.GetImageData(SaveTypes.PNG); Image bitmap; using (MemoryStream ms = new MemoryStream(buffer)) { bitmap = (Image)Image.FromStream(ms); ms.Dispose(); } if (File.Exists("1.png")) { File.Delete("1.png"); } bitmap.Save("1.png"); bitmap.Dispose(); b.Dispose(); BitmapImage bitmapImage; using (Image image = Image.FromFile("1.png")) { ServerSetting.MarCodeHeight = image.Height; ServerSetting.MarCodeWidth = image.Width; using (Graphics graphics = Graphics.FromImage(image)) { Font fonts = new Font("楷体", fontSize, System.Drawing.FontStyle.Bold); SizeF sizeF = graphics.MeasureString(header.Text, fonts); using (Pen pen = new Pen(Color.White, fontSize * 2)) { graphics.DrawRectangle(pen, new Rectangle(0, 0, image.Width, sizeF.Height.ToInt() + 10));//加椭圆边框 } graphics.DrawString(header.Text, fonts, System.Drawing.Brushes.Black, new PointF(image.Width / 2 - sizeF.Width / 2, 0)); } bitmapImage = ImageToBitmapImage(image); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; image.Dispose(); } return(bitmapImage); }
public FileResult BarCodeImage(string code) { using (var barCode = new Barcode()) { barCode.IncludeLabel = true; barCode.LabelFont = new Font("Calibri", 11, FontStyle.Bold); barCode.LabelPosition = LabelPositions.BOTTOMCENTER; barCode.Encode(TYPE.ISBN, code, Color.Black, Color.White, 150, 80); return File(barCode.GetImageData(SaveTypes.PNG), "image/png"); } }
private static void PrintCabecalho(TProtNFeInfProt protocol, PdfPTable pdfTable, TNFeInfNFeEmit emit, TNFeInfNFeIde ide, string nfNr, string serie) { var barcode = new Barcode(protocol.chNFe, TYPE.CODE128C); barcode.Encode(TYPE.CODE128C, protocol.chNFe, 300, 50); var byteImg = barcode.GetImageData(SaveTypes.JPG); var barCodeImg = Image.GetInstance(byteImg); pdfTable.AddCell( new BuilderCell("", 5).Create("IDENTIFICAÇÃO DO EMITENTE") .HorizontalCenter() .VerticalTop() .Colspan(9) .NoBorder() .BorderBuilder(true, true, false, true) .Standard()); pdfTable.AddCell( new BuilderCell("", 12).Create("DANFE") .Bold() .HorizontalCenter() .VerticalBottom() .Colspan(4) .NoBorder() .BorderBuilder(true, true, false, true) .Standard()); var barCodeCellfields = new BuilderCell().Create(string.Empty).HorizontalCenter().VerticalCenter().Colspan(6).Rowspan(3).Standard(); barCodeCellfields.AddElement(barCodeImg); pdfTable.AddCell(barCodeCellfields); pdfTable.AddCell( new BuilderCell("", 5).Create(" ").Colspan(9).NoBorder().BorderBuilder(true, true, false, false).Standard()); pdfTable.AddCell( new BuilderCell("", 8).Create("Documento Auxiliar da Nota Fiscal Eletrônica") .HorizontalCenter() .VerticalTop() .Colspan(4) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 11).Create(emit.xNome) .HorizontalCenter() .Colspan(9) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 10).Create("0 - ENTRADA") .VerticalBottom() .Colspan(3) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 12).Create(ide.tpNF.ToString().Equals("Item1") ? "1" : "0") .Bold() .HorizontalCenter() .VerticalCenter() .Colspan(1) .Rowspan(2) .Standard()); string endereçoEmit = string.Format("{0}, {1} - {2}", emit.enderEmit.xLgr, emit.enderEmit.nro, emit.enderEmit.xCpl); pdfTable.AddCell( new BuilderCell("", 8).Create(endereçoEmit) .HorizontalCenter() .Colspan(9) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 10).Create("1 - SAIDA") .VerticalTop() .Colspan(3) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 5).Create("CHAVE DE ACESSO") .HorizontalLeft() .VerticalTop() .Colspan(6) .NoBorder() .BorderBuilder(true, true, false, true) .Standard()); string bairroCep = string.Format("{0} - {1}", emit.enderEmit.xBairro, emit.enderEmit.CEP); pdfTable.AddCell( new BuilderCell("", 8).Create(bairroCep) .HorizontalCenter() .Colspan(9) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 10).Create(nfNr) .Bold() .VerticalBottom() .HorizontalCenter() .Colspan(4) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 6).Create(CustomMasks.MaskNfeId(protocol.chNFe)) .Bold() .HorizontalRight() .VerticalTop() .Colspan(6) .NoBorder() .BorderBuilder(true, true, true, false) .Standard()); string cityAndPhone = string.Format("{0}-{1} Fone/Fax: {2}", emit.enderEmit.xMun, emit.enderEmit.UF, emit.enderEmit.fone); pdfTable.AddCell( new BuilderCell("", 8).Create(cityAndPhone) .HorizontalCenter() .Colspan(9) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 10).Create(serie) .Bold() .VerticalCenter() .HorizontalCenter() .Colspan(4) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 6).Create( "Consulta de autenticidade no portal nacional da NF-e www.nfe.fazenda.gov.br/portal ou no site da Sefaz Autorizadora") .HorizontalCenter() .VerticalCenter() .Colspan(6) .Rowspan(2) .Standard()); pdfTable.AddCell( new BuilderCell("", 8).Create(" ").Colspan(9).NoBorder().BorderBuilder(true, true, false, false).Standard()); pdfTable.AddCell( new BuilderCell("", 8).Create(" ").Colspan(4).NoBorder().BorderBuilder(true, true, false, false).Standard()); pdfTable.AddCell( new BuilderCell("", 5).Create("NATUREZA DA OPERAÇÃO") .HorizontalLeft() .VerticalTop() .Colspan(13) .BorderBuilder(true, true, false, true) .Standard()); pdfTable.AddCell( new BuilderCell("", 5).Create("PROTOCOLO DE AUTORIZAÇÃO DE USO") .HorizontalLeft() .VerticalTop() .Colspan(6) .BorderBuilder(true, true, false, true) .Standard()); string protocoloAutorizacao = string.Format("{0} - {1}", protocol.nProt, CustomMasks.MaskDateTime(protocol.dhRecbto, true)); pdfTable.AddCell( new BuilderCell("", 9).Create(ide.natOp) .VerticalCenter() .HorizontalCenter() .Bold() .Colspan(13) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 9).Create(protocoloAutorizacao) .VerticalCenter() .HorizontalCenter() .Bold() .Colspan(6) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 5).Create("INSCRIÇÃO ESTADUAL") .HorizontalLeft() .VerticalTop() .Colspan(7) .BorderBuilder(true, true, false, true) .Standard()); pdfTable.AddCell( new BuilderCell("", 5).Create("INSCRIÇÃO ESTADUAL DO SUBST. TRIBUT") .HorizontalLeft() .VerticalTop() .Colspan(6) .BorderBuilder(true, true, false, true) .Standard()); pdfTable.AddCell( new BuilderCell("", 5).Create("CNPJ") .HorizontalLeft() .VerticalTop() .Colspan(6) .BorderBuilder(true, true, false, true) .Standard()); pdfTable.AddCell( new BuilderCell("", 9).Create(emit.IE) .VerticalCenter() .HorizontalCenter() .Bold() .Colspan(7) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 9).Create(emit.IEST) .VerticalCenter() .HorizontalCenter() .Bold() .Colspan(6) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); pdfTable.AddCell( new BuilderCell("", 9).Create(CustomMasks.MaskCpfCnpj(emit.Item)) .VerticalCenter() .HorizontalCenter() .Bold() .Colspan(6) .NoBorder() .BorderBuilder(true, true, false, false) .Standard()); }
public static byte[] GetBarcodeBytes(string data) { BarcodeLib.Barcode b = new BarcodeLib.Barcode(); b.Encode(TYPE.CODE128, data); return(b.GetImageData(SaveTypes.PNG)); }