public void Print(List <TicketBase> tickets, int issueingNumber, Action <int> onPrint, Action <Exception, int> onError) { StarPrinterStatus status = port.BeginCheckedBlock(); if (status.Offline) { } ICommandBuilder builder = StarIoExt.CreateCommandBuilder(Emulation.StarGraphic); builder.BeginDocument(); tickets.ForEach((t, i) => { try { int height = 8 * 58 - 85 - 15; // height = 8 * 58 - 25 - 15; Bitmap bmp = t.Bitmap.Clone(new Rectangle((t.Bitmap.Width - 576) / 2, 70, 576, height), t.Bitmap.PixelFormat); builder.AppendBitmapWithAbsolutePosition(bmp, false, 576, false, BitmapConverterRotation.Normal, 0); builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed); bmp.Dispose(); onPrint(i); } catch (Exception ex) { onError(ex, i); } }); builder.EndDocument(); byte[] command = builder.Commands; uint writtenLength = port.WritePort(command, 0, (uint)command.Length); status = port.EndCheckedBlock(); if (status.Offline) { } }
public static byte[] CreatePageModeData(Emulation emulation, int width) { byte[] data; data = Encoding.UTF8.GetBytes("Hello World.\n"); Bitmap starLogoImage; using (var stream = Properties.Resources.star_logo_image) { starLogoImage = new Bitmap(stream); } int height = 30 * 8; // 30mm!! Rectangle rectangle; ICommandBuilder builder = StarIoExt.CreateCommandBuilder(emulation); builder.BeginDocument(); builder.Append(Encoding.UTF8.GetBytes("\n*Normal*\n")); rectangle = new Rectangle(0, 0, width, height); builder.BeginPageMode(rectangle, BitmapConverterRotation.Normal); builder.Append(data); builder.AppendPageModeVerticalAbsolutePosition(160); builder.Append(data); builder.AppendPageModeVerticalAbsolutePosition(80); builder.AppendAbsolutePosition(data, 40); builder.EndPageMode(); builder.Append(Encoding.UTF8.GetBytes("\n*Right90*\n")); //rectangle = new Rectangle(0, 0, width, height); //rectangle = new Rectangle(0, 0, height, width); rectangle = new Rectangle(0, 0, width, width); builder.BeginPageMode(rectangle, BitmapConverterRotation.Right90); builder.Append(data); builder.AppendPageModeVerticalAbsolutePosition(160); builder.Append(data); builder.AppendPageModeVerticalAbsolutePosition(80); builder.AppendAbsolutePosition(data, 40); builder.EndPageMode(); //builder.Append(Encoding.UTF8.GetBytes("\n*Rotate180*\n")); //rectangle = new Rectangle(0, 0, width, height); //builder.BeginPageMode(rectangle, BitmapConverterRotation.Rotate180); //builder.Append(data); //builder.AppendPageModeVerticalAbsolutePosition(160); //builder.Append(data); //builder.AppendPageModeVerticalAbsolutePosition(80); //builder.AppendAbsolutePosition(data, 40); //builder.EndPageMode(); //builder.Append(Encoding.UTF8.GetBytes("\n*Left90*\n")); //rectangle = new Rectangle(0, 0, width, height); //rectangle = new Rectangle(0, 0, height, width); //builder.BeginPageMode(rectangle, BitmapConverterRotation.Left90); //builder.Append(data); //builder.AppendPageModeVerticalAbsolutePosition(160); //builder.Append(data); //builder.AppendPageModeVerticalAbsolutePosition(80); //builder.AppendAbsolutePosition(data, 40); //builder.EndPageMode(); builder.Append(Encoding.UTF8.GetBytes("\n*Mixed Text*\n")); //rectangle = new Rectangle(0, 0, width, height); rectangle = new Rectangle(0, 0, width, width); builder.BeginPageMode(rectangle, BitmapConverterRotation.Normal); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendAbsolutePosition(data, width / 2); builder.AppendPageModeRotation(BitmapConverterRotation.Right90); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendAbsolutePosition(data, width / 2); builder.AppendPageModeRotation(BitmapConverterRotation.Rotate180); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendAbsolutePosition(data, width / 2); builder.AppendPageModeRotation(BitmapConverterRotation.Left90); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendAbsolutePosition(data, width / 2); builder.EndPageMode(); builder.Append(Encoding.UTF8.GetBytes("\n*Mixed Bitmap*\n")); //rectangle = new Rectangle(0, 0, width, height); rectangle = new Rectangle(0, 0, width, width); builder.BeginPageMode(rectangle, BitmapConverterRotation.Normal); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendBitmapWithAbsolutePosition(starLogoImage, false, width / 2); builder.AppendPageModeRotation(BitmapConverterRotation.Right90); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendBitmapWithAbsolutePosition(starLogoImage, true, width / 2); builder.AppendPageModeRotation(BitmapConverterRotation.Rotate180); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendBitmapWithAbsolutePosition(starLogoImage, true, width / 2); builder.AppendPageModeRotation(BitmapConverterRotation.Left90); builder.AppendPageModeVerticalAbsolutePosition(width / 2); builder.AppendBitmapWithAbsolutePosition(starLogoImage, true, width / 2); builder.EndPageMode(); builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed); builder.EndDocument(); return(builder.Commands); }
public static byte[] CreateBitmapData(Emulation emulation) { ICommandBuilder builder = StarIoExt.CreateCommandBuilder(emulation); builder.BeginDocument(); string logoFile = appPath + @"\" + dr["logo"]; Bitmap logo = (Bitmap)Bitmap.FromFile(logoFile); Bitmap newImage = ResizeBitmap(logo, 400, 66); builder.AppendBitmap(newImage, false); builder.AppendUnitFeed(16); String BitmapTitle; Font TitleFont; Bitmap rasterImage; if (dr["atttext"] != "") { BitmapTitle = "電子發票證明聯補印\n"; TitleFont = new Font("新細明體", 36); rasterImage = CreateBitmapFromString(BitmapTitle, 92.0F, 96.0F, TitleFont, -8); } else { BitmapTitle = "電子發票證明聯\n"; TitleFont = new Font("新細明體", 40); rasterImage = CreateBitmapFromString(BitmapTitle, 96.0F, 96.0F, TitleFont, 0); } builder.AppendBitmap(rasterImage, false); String InvoiceTitle = " " + dr["year"] + "年" + dr["months"] + "月" + "\n"; InvoiceTitle += " " + dr["invoice"] + "\n"; Font InvoiceTitleFont = new Font("新細明體", 40, FontStyle.Bold); Bitmap rasterImage2 = CreateBitmapFromString(InvoiceTitle, 96.0F, 96.0F, InvoiceTitleFont, 0); builder.AppendBitmap(rasterImage2, false); //格式 String DateTime = " " + dr["printtime"] + " "; if (dr["fixtext"] != "") { DateTime += "格式 " + dr["fixtext"] + "\n"; } else { DateTime += "\n"; } Font DateTimeFont = new Font("新細明體", 20); Bitmap rasterImage3 = CreateBitmapFromString(DateTime, 96.0F, 96.0F, DateTimeFont, 0); builder.AppendBitmap(rasterImage3, false); String Random_Total = " 隨機碼 " + dr["randcode"] + " "; Random_Total += "總計 " + dr["total"] + "\n"; Font Random_TotalFont = new Font("新細明體", 20); Bitmap rasterImage4 = CreateBitmapFromString(Random_Total, 96.0F, 96.0F, Random_TotalFont, 0); builder.AppendBitmap(rasterImage4, false); String Seller_Buyer = " 賣方 " + dr["sellerid"] + " "; if (dr["byerid"] != "0000000000") { Seller_Buyer += "買方 " + dr["byerid"] + "\n"; } else { Seller_Buyer += "\n"; } Font Seller_BuyerFont = new Font("新細明體", 20); Bitmap rasterImage5 = CreateBitmapFromString(Seller_Buyer, 96.0F, 96.0F, Seller_BuyerFont, 0); builder.AppendBitmap(rasterImage5, false); builder.AppendUnitFeed(5); Image c39Img = GetCode39(dr["barcode"], 50); //計算條碼寬度產生的比例(會依照印表機DPI值變化) //可列印寬度(兩側留白0.3以上) = 5.7 - (0.3 * 2) = 5.1cm float ItoC = 2.54f; float dpiX = (float)200; float dpiY = (float)200; float widthlimit = (5.7f / ItoC) * dpiX; float rate = 0f; float newWidth = 0f; do { rate++; newWidth = c39Img.Width * rate; } while ((c39Img.Width * (rate + 1.0f)) <= widthlimit); //計算X定位點(條碼置中) float newX = (((5.7f / ItoC) * dpiX) / 2.0f) - (newWidth / 2.0f); Bitmap mybmp1 = new Bitmap((int)newWidth, 60); Graphics gr1 = Graphics.FromImage(mybmp1); gr1.DrawImage(c39Img, new Point[] { new Point(0, 0), new Point((int)newWidth, 0), new Point(0, c39Img.Height) }); gr1.Dispose(); builder.AppendBitmap(mybmp1, false); //QR碼 //利用matrix來計算產生QR碼的實際Size(去白邊) var hints = new Dictionary <EncodeHintType, object> { { EncodeHintType.CHARACTER_SET, "UTF-8" }, { EncodeHintType.QR_VERSION, 8 } }; var matrix = new MultiFormatWriter().encode(dr["qrcode1"], BarcodeFormat.QR_CODE, 140, 140, hints); var matrix2 = new MultiFormatWriter().encode(dr["qrcode2"], BarcodeFormat.QR_CODE, 140, 140, hints); matrix = CutWhiteBorder(matrix); matrix2 = CutWhiteBorder(matrix2); //把QR碼實際Size給BarcodeWriter參考產生 var qr1Writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions { Height = matrix.Height, Width = matrix.Width, CharacterSet = "utf-8", Margin = 0, ErrorCorrection = ErrorCorrectionLevel.L, QrVersion = 8 } }; var qr2Writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions { Height = matrix2.Height, Width = matrix2.Width, CharacterSet = "utf-8", Margin = 0, ErrorCorrection = ErrorCorrectionLevel.L, QrVersion = 8 } }; //QR碼至此產生的大小為不含白邊的原圖大小 Image qr1Img = qr1Writer.Write(dr["qrcode1"]); Image qr2Img = qr2Writer.Write(dr["qrcode2"]); Bitmap mybmp = new Bitmap(600, 180); Graphics gr = Graphics.FromImage(mybmp); //處理第一張圖片 gr.DrawImage(qr1Img, new Point[] { new Point(0, 0), new Point(140, 0), new Point(0, 140) }); gr.DrawImage(qr2Img, new Point[] { new Point(204, 0), new Point(344, 0), new Point(204, 140) }); //處理第二張圖片 gr.Dispose(); builder.AppendBitmapWithAbsolutePosition(mybmp, false, 30); // 打統編要接著印交易明細 if (dr["byerid"] == "0000000000") { builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed); } builder.AppendPeripheral(PeripheralChannel.No1); builder.EndDocument(); return(builder.Commands); }
public static byte[] CreateBitmapData(Emulation emulation, int width) { Bitmap sphereImage; using (var stream = Properties.Resources.sphere_image) { sphereImage = new Bitmap(stream); } Bitmap starLogoImage; using (var stream = Properties.Resources.star_logo_image) { starLogoImage = new Bitmap(stream); } ICommandBuilder builder = StarIoExt.CreateCommandBuilder(emulation); builder.BeginDocument(); builder.Append(Encoding.UTF8.GetBytes("*diffusion:true*\n")); builder.AppendBitmap(sphereImage, true); builder.Append(Encoding.UTF8.GetBytes("\n*diffusion:false*\n")); builder.AppendBitmap(sphereImage, false); builder.Append(Encoding.UTF8.GetBytes("\n*Normal*\n")); builder.AppendBitmap(starLogoImage, true); builder.Append(Encoding.UTF8.GetBytes("\n*width:Full, bothScale:true*\n")); builder.AppendBitmap(starLogoImage, true, width, true); builder.Append(Encoding.UTF8.GetBytes("\n*width:Full, bothScale:false*\n")); builder.AppendBitmap(starLogoImage, true, width, false); builder.Append(Encoding.UTF8.GetBytes("\n*Right90*\n")); builder.AppendBitmap(starLogoImage, true, BitmapConverterRotation.Right90); builder.Append(Encoding.UTF8.GetBytes("\n*Rotate180*\n")); builder.AppendBitmap(starLogoImage, true, BitmapConverterRotation.Rotate180); //builder.Append(Encoding.UTF8.GetBytes("\n*Left90*\n")); //builder.AppendBitmap(starLogoImage, true, BitmapConverterRotation.Left90); builder.Append(Encoding.UTF8.GetBytes("\n*Normal, AbsolutePosition:40*\n")); builder.AppendBitmapWithAbsolutePosition(starLogoImage, true, 40); //builder.Append(Encoding.UTF8.GetBytes("\n*Right90, AbsolutePosition:40*\n")); //builder.AppendBitmapWithAbsolutePosition(starLogoImage, true, BitmapConverterRotation.Right90, 40); //builder.Append(Encoding.UTF8.GetBytes("\n*Rotate180, AbsolutePosition:40*\n")); //builder.AppendBitmapWithAbsolutePosition(starLogoImage, true, BitmapConverterRotation.Rotate180, 40); //builder.Append(Encoding.UTF8.GetBytes("\n*Left90, AbsolutePosition:40*\n")); //builder.AppendBitmapWithAbsolutePosition(starLogoImage, true, BitmapConverterRotation.Left90, 40); builder.Append(Encoding.UTF8.GetBytes("\n*Normal, Alignment:Center*\n")); builder.AppendBitmapWithAlignment(starLogoImage, true, AlignmentPosition.Center); //builder.Append(Encoding.UTF8.GetBytes("\n*Right90, Alignment:Center*\n")); //builder.AppendBitmapWithAlignment(starLogoImage, true, BitmapConverterRotation.Right90, AlignmentPosition.Center); //builder.Append(Encoding.UTF8.GetBytes("\n*Rotate180, Alignment:Center*\n")); //builder.AppendBitmapWithAlignment(starLogoImage, true, BitmapConverterRotation.Rotate180, AlignmentPosition.Center); //builder.Append(Encoding.UTF8.GetBytes("\n*Left90, Alignment:Center*\n")); //builder.AppendBitmapWithAlignment(starLogoImage, true, BitmapConverterRotation.Left90, AlignmentPosition.Center); builder.Append(Encoding.UTF8.GetBytes("\n*Normal, Alignment:Right*\n")); builder.AppendBitmapWithAlignment(starLogoImage, true, AlignmentPosition.Right); //builder.Append(Encoding.UTF8.GetBytes("\n*Right90, Alignment:Right*\n")); //builder.AppendBitmapWithAlignment(starLogoImage, true, BitmapConverterRotation.Right90, AlignmentPosition.Right); //builder.Append(Encoding.UTF8.GetBytes("\n*Rotate180, Alignment:Right*\n")); //builder.AppendBitmapWithAlignment(starLogoImage, true, BitmapConverterRotation.Rotate180, AlignmentPosition.Right); //builder.Append(Encoding.UTF8.GetBytes("\n*Left90, Alignment:Right*\n")); //builder.AppendBitmapWithAlignment(starLogoImage, true, BitmapConverterRotation.Left90, AlignmentPosition.Right); builder.AppendCutPaper(CutPaperAction.PartialCutWithFeed); builder.EndDocument(); return(builder.Commands); }