您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何從服務(wù)端下載到本地圖片并在Image控件中顯示,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
根據(jù)圖片名查詢服務(wù)端的三種圖片,再依次顯示在對應(yīng)的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"; List<string> Imagelist = new List<string>(); List<XRPictureBox> xrImagelist = new List<XRPictureBox>(); 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 服務(wù)器文件下載 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(); //對于要重復(fù)使用緩存而不是保存到本地再引用,避免報錯:圖片已被另一進(jìn)程中使用 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<DataTable> list = new List<DataTable>(); 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"; List<string> Imagelist = new List<string>(); 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 服務(wù)器文件下載 in_node = new TRSNode("View_File_In"); out_node = new TRSNode("Cmn_Out"); CommonRoutine.SetInMsg(in_node); in_node.ProcStep = '2'; //查詢對應(yīng)的文件夾 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文件到本地,進(jìn)行編輯 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); } } } //在對應(yīng)的控件中顯示 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、當(dāng)所用的圖片需要被重復(fù)引用時,將圖片從服務(wù)端下載至本地再引用的做法容易報錯:圖片正由另一個進(jìn)程使用。
解決方法,一是使用File.WriteAllBytes()方法時,記得釋放資源。
二是使用內(nèi)存的方式直接將圖片填充至控件上,而不是將圖片下載至客戶端,再填充。
MemoryStream ms_buffer; try { ms_buffer = new MemoryStream(); //對于要重復(fù)使用緩存而不是保存到本地再引用,避免報錯:圖片已被另一進(jìn)程中使用 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、當(dāng)傳入的DataTable被重復(fù)使用時,容易報錯:DataTable已屬于另一個DataSet。
解決方法就是添加克隆表或復(fù)制的表到DataSet中。
ds.Tables.Add(dtOrd.Copy());
關(guān)于如何從服務(wù)端下載到本地圖片并在Image控件中顯示就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。