using BBWY.Client.Extensions; using BBWY.Client.Helpers; using BBWY.Client.Models; using BBWY.Client.ViewModels.PackTask; using BBWY.Controls; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Linq; using System.Net.Http; using System.Printing; using System.Reflection; using System.Security.Policy; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using WebSocketSharp; namespace BBWY.Client.Views.PackTask { /// /// PackDetailWindow.xaml 的交互逻辑 /// public partial class PackDetailWindow : BWindow { public PackDetailWindow(PackTaskModel model, Action reflashWindow) { InitializeComponent(); var serviceViewModel = this.DataContext as PackDetailViewModel; if (model.Brand.IsNullOrEmpty()) { if (model.BarCodeModel!=null&&!model.BarCodeModel.Brand.IsNullOrEmpty()) model.Brand = model.BarCodeModel.Brand; else if (model.CertificateModel != null && model.CertificateModel.Any(c => c.Brand != null)) model.Brand = model.CertificateModel.FirstOrDefault(c => c.Brand != null)?.Brand; } if (model.BrandName.IsNullOrEmpty()) { if (model.BarCodeModel != null && !model.BarCodeModel.BrandName.IsNullOrEmpty()) model.BrandName = model.BarCodeModel.BrandName; else if (model.CertificateModel != null && model.CertificateModel.Any(c => c.BrandName != null)) model.BrandName = model.CertificateModel.FirstOrDefault(c => c.BrandName != null)?.BrandName; } if (model.BarCodeModel != null) { model.BarCodeModel.ShopName = model.ShopName; } serviceViewModel.PackTaskModel = model; serviceViewModel.PackTaskList = new System.Collections.ObjectModel.ObservableCollection { model }; serviceViewModel.FloorDragNumber = model.FloorDragNumber; serviceViewModel.TaskCount = model.SkuCount; serviceViewModel.TaskId = model.TaskId; serviceViewModel.OrderId = model.OrderId; serviceViewModel.SkuId = model.SkuId; serviceViewModel.PackUserName = model.PackUser?.Replace("\r\n", ",")?.Replace("\n", ",")?.Replace("\r", ","); serviceViewModel.LoadPackDatas(); if (reflashWindow != null) serviceViewModel.ReflashWindow = reflashWindow; // DownloadImags(model.ItemList[0].Logo.Replace("80x80", "500x500")); LoadImage(model.ItemList[0].Logo.Replace("80x80", "200x200")); } //PackTaskModel model = new PackTaskModel(); ///// ///// 图片 ///// //public BitmapImage LogoImage { get; set; } private void BButton_Click(object sender, RoutedEventArgs e) { //PrintPackTaskDetail printWindow = new PrintPackTaskDetail(model); //if (printWindow==null) //{ // MessageBox.Show("页面加载中,请重试!"); // return; //} string printName = cbPrintName.Text.Trim(); PrintBox(printName); var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string printNames = System.IO.Path.Combine(applicationPath, "printName.init"); try { if (File.Exists(printNames)) { File.Delete(printNames); } } catch { } File.WriteAllText(printNames, cbPrintName.Text); return; } public void PrintBox(string printName) { //PrintData(1, printName, model); //return; var localPrintServer = new LocalPrintServer(); if (string.IsNullOrEmpty(printName)) { return; } var printQueue = localPrintServer.GetPrintQueue(printName); if (printQueue.IsInError) { System.Windows.MessageBox.Show("打印机处于错误状态"); return; } MyPrintHelper.SetDefaultPrint(printName);//设置默认打印机 PrintDialog printDialog = new PrintDialog(); printDialog.PrintTicket.PageOrientation = PageOrientation.ReversePortrait;//设置为横向打印 PageOrientation.Landscape Portrait为纵向 //设置纸张大小 var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth); var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight); printDialog.PrintTicket.PageMediaSize = new PageMediaSize(pageWidth, pageHeight); print_box.Height = pageHeight; print_box.Width = pageWidth; this.print_box.Arrange(new Rect(new System.Windows.Point(0, 0), new System.Windows.Size(pageWidth, pageHeight))); this.UpdateLayout(); //刷新界面 printDialog.PrintVisual(this.print_box, "打印任务"); this.Close(); } private async void LoadImage(string imageUrl) { try { using (HttpClient client = new HttpClient()) { // 发送 HTTP 请求并获取图片数据 var uri = new Uri(Uri.EscapeUriString(imageUrl)); byte[] imageData = await client.GetByteArrayAsync(uri); // 创建 BitmapImage 对象并设置图片数据 // 在 UI 线程上更新 Image 控件的 Source 属性 var LogoImage = imageData.ByteToBitmapImage(); img_logo.Source = LogoImage; } } catch (Exception ex) { // 处理加载图片失败的异常 Console.WriteLine("加载图片失败:" + ex.Message); } } private void DownloadImags(string url) { var client = new HttpClient(); var uri = new Uri(Uri.EscapeUriString(url)); byte[] urlContents = client.GetByteArrayAsync(uri).Result; var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string imagePath = System.IO.Path.Combine(applicationPath, "123.bmp"); if (File.Exists(imagePath)) { File.Delete(imagePath); } using (System.IO.FileStream fs = new System.IO.FileStream(imagePath, System.IO.FileMode.CreateNew)) { fs.Write(urlContents, 0, urlContents.Length); } BitmapImage img = new BitmapImage(new Uri(imagePath)); img_logo.Source = img; this.UpdateLayout(); //刷新界面 } private void PrintData(int printCount, string printName, PackTaskModel model) { try { PrintDocument document = new PrintDocument(); document.PrinterSettings.PrinterName = printName;//使用打印机名称,指定特定的打印机进行打印。 //设置打印页面 //document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 236, 157); document.PrintPage += (s, a) => { Font font = new Font("宋体", 6, System.Drawing.FontStyle.Regular); MyPrintHelper.PrintPackDetail(model, ref a, font); }; document.PrinterSettings.Copies = (short)printCount;//打印份数 document.Print(); } catch (Exception ex) { App.Current.Dispatcher.Invoke(() => { new TipsWindow($"打印失败,{ex.Message}").Show(); }); } } } }