Browse Source

供应商打印增加图片打印功能

Hcsy 7 years ago
parent
commit
3bc94e61b6

+ 1 - 1
UAS_Web/Browser.cs

@@ -18,7 +18,7 @@ namespace UAS_Web
         {
             InitializeComponent();
             Text = "思拓微-供应商条码打印";
-            string path = "https://218.17.158.219:9443/uas_dev/jsps/vendbarcode/login.jsp";
+            string path = "http://192.168.253.80:8090/ERP/jsps/vendbarcode/login.jsp";
             webBrowser = new ChromiumWebBrowser(path)
             {
                 Dock = DockStyle.Fill

+ 30 - 1
UAS_Web/PrinterList.Designer.cs

@@ -31,6 +31,8 @@
             this.Printer = new System.Windows.Forms.ComboBox();
             this.Confirm = new System.Windows.Forms.Button();
             this.PrintFile = new System.Drawing.Printing.PrintDocument();
+            this.PRXA = new System.Windows.Forms.RadioButton();
+            this.PRPIC = new System.Windows.Forms.RadioButton();
             this.SuspendLayout();
             // 
             // Printer
@@ -52,11 +54,35 @@
             this.Confirm.Text = "确认";
             this.Confirm.UseVisualStyleBackColor = true;
             // 
+            // PRXA
+            // 
+            this.PRXA.AutoSize = true;
+            this.PRXA.Checked = true;
+            this.PRXA.Location = new System.Drawing.Point(29, 84);
+            this.PRXA.Name = "PRXA";
+            this.PRXA.Size = new System.Drawing.Size(71, 16);
+            this.PRXA.TabIndex = 2;
+            this.PRXA.TabStop = true;
+            this.PRXA.Text = "指令打印";
+            this.PRXA.UseVisualStyleBackColor = true;
+            // 
+            // PRPIC
+            // 
+            this.PRPIC.AutoSize = true;
+            this.PRPIC.Location = new System.Drawing.Point(226, 84);
+            this.PRPIC.Name = "PRPIC";
+            this.PRPIC.Size = new System.Drawing.Size(71, 16);
+            this.PRPIC.TabIndex = 3;
+            this.PRPIC.Text = "图片打印";
+            this.PRPIC.UseVisualStyleBackColor = true;
+            // 
             // PrinterList
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(321, 170);
+            this.Controls.Add(this.PRPIC);
+            this.Controls.Add(this.PRXA);
             this.Controls.Add(this.Confirm);
             this.Controls.Add(this.Printer);
             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
@@ -65,7 +91,8 @@
             this.Text = "打印机列表";
             this.TopMost = true;
             this.Load += new System.EventHandler(this.PrinterList_Load);
-            this.ResumeLayout(true);
+            this.ResumeLayout(false);
+            this.PerformLayout();
 
         }
 
@@ -74,5 +101,7 @@
         private System.Windows.Forms.ComboBox Printer;
         private System.Windows.Forms.Button Confirm;
         private System.Drawing.Printing.PrintDocument PrintFile;
+        private System.Windows.Forms.RadioButton PRXA;
+        private System.Windows.Forms.RadioButton PRPIC;
     }
 }

+ 10 - 0
UAS_Web/UAS_Web.csproj

@@ -72,6 +72,9 @@
     <ErrorReport>prompt</ErrorReport>
     <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
   </PropertyGroup>
+  <PropertyGroup>
+    <ApplicationIcon>tool\print.ico</ApplicationIcon>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="CefSharp, Version=49.0.1.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138, processorArchitecture=x86">
       <SpecificVersion>False</SpecificVersion>
@@ -96,6 +99,10 @@
     <Reference Include="System.Drawing" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
+    <Reference Include="zxing, Version=0.16.2.0, Culture=neutral, PublicKeyToken=4e88037ac681fe60, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>tool\zxing.dll</HintPath>
+    </Reference>
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Browser.cs">
@@ -150,6 +157,9 @@
   <ItemGroup>
     <None Include="tool\print.ico" />
   </ItemGroup>
+  <ItemGroup>
+    <Content Include="tool\zxing.dll" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="..\packages\cef.redist.x64.3.2623.1401\build\cef.redist.x64.targets" Condition="Exists('..\packages\cef.redist.x64.3.2623.1401\build\cef.redist.x64.targets')" />
   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

+ 67 - 1
UAS_Web/tool/PrintHelper.cs

