使用ASP.NET从客户端打印条形码

时间:2014-04-30 10:13:57

标签: c# asp.net

我需要从客户端机器上打印条形码,在Visual Studio中运行程序时,条形码打印得很完美,但是当代码发布时,&放入IIS服务器,条形码不在客户端计算机上打印,而是在服务器端打印。

即使我尝试从客户端计算机打印它也不起作用。一旦条形码打印机与服务器连接,它就会打印客户机和服务器上给出的所有条形码。打印机专用于条形码打印,并连接到服务器而不是客户端。

“条形码必须打印客户机

我的代码:

protected void btnPrint_Click(object sender, EventArgs e)
{
    List<string> list = new List<string>();
    foreach (String printer in PrinterSettings.InstalledPrinters)
    {
        list.Add(printer.ToString());
    }
    string printerName = "";
    for (int i = 0; i < list.Count; i++) // Loop through List with for
    {
        printerName = list[i].ToString().ToLower();
        if (printerName.Equals(@"tsc ttp-244 plus"))
        {
            // Console.WriteLine("Printer = " + printer["Name"]);
            if (list[i].ToString().ToLower().Equals("true"))
            {
                // printer is offline by user
                //lblMsg.Text = "Your Plug-N-Play printer is not connected.";
            }
            else
            {
                // printer is not offline               
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                pd.PrinterSettings.PrinterName = "TSC TTP-244 Plus";
                pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 197, 98);//Document No Size                    
                // pd.DefaultPageSettings.Margins = new Margins(70, 12, 12, 12);
                // pd.DefaultPageSettings.Margins.Left = 100;            
                pd.Print();
            }
        }
    }
    PlBarcodeFileNo.Controls.Add(_BarcodeImageView(lblBarcode.Text));
}

private System.Web.UI.WebControls.Image _BarcodeImageView(string barCodeimgNo)
{
    System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
    using (Bitmap bitMap = new Bitmap(barCodeimgNo.Length * 40, 80))
    {
        using (Graphics graphics = Graphics.FromImage(bitMap))
        {
            Font oFont = new Font("IDAutomationHC39M", 16);
            PointF point = new PointF(2f, 2f);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
            graphics.DrawString("*" + barCodeimgNo + "*", oFont, blackBrush, point);
        }
        using (MemoryStream ms = new MemoryStream())
        {
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteImage = ms.ToArray();

            Convert.ToBase64String(byteImage);
            imgBarCode.ImageUrl = "data:image/png;base64," +                     Convert.ToBase64String(byteImage);
        }
    }
    return imgBarCode;
}

0 个答案:

没有答案