重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关如何从服务端下载到本地图片并在Image控件中显示,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联公司服务紧随时代发展步伐,进行技术革新和技术进步,经过十载的发展和积累,已经汇集了一批资深网站策划师、设计师、专业的网站实施团队以及高素质售后服务人员,并且完全形成了一套成熟的业务流程,能够完全依照客户要求对网站进行成都网站建设、网站建设、建设、维护、更新和改版,实现客户网站对外宣传展示的首要目的,并为客户企业品牌互联网化提供全面的解决方案。
根据图片名查询服务端的三种图片,再依次显示在对应的Image控件上。
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using DevExpress.XtraReports.UI; using System.Data; using TRSCore; using System.Text; using MESCore; using DevExpress.DataAccess.EntityFramework; using System.Collections.Generic; using System.Windows.Forms; using System.IO; using Neodynamic.SDK.Printing; namespace QCMCore.Reports { public partial class rptQCMCOACard : DevExpress.XtraReports.UI.XtraReport { public rptQCMCOACard() { InitializeComponent(); } public rptQCMCOACard(DataTable dt,string sBatchID,string sModelID) { InitializeComponent(); this.dtOrd = dt; this.sBatchID = sBatchID; this.sModelID = sModelID; } DataTable dtOrd = new DataTable(); DataSet ds = new DataSet(); string sBatchID = ""; string sModelID = ""; string sImage1 = ""; string sImage2 = ""; string sImage3 = ""; private bool View_Iamges() { TRSNode in_node = new TRSNode("View_Label_In"); TRSNode out_node = new TRSNode("View_Label_Out"); try { sImage1 = "图片1-1.png"; sImage2 = "图片3-1.png"; sImage3 = "图片2.png"; ListImagelist = new List (); List xrImagelist = new List (); xrImagelist.Add(xrPictureBox1); xrImagelist.Add(xrPictureBox2); xrImagelist.Add(xrPictureBox3); Imagelist.Add(sImage1); Imagelist.Add(sImage2); Imagelist.Add(sImage3); if (Imagelist != null && Imagelist.Count > 0) { for (int i = 0; i < Imagelist.Count; i++) { if (Imagelist[i] != "") { //open a thermal label template // string filePath = Application.StartupPath + "\\UserFile\\Label\\" + sLastSaveFile; #region 服务器文件下载 in_node = new TRSNode("View_File_In"); out_node = new TRSNode("Cmn_Out"); CommonRoutine.SetInMsg(in_node); in_node.ProcStep = '2'; in_node.AddString("FILENAME", CommonFunction.Trim(Imagelist[i])); if (CommonRoutine.CallService("BAS", "BAS_View_File", in_node, ref out_node) == false) { return false; } #endregion byte[] bytearay = CommonFunction.RemoveByteOrderMark(out_node.GetBlob("FILEDATA")); if (bytearay != null) { MemoryStream ms_buffer; try { ms_buffer = new MemoryStream(); //对于要重复使用缓存而不是保存到本地再引用,避免报错:图片已被另一进程中使用 ms_buffer.Write(bytearay, 0, bytearay.Length); ms_buffer.Position = 0; xrImagelist[i].Image = null; // xrImagelist[i].Image = Image.FromStream(ms_buffer); string filedata = System.Text.Encoding.Default.GetString(bytearay); } catch (Exception ex) { CommonFunction.ShowMsgBox(ex.Message); } } } } return true; } else { return false; } } catch (Exception ex) { CommonFunction.ShowMsgBox(ex.Message); return false; } } private bool FillHeader() { try { if (dtOrd == null) { CommonFunction.ShowMsgBox("请先选择需要打印的ITEM!"); return false; } this.xrtBatchID.Text = sBatchID; this.xrtModel.Text = sModelID; if (View_Iamges()==false ) { CommonFunction.ShowMsgBox("图片找不到!"); return false; } //this.xrPictureBox1.ImageUrl = Application.StartupPath + "\\UserFile\\Images\\图片1-1.png"; //this.xrPictureBox2.ImageUrl = Application.StartupPath + "\\UserFile\\Images\\图片3-1.png"; //this.xrPictureBox3.ImageUrl = Application.StartupPath + "\\UserFile\\Images\\图片2.png"; this.xrtDateTime.Text = DateTime.Now.ToShortDateString().ToString(); // 20018-9-4 if (dtOrd.Rows.Count > 0) { ds.Clear(); ds.Tables.Add(dtOrd.Copy()); //使用Copy的Table,以免报错:DataTable已存在另一个DataSet XtraReport report = new XtraReport(); report.DataSource = ds; report.DataMember = "queryProducts"; List list = new List (); for (int i = 0; i < dtOrd.Rows.Count;i++) { DataTable dt = new DataTable(); dt.Columns.Add("ITEM_SEQ"); dt.Columns.Add("TESTING_ITEM"); dt.Columns.Add("TESTING_RESULT"); dt.Columns.Add("TESTING_SPECIFICATION"); dt.Columns.Add("DIMENS"); dt.Columns.Add("TESTING_METHOD"); dt.Rows.Add(dtOrd.Rows[i].ItemArray); list.Add(dt); } XRTable table = new XRTable(); for (int i = 0; i < dtOrd.Rows.Count; i++) { if (list[i]!=null) { XRTableRow row = new XRTableRow(); XRTableCell TESTING_ITEM = new XRTableCell(); XRTableCell TESTING_RESULT = new XRTableCell(); XRTableCell TESTING_SPECIFICATION = new XRTableCell(); XRTableCell DIMENS = new XRTableCell(); XRTableCell TESTING_METHOD = new XRTableCell(); row.Cells.Add(TESTING_ITEM); row.Cells.Add(TESTING_RESULT); row.Cells.Add(TESTING_SPECIFICATION); row.Cells.Add(DIMENS); row.Cells.Add(TESTING_METHOD); //TESTING_ITEM.WidthF = 140.14f; //TESTING_RESULT.WidthF = 140.14f; //TESTING_SPECIFICATION.WidthF = 140.14f; //DIMENS.WidthF = 140.14f; //TESTING_METHOD.WidthF = 140.14f; TESTING_ITEM.DataBindings.Add("Text", list[i], "TESTING_ITEM"); TESTING_RESULT.DataBindings.Add("Text", list[i], "TESTING_RESULT"); TESTING_SPECIFICATION.DataBindings.Add("Text", list[i], "TESTING_SPECIFICATION"); DIMENS.DataBindings.Add("Text", list[i], "DIMENS"); TESTING_METHOD.DataBindings.Add("Text", list[i], "TESTING_METHOD"); xrTable7.Rows.Add(row); //xrTable7.InsertRowBelow(xrTable7.Rows[i]); } } DetailBand detailBand = new DetailBand(); detailBand.Controls.Add(table); report.Bands.Add(detailBand); } return true; } catch (Exception ex) { CommonFunction.ShowMsgBox(ex.Message); return false; } } private void ReportHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { FillHeader(); } } }
=========初试代码有bug
private bool View_Iamges() { TRSNode in_node = new TRSNode("View_Label_In"); TRSNode out_node = new TRSNode("View_Label_Out"); try { sImage1 = "图片1-1.png"; sImage2 = "图片3-1.png"; sImage3 = "图片2.png"; ListImagelist = new List (); Imagelist.Add(sImage1); Imagelist.Add(sImage2); Imagelist.Add(sImage3); if (Imagelist != null && Imagelist.Count > 0) { for (int i = 0; i < Imagelist.Count; i++) { if (Imagelist[i] != "") { //open a thermal label template // string filePath = Application.StartupPath + "\\UserFile\\Label\\" + sLastSaveFile; #region 服务器文件下载 in_node = new TRSNode("View_File_In"); out_node = new TRSNode("Cmn_Out"); CommonRoutine.SetInMsg(in_node); in_node.ProcStep = '2'; //查询对应的文件夹 in_node.AddString("FILENAME", CommonFunction.Trim(Imagelist[i])); if (CommonRoutine.CallService("BAS", "BAS_View_File", in_node, ref out_node) == false) { return false; } #endregion byte[] bytearay = CommonFunction.RemoveByteOrderMark(out_node.GetBlob("FILEDATA")); if (bytearay != null) { string filedata = System.Text.Encoding.Default.GetString(bytearay); //保存Label文件到本地,进行编辑 string filePath = Application.StartupPath + "\\UserFile\\Images\\" + Imagelist[i]; File.WriteAllBytes(filePath, bytearay); string picPath = filePath.Substring(0, filePath.Length - 4) + ".png";//本地Label图片路径 if (!File.Exists(picPath))//如果图片不存在,生成 { ThermalLabel tl = ThermalLabel.CreateFromXmlTemplate(filedata); //Display ThermalLabel as a TIFF image if (tl != null) { //替换DataField,使其显示 foreach (Item item in tl.Items) { if (item.DataField != "") { item.DataField = "";//必须置为空,否则无法显示 } } using (PrintJob pj = new PrintJob()) { pj.ThermalLabel = tl; pj.Copies = 1; MemoryStream ms = new MemoryStream(); ImageSettings imgSett = new ImageSettings(); imgSett.ImageFormat = Neodynamic.SDK.Printing.ImageFormat.Png; //imgSett.PixelFormat = PixelFormat.BGRA32; imgSett.AntiAlias = true; //imgSett.TransparentBackground = true; pj.ExportToImage(picPath, imgSett, 300); } } } //在对应的控件中显示 if (i == 0) xrPictureBox1.Image = Image.FromFile(picPath); if (i == 1) xrPictureBox2.Image = Image.FromFile(picPath); if (i == 2) xrPictureBox3.Image = Image.FromFile(picPath); } } } return true; } return false; } catch (Exception ex) { CommonFunction.ShowMsgBox(ex.Message); return false; } }
注:
1、当所用的图片需要被重复引用时,将图片从服务端下载至本地再引用的做法容易报错:图片正由另一个进程使用。
解决方法,一是使用File.WriteAllBytes()方法时,记得释放资源。
二是使用内存的方式直接将图片填充至控件上,而不是将图片下载至客户端,再填充。
MemoryStream ms_buffer; try { ms_buffer = new MemoryStream(); //对于要重复使用缓存而不是保存到本地再引用,避免报错:图片已被另一进程中使用 ms_buffer.Write(bytearay, 0, bytearay.Length); ms_buffer.Position = 0; xrImagelist[i].Image = null; // xrImagelist[i].Image = Image.FromStream(ms_buffer); string filedata = System.Text.Encoding.Default.GetString(bytearay); } catch (Exception ex) { CommonFunction.ShowMsgBox(ex.Message); }
2、当传入的DataTable被重复使用时,容易报错:DataTable已属于另一个DataSet。
解决方法就是添加克隆表或复制的表到DataSet中。
ds.Tables.Add(dtOrd.Copy());
关于如何从服务端下载到本地图片并在Image控件中显示就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。