1.前端用ListBox載入顯示多張圖片

  1. 1 <ListBox Name="lbHeadImages" Grid.Row="1" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
  2. 2 <ListBox.ItemTemplate>
  3. 3 <DataTemplate>
  4. 4 <Grid >
  5. 5 <Rectangle Fill="#fff">
  6. 6 <Rectangle.Effect>
  7. 7 <DropShadowEffect Opacity="0.5" ShadowDepth="0"/>
  8. 8 </Rectangle.Effect>
  9. 9 </Rectangle>
  10. 10 <StackPanel Margin="2">
  11. 11 <Image Stretch="UniformToFill" Width="100" Height="120" VerticalAlignment="Center" HorizontalAlignment="Center"
  12. 12 Source="{Binding Image,Mode=TwoWay,NotifyOnSourceUpdated=True}"
  13. 13 MouseLeftButtonDown="Image_MouseLeftButtonDown"></Image>
  14. 14 </StackPanel>
  15. 15 </Grid>
  16. 16 </DataTemplate>
  17. 17 </ListBox.ItemTemplate>
  18. 18 <ListBox.ItemsPanel>
  19. 19 <ItemsPanelTemplate>
  20. 20 <WrapPanel Name="wrapPanel" HorizontalAlignment="Stretch" />
  21. 21 <!--<UniformGrid Columns="6"></UniformGrid>-->
  22. 22 </ItemsPanelTemplate>
  23. 23 </ListBox.ItemsPanel>
  24. 24 </ListBox>

2.客戶端——選擇圖片(可選擇多張),前臺載入顯示

  1. 1 private void btnSelectBehindImage_Click(object sender, RoutedEventArgs e)
  2. 2 {
  3. 3 OpenFileDialog openFileDialog = new OpenFileDialog();
  4. 4 openFileDialog.Multiselect = true;
  5. 5 openFileDialog.Filter = "All Image Files|*.jpg;*.png";
  6. 6 if ((bool)openFileDialog.ShowDialog()) {
  7. 7 if (openFileDialog.OpenFile() != null) {
  8. 8 behindImages.Clear();
  9. 9 string[] fileNames = openFileDialog.FileNames;
  10. 10 List<FinishProRptCheckVM.ImageRow> items = new List<FinishProRptCheckVM.ImageRow>();
  11. 11 for (int i = 0; i < fileNames.Count(); i ++) {
  12. 12 string filePath = fileNames[i];
  13. 13 var size = new FileInfo(filePath).Length / 1024 /1024; //獲取檔案大小(M)
  14. 14 if (size >= 1) { MessageBox.Show("上傳圖片大小不能超過1M!");return; }
  15. 15 byte[] bytes = File.ReadAllBytes(filePath);
  16. 16 behindImages.Add(bytes);
  17. 17
  18. 18 FinishProRptCheckVM.ImageRow row = new FinishProRptCheckVM.ImageRow() { Image = new BitmapImage(new Uri(filePath))};
  19. 19 items.Add(row);
  20. 20 }
  21. 21 lbBehindImages.ItemsSource = items;
  22. 22 }
  23. 23 }
  24. 24 }

3.客戶端上傳圖片

  1. 1 private bool UploadImage(string bill_no)
  2. 2 {
  3. 3 //上傳伺服器資料夾
  4. 4 string url = "/api/qc/iqc/experimentrptservice/uploadfile";
  5. 5 var result = Framework.PostData(url, lstBytes1, behindImages, bill_no);
  6. 6 if (result.success && result.data)
  7. 7 {
  8. 8 return true;
  9. 9 }
  10. 10 else
  11. 11 {
  12. 12 return false;
  13. 13 }
  14. 14 }

4.服務端儲存圖片到伺服器資料夾

  1. 1 public static object UploadFile(List<byte[]> headerPhoto,List<byte[]> behindPhoto,string billNo) {
  2. 2 try
  3. 3 {
  4. 4 //獲取儲存檔案的資料夾路徑
  5. 5 string filePath = "D:\\image\\";
  6. 6 if (!System.IO.Directory.Exists(filePath))//資料夾不存在建立資料夾
  7. 7 {
  8. 8 Directory.CreateDirectory(filePath);
  9. 9 }
  10. 10 else {//資料夾存在,找對應實驗是否存在上傳過的圖片,存在則刪除
  11. 11 string[] strDataFiles = Directory.GetFiles(filePath);
  12. 12 var imageFiles = from a in strDataFiles where a.Contains(billNo) && (a.EndsWith(".png") || a.EndsWith(".jpg")) select a;
  13. 13 foreach (string imagePath in imageFiles) {
  14. 14 File.Delete(imagePath);
  15. 15 }
  16. 16 }
  17. 17 //儲存實驗前圖片
  18. 18 for (int i = 0; i < headerPhoto.Count; i++)//遍歷二進位制的陣列的陣列
  19. 19 {
  20. 20 string strRiQiWenJian = "實驗前" + i.ToString() + "_" + billNo + ".png";
  21. 21 string strBaoCunLuJing = "D:\\image\\" + strRiQiWenJian;
  22. 22 FileInfo fi = new FileInfo(strBaoCunLuJing);
  23. 23 FileStream fs;
  24. 24 fs = fi.OpenWrite();
  25. 25 fs.Write(headerPhoto[i], 0, headerPhoto[i].Length);
  26. 26 fs.Flush();
  27. 27 fs.Close();
  28. 28 }
  29. 29 return true;
  30. 30 }
  31. 31 catch
  32. 32 {
  33. 33 return false;
  34. 34 }
  35. 35 }

5.客戶端,進入介面,初始化從伺服器載入對應圖片顯示

  1. 1 public void LoadImages (){
  2. 2 string url = "/api/qc/iqc/experimentrptservice/loadimages?bill_no=" + ParamModel.BILL_NO;
  3. 3 var result = Framework.GetData(url);
  4. 4 if (result.success){
  5. 5 lstBytes1 = JsonConvert.DeserializeObject<List<byte[]>>(result.data.HeaderBytePic.ToString());
  6. 6 behindImages = JsonConvert.DeserializeObject<List<byte[]>>(result.data.BehindBytePic.ToString());
  7. 7
  8. 8 //載入實驗前圖片
  9. 9 List<FinishProRptCheckVM.ImageRow> headerImages = new List<FinishProRptCheckVM.ImageRow>();
  10. 10 for (int i = 0; i < lstBytes1.Count;i++) {
  11. 11 BitmapImage img = new BitmapImage();
  12. 12 img.BeginInit();
  13. 13 img.StreamSource = new MemoryStream(lstBytes1[i]);
  14. 14 img.EndInit();
  15. 15 //img.Freeze();
  16. 16 FinishProRptCheckVM.ImageRow row = new FinishProRptCheckVM.ImageRow() { Image = img };
  17. 17 headerImages.Add(row);
  18. 18 }
  19. 19 lbHeadImages.ItemsSource = headerImages;
  20. 20 }
  21. 21 }

6.效果圖