条码

来源:互联网 发布:网络电视机顶盒品牌 编辑:程序博客网 时间:2024/04/26 03:54
 


          if (dgvProductBar.RowCount > 0)
            {
                try
                {
                 读设置参数(打印参数)
                }
                catch
                {                
                }
              

                MakeImage();
             
                try
                {

//打印预览
                    PrintPreviewDialog printPreview = new PrintPreviewDialog();
                    printPreview.PrintPreviewControl.Document = printDocument1;
                    printPreview.ShowDialog();
                }
                catch
                {
                     
                }


            }
            else
            {
                MessageBox.Show("没有数据");
            }

  private void MakeImage()
        {
            if (dgvProductBar.Rows.Count > 0)
            {
                Rectangle rect = barcodeControl2.ClientRectangle;
                if (!Directory.Exists(FolderPath))
                {
                   
                    Directory.CreateDirectory(FolderPath);
                }
                DirectoryInfo Dir = new DirectoryInfo(FolderPath);
                foreach (FileInfo file in Dir.GetFiles())
                {
                    file.Delete();
                }

            //得到的条码集合
                for (int i = 0; i < getBarCodeDt.Rows.Count; i++)
                {
                    string sbcode = getBarCodeDt.Rows[i]["BarCode"].ToString();
                    barcodeControl1.Data = getBarCodeDt.Rows[i]["BarCode"].ToString();
                    Invalidate();

                    string FilePath = "";
                    FilePath = "d:\\Temp\\" + sbcode + ".jpg";
                    try
                    {
                        barcodeControl1.SaveImage(ImageFormat.Jpeg, 1, 100, true, false, null, FilePath);
                    }
                    catch
                    {
                        MessageBox.Show("出错!");
                        return;
                    }
                }
            }
        }

   #region
            files = System.IO.Directory.GetFiles(FolderPath);//得到d盘中的临时文件数组
            Graphics g = e.Graphics;
            Rectangle rect = barcodeControl1.ClientRectangle;
            string sBarcode = "";
            try{
            if (inext < files.Length)
            {
                
                sBarcode = files[inext].Replace(FolderPath, "").Replace(".jpg", "").Trim('\\');
                axBarcodeX1.Caption = sBarcode;
                string srcBar = sBarcode.Substring(0, 4);//                 
                DataRow[] drs = getBarCodeDt.Select(string.Format(" BarCode='{0}'", sBarcode));
                string sProDate = drs[0]["Operate_date"].ToString();

                //string sProductAreaName = "";
                ////drs[0]["ProductAreaName"].ToString();
                string sPrdctsetName = drs[0]["productName"].ToString();
                //drs[0]["PrdctsetName"].ToString();
                ////
                //string sBarTitle = GetTitle(srcBar);
                string sProName = sPrdctsetName;
                //GetTitle1(srcBar);
                string sSellPrice = drs[0]["S_UnitPrice"].ToString();
                //GetTitle2(srcBar, sProductAreaName, sPrdctsetName);
                string sOrigin = "";
                //GetTitleOrigin(srcBar);

                //if(sBarTitle.Length>26)
                // sBarTitle=sBarTitle.Substring(0,24);
                if (sProDate.Length > 0)
                {
                    DateTime dtime = DateTime.Parse(sProDate);
                    sProDate =  dtime.Year.ToString().Substring(2, 2) + dtime.Month.ToString()  + dtime.Day.ToString();
                }
                string str = "";
                if (sProName.Length > 0)
                {
                    //str = sProDate +"\r\n品名:"+ sProName;
                    str = sProDate + "\r\t价格:" + sSellPrice;
                }
                if (sSellPrice.Length > 0)
                    //str += "\r\n价格:" + sSellPrice;
                    str += "\r\n" + sProName;
                if (sOrigin.Length > 0)
                {
                    str += "\r\n" + sOrigin;
                    numericUpDown3.Value = 80;
                }
                else
                {
                    numericUpDown3.Value = 70;
                }
                //axBarcodeX1.Title = str;
                //_______________________________
                float leftMargin = e.MarginBounds.Left;
                float topMargin = e.MarginBounds.Top;
                float xPosition = float.Parse(txtLeftTitle.Text.Trim()); ;
                float yPosition = 5;
                Font myFont = new Font("Arial", float.Parse(txtFontSize.Text.Trim()), FontStyle.Bold);
                SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
                g.DrawString(str, myFont, myBrush, xPosition, yPosition, new StringFormat());
                //_______________________________

                //axBarcodeX1.Title = "品名:" + sProName;
                IntPtr hdc = new IntPtr();
                float iw = e.Graphics.DpiX;
                float ih = e.Graphics.DpiY;
                double ww = System.Convert.ToDouble(txtRW.Text);
                double hh = System.Convert.ToDouble(txtRH.Text);
                hdc = e.Graphics.GetHdc();
                axBarcodeX1.PaintAt((int)hdc, (int)(numericUpDown2.Value), (int)(numericUpDown3.Value), (int)(ww * iw), (int)(hh * ih));
                // Release handle to device context.
                e.Graphics.ReleaseHdc(hdc);

                //-------end
              #region
//                if (inext == files.Length - 1)
//                {
//                    e.HasMorePages = false;
//                }
//                else
//                {
//                    sBarcode = files[inext + 1].Replace(FolderPath, "").Replace(".jpg", "").Trim('\\');
//                    axBarcodeX1.Caption = sBarcode;
//                    srcBar = sBarcode.Substring(0, 4);
//                    drs = dgvProductBar.Select(string.Format(" BarCode='{0}'", sBarcode));
//                    sProDate = drs[0]["Operate_date"].ToString();

//                    sPrdctsetName = drs[0]["productName"].ToString();

//                    sProName = sPrdctsetName;
//                    sSellPrice = drs[0]["S_UnitPrice"].ToString();
//                    sOrigin = "";
//                    //GetTitleOrigin(srcBar);
//                    // drs = dtBarCode.Select(string.Format(" BarCode='{0}'", sBarcode));
//                    // sProDate = drs[0]["Operate_date"].ToString();
//                    if (sProDate.Length > 0)
//                    {
//                        DateTime dtime = DateTime.Parse(sProDate);
//                        sProDate =  dtime.Year.ToString().Substring(2, 2)  + dtime.Month.ToString()  + dtime.Day.ToString();
//                    }
//                    str = "";
//                    if (sProName.Length > 0)
//                    {
//                        //str = sProDate + "\r\n品名:" + sProName;
//                        str = sProDate + "\r\t价格:" + sSellPrice;
//                    }
//                    if (sSellPrice.Length > 0)
//                        //str += "\r\n价格:" + sSellPrice;
//                        str += "\r\n" + sProName;
//                    if (sOrigin.Length > 0)
//                    {
//                        str += "\r\n" + sOrigin;
//                        numericUpDown3.Value = 80;
//                    }
//                    else
//                    {
//                        numericUpDown3.Value = 70;
//                    }
//                    //axBarcodeX1.Title = str;
//                    //axBarcodeX1.Title = "品名:" + sProName;
//                    //___________________________________
//                    //leftMargin = float.Parse(txtTitleSpace.Text.Trim());
//                    topMargin = e.MarginBounds.Top;
//                    xPosition = float.Parse(txtTitleSpace.Text.Trim());
//                    yPosition = 5;
//                    myFont = new Font("Arial", float.Parse(txtFontSize.Text.Trim()), FontStyle.Bold);
//                    g.DrawString(str, myFont, myBrush, xPosition, yPosition, new StringFormat());
//                    //___________________________________
//                    hdc = e.Graphics.GetHdc();
//                    axBarcodeX1.PaintAt((int)hdc, int.Parse(SpaceBet.Text), (int)(numericUpDown3.Value), (int)(ww * iw), (int)(hh * ih));
//                    e.Graphics.ReleaseHdc(hdc);
//                    e.HasMorePages = true;//追加一页从新打印
//                }
                //inext = inext + 2;
                //if (inext == files.Length)
                //{
                //    e.HasMorePages = false;
                //}
#endregion
            }
            else
            {
                e.HasMorePages = false;
            }
            }catch(Exception ex)
            {
           
            }          
           
            e.Graphics.Dispose();
            #endregion