1.前端用ListBox載入顯示多張圖片
- 1 <ListBox Name="lbHeadImages" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
- 2 <ListBox.ItemTemplate>
- 3 <DataTemplate>
- 4 <Grid >
- 5 <Rectangle Fill="#fff">
- 6 <Rectangle.Effect>
- 7 <DropShadowEffect Opacity="0.5" ShadowDepth="0"/>
- 8 </Rectangle.Effect>
- 9 </Rectangle>
- 10 <StackPanel Margin="2">
- 11 <Image Stretch="UniformToFill" Width="100" Height="120" VerticalAlignment="Center" HorizontalAlignment="Center"
- 12 Source="{Binding Image,Mode=TwoWay,NotifyOnSourceUpdated=True}"
- 13 MouseLeftButtonDown="Image_MouseLeftButtonDown"></Image>
- 14 </StackPanel>
- 15 </Grid>
- 16 </DataTemplate>
- 17 </ListBox.ItemTemplate>
- 18 <ListBox.ItemsPanel>
- 19 <ItemsPanelTemplate>
- 20 <WrapPanel Name="wrapPanel" HorizontalAlignment="Stretch" />
- 21 <!--<UniformGrid Columns="6"></UniformGrid>-->
- 22 </ItemsPanelTemplate>
- 23 </ListBox.ItemsPanel>
- 24 </ListBox>
2.客戶端——選擇圖片(可選擇多張),前臺載入顯示
- 1 private void btnSelectBehindImage_Click(object sender, RoutedEventArgs e)
- 2 {
- 3 OpenFileDialog openFileDialog = new OpenFileDialog();
- 4 openFileDialog.Multiselect = true;
- 5 openFileDialog.Filter = "All Image Files|*.jpg;*.png";
- 6 if ((bool)openFileDialog.ShowDialog()) {
- 7 if (openFileDialog.OpenFile() != null) {
- 8 behindImages.Clear();
- 9 string[] fileNames = openFileDialog.FileNames;
- 10 List<FinishProRptCheckVM.ImageRow> items = new List<FinishProRptCheckVM.ImageRow>();
- 11 for (int i = 0; i < fileNames.Count(); i ++) {
- 12 string filePath = fileNames[i];
- 13 var size = new FileInfo(filePath).Length / 1024 /1024; //獲取檔案大小(M)
- 14 if (size >= 1) { MessageBox.Show("上傳圖片大小不能超過1M!");return; }
- 15 byte[] bytes = File.ReadAllBytes(filePath);
- 16 behindImages.Add(bytes);
- 17
- 18 FinishProRptCheckVM.ImageRow row = new FinishProRptCheckVM.ImageRow() { Image = new BitmapImage(new Uri(filePath))};
- 19 items.Add(row);
- 20 }
- 21 lbBehindImages.ItemsSource = items;
- 22 }
- 23 }
- 24 }
3.客戶端上傳圖片
- 1 private bool UploadImage(string bill_no)
- 2 {
- 3 //上傳伺服器資料夾
- 4 string url = "/api/qc/iqc/experimentrptservice/uploadfile";
- 5 var result = Framework.PostData(url, lstBytes1, behindImages, bill_no);
- 6 if (result.success && result.data)
- 7 {
- 8 return true;
- 9 }
- 10 else
- 11 {
- 12 return false;
- 13 }
- 14 }
4.服務端儲存圖片到伺服器資料夾
- 1 public static object UploadFile(List<byte[]> headerPhoto,List<byte[]> behindPhoto,string billNo) {
- 2 try
- 3 {
- 4 //獲取儲存檔案的資料夾路徑
- 5 string filePath = "D:\\image\\";
- 6 if (!System.IO.Directory.Exists(filePath))//資料夾不存在建立資料夾
- 7 {
- 8 Directory.CreateDirectory(filePath);
- 9 }
- 10 else {//資料夾存在,找對應實驗是否存在上傳過的圖片,存在則刪除
- 11 string[] strDataFiles = Directory.GetFiles(filePath);
- 12 var imageFiles = from a in strDataFiles where a.Contains(billNo) && (a.EndsWith(".png") || a.EndsWith(".jpg")) select a;
- 13 foreach (string imagePath in imageFiles) {
- 14 File.Delete(imagePath);
- 15 }
- 16 }
- 17 //儲存實驗前圖片
- 18 for (int i = 0; i < headerPhoto.Count; i++)//遍歷二進位制的陣列的陣列
- 19 {
- 20 string strRiQiWenJian = "實驗前" + i.ToString() + "_" + billNo + ".png";
- 21 string strBaoCunLuJing = "D:\\image\\" + strRiQiWenJian;
- 22 FileInfo fi = new FileInfo(strBaoCunLuJing);
- 23 FileStream fs;
- 24 fs = fi.OpenWrite();
- 25 fs.Write(headerPhoto[i], 0, headerPhoto[i].Length);
- 26 fs.Flush();
- 27 fs.Close();
- 28 }
- 29 return true;
- 30 }
- 31 catch
- 32 {
- 33 return false;
- 34 }
- 35 }
5.客戶端,進入介面,初始化從伺服器載入對應圖片顯示
- 1 public void LoadImages (){
- 2 string url = "/api/qc/iqc/experimentrptservice/loadimages?bill_no=" + ParamModel.BILL_NO;
- 3 var result = Framework.GetData(url);
- 4 if (result.success){
- 5 lstBytes1 = JsonConvert.DeserializeObject<List<byte[]>>(result.data.HeaderBytePic.ToString());
- 6 behindImages = JsonConvert.DeserializeObject<List<byte[]>>(result.data.BehindBytePic.ToString());
- 7
- 8 //載入實驗前圖片
- 9 List<FinishProRptCheckVM.ImageRow> headerImages = new List<FinishProRptCheckVM.ImageRow>();
- 10 for (int i = 0; i < lstBytes1.Count;i++) {
- 11 BitmapImage img = new BitmapImage();
- 12 img.BeginInit();
- 13 img.StreamSource = new MemoryStream(lstBytes1[i]);
- 14 img.EndInit();
- 15 //img.Freeze();
- 16 FinishProRptCheckVM.ImageRow row = new FinishProRptCheckVM.ImageRow() { Image = img };
- 17 headerImages.Add(row);
- 18 }
- 19 lbHeadImages.ItemsSource = headerImages;
- 20 }
- 21 }
6.效果圖