@@ -1,9 +1,14 @@
 using System;
 using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Drawing2D;
+using System.Drawing.Text;
 using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
 using System.Text;
+using ZXing;
+using ZXing.QrCode;
 
 namespace UAS_Web.tool
 {
@@ -92,7 +97,6 @@ namespace UAS_Web.tool
             // 非托管指针
             IntPtr pUnmanagedBytes = new IntPtr(0);
             int nLength;
-
             nLength = Convert.ToInt32(fs.Length);
             // 读取文件的内容放入数组中.
             bytes = br.ReadBytes(nLength);
@@ -120,5 +124,67 @@ namespace UAS_Web.tool
             Marshal.FreeCoTaskMem(pBytes);
             return true;
         }
+
+        //图片添加文字
+        public static void Printstring(string str, int size, Graphics g, double x, double y)
+        {
+            //g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
+            //g.InterpolationMode = InterpolationMode.HighQualityBilinear;
+            //g.CompositingQuality = CompositingQuality.HighQuality;
+            g.CompositingQuality = CompositingQuality.HighQuality;
+            g.SmoothingMode = SmoothingMode.AntiAlias;
+            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
+            g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
+            g.PixelOffsetMode = PixelOffsetMode.Half;
+            //Font font = new Font(new FontFamily("宋体"), (float)(siez*1.3), FontStyle.Regular);
+            Font font = new Font(new FontFamily("微软雅黑"), (float)(size*1.2), FontStyle.Regular, GraphicsUnit.Point);
+            g.DrawString(str, font, Brushes.Black, new PointF((float)x * 4, (float)y * 4));
+        }
+
+        //图片添加条码
+        public static void DrawBarcode(string str, ref Bitmap bitmap, double x, double y, double width, double height)
+        {
+
+            QrCodeEncodingOptions options = new QrCodeEncodingOptions();
+            options.Margin = 1;
+            options.Width = (int)width * 4;
+            options.Height = (int)height * 4;
+            options.PureBarcode = true;
+            BarcodeWriter writer = new BarcodeWriter();
+            writer.Format = BarcodeFormat.CODE_128;
+            writer.Options = options;
+            Bitmap imgTemp = writer.Write(str);
+            //BarCode _Code = new BarCode();
+            //_Code.ValueFont = new Font(new FontFamily("黑体"), (float)(6 * 25.4 /18), FontStyle.Regular);
+            //_Code.Height = 6*4;
+            //Bitmap imgTemp = _Code.GetCodeImage("0012490202261807040001",
+            //BarCode.Encode.Code128A);
+            bitmap = (Bitmap)CombinImage(bitmap, imgTemp, (float)x, (float)y);
+        }
+
+
+
+        /// <summary>
+        /// 调用此函数后使此两种图片合并,类似相册,有个
+        /// 背景图,中间贴自己的目标图片
+        /// </summary>
+        /// <param name="imgBack">粘贴的源图片</param>
+        /// <param name="img">粘贴的目标图片</param>
+        public static Image CombinImage(Image imgBack, Image img, float x, float y)
+        {      //照片图片
+               //从指定的System.Drawing.Image创建新的System.Drawing.Graphics       
+            Graphics g = Graphics.FromImage(imgBack);
+            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
+            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
+            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
+            g.PixelOffsetMode = PixelOffsetMode.Half;
+            //g.DrawImage(imgBack, 0, 0, 148, 124);      // g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
+            //g.FillRectangle(System.Drawing.Brushes.Black, -50, -50, (int)212, ((int)203));//相片四周刷一层黑色边框,这里没有,需要调尺寸
+            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
+            g.DrawImage(img, x * 2-5, y * 4, img.Width, img.Height);
+            g.Save();
+            g.Dispose();
+            return imgBack;
+        }
     }
 }

+ 89 - 0
UAS_Web/tool/RequestHandler.cs

@@ -2,8 +2,11 @@
 using System;
 using System.Collections.Generic;
 using System.Drawing;
+using System.Drawing.Drawing2D;
+using System.Linq;
 using System.Text.RegularExpressions;
 using System.Windows.Forms;
+using System.Drawing.Printing;
 
 namespace UAS_Web.tool
 {
@@ -11,6 +14,13 @@ namespace UAS_Web.tool
     {
         public event Action<byte[]> NotifyMsg;
 
+        //打印图片集合
+        List<Bitmap> bitmaps = new List<Bitmap>();
+
+        //图片打印中间变量
+        int imagecount = 0;
+        int index = 0;
+
         string PrinterName = "";
 
         string PrintType = "";
@@ -39,6 +49,9 @@ namespace UAS_Web.tool
                 case "getPrintType.action":
                     filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
                     return filter;
+                case "vendorZplPrint.action":
+                    filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter;
+                    return filter;
                 default:
                     break;
             }
@@ -94,6 +107,7 @@ namespace UAS_Web.tool
                         //string PrintCode = data["exceptionInfo"].ToString();
                         //MessageBox.Show(PrintCode);
                     }
+              
                     break;
                 case "getPrintType.action":
                     filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
@@ -101,20 +115,95 @@ namespace UAS_Web.tool
                     data = ToDictionary(str);
                     PrintType = data["data"].ToString().Replace("\"", "");
                     break;
+                case "vendorZplPrint.action":
+                    filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter;
+                    str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray());
+                    data = ToDictionary(str);
+                    List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
+                    List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
+                    parameter = (List<Dictionary<string, object>>)data["parameter"];
+                    barcode = (List<Dictionary<string, object>>)data["barcode"];
+                    int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
+                    int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
+
+                    foreach (var item in barcode)
+                    {
+                        Bitmap bit = new Bitmap(la_width * 4, la_height * 4);
+
+
+                        using (Graphics g = Graphics.FromImage(bit))
+                        {
+                            g.CompositingQuality = CompositingQuality.HighQuality;
+                            g.SmoothingMode = SmoothingMode.HighQuality;
+                            g.InterpolationMode = InterpolationMode.HighQualityBicubic;   
+                            ////定义一枝画笔
+                            //Pen pen = new Pen(Color.Black);
+                            ////用这个画笔在左上角画一个矩形
+                            //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
+                            foreach (var itemc in item)
+                            {
+                                Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
+                                if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
+                                {
+                                    PrintHelper.Printstring(itemc.Value!=null?itemc.Value.ToString().Replace("\"", ""):" ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()));
+                                }
+                                else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
+                                {
+                                    PrintHelper.DrawBarcode(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", ref bit, double.Parse(Sidc["LP_LEFTRATE"].ToString()), double.Parse(Sidc["LP_TOPRATE"].ToString()), double.Parse(Sidc["LP_WIDTH"].ToString()), double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
+                                    PrintHelper.Printstring(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()) + double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
+                                }
+                            }
+                        }
+                        bitmaps.Add(bit);
+                    }
+                    imagecount = bitmaps.Count;
+                    ////设置打印机名称
+                    print.PrintDoc.PrintPage += PrintPage;
+                    print.PrintDoc.Print();
+                    break;
                 default:
                     break;
             }
         }
 
+        private void PrintPage(object sender, PrintPageEventArgs e)
+        {
+            Image i = bitmaps[index];
+            Graphics g = e.Graphics;
+            g.CompositingQuality = CompositingQuality.HighQuality;
+            g.SmoothingMode = SmoothingMode.HighQuality;
+            g.InterpolationMode = InterpolationMode.NearestNeighbor;
+            g.PixelOffsetMode = PixelOffsetMode.Half;
+            g.DrawImage(i, 0, -5, i.Width, i.Height);
+            if (index < imagecount - 1)
+            {
+                e.HasMorePages = true;
+                index++;
+            }
+            else
+            {
+                index = 0;
+                e.HasMorePages = false;
+                bitmaps.Clear();
+            }
+        }
+
         private void RequestHandler_Click(object sender, EventArgs e)
         {
             //设置打印机名称
             print.PrintDoc.PrinterSettings.PrinterName = print.Controls["Printer"].Text;
             PrinterName = print.Controls["Printer"].Text;
+            RadioButton PRXA = (RadioButton)print.Controls["PRXA"];
+            RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
             //获取矩阵图的分辨率
             Graphics gr = print.PrintDoc.PrinterSettings.CreateMeasurementGraphics();
             print.browser.FocusedFrame.ExecuteJavaScriptAsync("(function(value,value1){dpi=value,printType=value1})('" + gr.DpiX + "','" + PrintType + "')");
+            if (PRPIC.Checked) {
+                print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmPicturePrint').click();");
+            }
+            else
             print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmZplPrint').click();");
+
             print.Close();
         }
 

BIN
UAS_Web/tool/zxing.dll