You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
308 lines
12 KiB
308 lines
12 KiB
using AForge.Controls;
|
|
using ShenTun.ImageCollection.Common;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Security.Policy;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ShenTun.ImageCollection.AForgeCamera
|
|
{
|
|
public partial class FrmImageList : Form
|
|
{
|
|
private RequestImageEdit requset;
|
|
private bool isLoad = false;
|
|
private string defalutImg = string.Empty;
|
|
public FrmImageList(RequestImageEdit requestImage)
|
|
{
|
|
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
|
|
SetStyle(ControlStyles.UserPaint, true);
|
|
SetStyle(ControlStyles.DoubleBuffer, true);
|
|
InitializeComponent();
|
|
requset = requestImage;
|
|
lvList.SmallImageList = smallList;
|
|
}
|
|
private void FrmImageList_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
//string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
//defalutImg = string.Format("{0}\\defalut.jpg", path);
|
|
//imageView1.loadImageAndInit(defalutImg, null);
|
|
InitCtrl();
|
|
DowloadFile();
|
|
if (this.lvList.Items.Count == 0) return;
|
|
int index = this.lvList.SelectedIndices.Count > 0 ?
|
|
this.lvList.SelectedIndices[0] : 0;
|
|
RequestImage image = lvList.Items[index].Tag as RequestImage;
|
|
ShowImage(image);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("载入图片异常:" + ex.Message);
|
|
}
|
|
}
|
|
private void InitCtrl()
|
|
{
|
|
lvList.Items.Clear();
|
|
lvList.Columns.Clear();
|
|
//lvList.ShowGroups = false;
|
|
lvList.View = View.Details;//详细信息
|
|
#region 列标头信息 添加
|
|
lvList.Columns.Add("文件名", 300, HorizontalAlignment.Left);
|
|
lvList.Columns.Add("修改日期", 300, HorizontalAlignment.Left);
|
|
lvList.Columns.Add("类型", 80, HorizontalAlignment.Left);
|
|
lvList.Columns.Add("大小", 60, HorizontalAlignment.Left);
|
|
#endregion
|
|
}
|
|
private void ShowImage(RequestImage requestImage)
|
|
{
|
|
if (requestImage == null) return;
|
|
|
|
SelectImageInfo imageInfo = new SelectImageInfo()
|
|
{
|
|
ImageSvr = requset.ImageSvr,
|
|
PeisApiUrl = requset.PeisApiUrl,
|
|
Token = requset.Token,
|
|
Image = requestImage
|
|
};
|
|
using (FileStream fileStream = File.Open(requestImage.localPathName, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
|
|
{
|
|
// 创建一个字节数组来保存FileStream的内容
|
|
byte[] fileBytes = new byte[fileStream.Length];
|
|
fileStream.Read(fileBytes, 0, fileBytes.Length);
|
|
|
|
// 使用字节数组创建MemoryStream
|
|
using (MemoryStream memoryStream = new MemoryStream(fileBytes))
|
|
{
|
|
imageEdit1.LoadImageAndInit(memoryStream, imageInfo);
|
|
}
|
|
}
|
|
}
|
|
private void DowloadFile()
|
|
{
|
|
if (string.IsNullOrEmpty(requset.ImageSvr))
|
|
{
|
|
MessageBox.Show("获取ImageSvr字段失败");
|
|
return;
|
|
}
|
|
if (requset.Images == null)
|
|
{
|
|
MessageBox.Show("获取Images字段失败");
|
|
return;
|
|
}
|
|
smallList.Images.Clear();
|
|
int index = 0;//定义索引
|
|
var objStr = requset.Images.ToString();
|
|
List<RequestImage> imgList=new List<RequestImage>();
|
|
if (objStr.Contains("["))
|
|
{
|
|
var array = JsonHelper.DeserializeObject<RequestImage[]>(objStr);
|
|
imgList=array.ToList();
|
|
}
|
|
else {
|
|
var obj = JsonHelper.DeserializeObject<RequestImage>(objStr);
|
|
imgList.Add(obj);
|
|
}
|
|
|
|
Uri uri = new Uri(requset.ImageSvr);
|
|
progressBar1.Visible = true;
|
|
foreach (RequestImage img in imgList) {
|
|
if (!File.Exists(img.localPathName)) {
|
|
int iPos=img.pictureFilename.LastIndexOf("/");
|
|
string localFilename= img.pictureFilename.Substring(iPos+1);
|
|
Uri uri2 = new Uri(uri, img.pictureFilename);
|
|
string url = uri2.ToString();
|
|
string dirPath= Path.GetDirectoryName(img.localPathName);
|
|
try
|
|
{
|
|
if (!Directory.Exists(dirPath))
|
|
{
|
|
Directory.CreateDirectory(dirPath);
|
|
}
|
|
if (string.IsNullOrEmpty(img.localPathName))
|
|
{
|
|
string filePath = ImageHelper.GetLoaclPath(requset.ImagePath, requset.AsbitemName);
|
|
string fileName = string.Format("{0}\\{1}", filePath, localFilename);
|
|
img.localPathName = fileName;
|
|
if (File.Exists(fileName))
|
|
{
|
|
File.Delete(fileName);
|
|
|
|
}
|
|
img.localPathName = fileName;
|
|
}
|
|
}
|
|
catch {
|
|
string filePath = ImageHelper.GetLoaclPath(requset.ImagePath, requset.AsbitemName);
|
|
if (!Directory.Exists(filePath))
|
|
{
|
|
Directory.CreateDirectory(filePath);
|
|
}
|
|
string fileName = string.Format("{0}\\{1}", filePath, localFilename);
|
|
img.localPathName = fileName;
|
|
if (File.Exists(fileName))
|
|
{
|
|
File.Delete(fileName);
|
|
|
|
}
|
|
img.localPathName = fileName;
|
|
}
|
|
|
|
|
|
|
|
DownloadFile(url, img.localPathName,this.progressBar1);
|
|
}
|
|
}
|
|
progressBar1.Visible = false;
|
|
InitCtrl();
|
|
foreach (RequestImage imge in imgList) {
|
|
using (FileStream image = new FileStream(imge.localPathName, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
|
|
{
|
|
Image img = Image.FromStream(image, false, true);
|
|
string PthName = Path.GetFileNameWithoutExtension(imge.localPathName);//获取图片的名称
|
|
smallList.Images.Add(PthName, img);//小图标显示
|
|
//dic.Add(index, imge.localPathName);//index 图片的索引 fpath 图片的路径
|
|
ListViewItem li = new ListViewItem();
|
|
li.ImageIndex = index;//获取图片的索引
|
|
li.Text = PthName;//获取图片的文本信息
|
|
li.Tag = imge;
|
|
li.SubItems.Add(File.GetCreationTime(imge.localPathName).ToString());//获取文件修改的日期
|
|
li.SubItems.Add(Path.GetExtension(imge.localPathName));//文件的类型
|
|
long length = new FileInfo(imge.localPathName).Length;//获取文件大小 字节
|
|
li.SubItems.Add((length / 1024).ToString());//KB
|
|
lvList.Items.Add(li);//添加到集合中显示
|
|
}
|
|
|
|
index++;
|
|
}
|
|
//lvList.GridLines = true;//显示网格
|
|
isLoad = true;
|
|
}
|
|
|
|
public void DownloadFile(string url, string filename, System.Windows.Forms.ProgressBar prog)
|
|
{
|
|
try
|
|
{
|
|
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
|
|
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
|
|
long totalBytes = myrp.ContentLength;
|
|
if (prog != null)
|
|
{
|
|
prog.Maximum = (int)totalBytes;
|
|
}
|
|
using (System.IO.Stream st = myrp.GetResponseStream())
|
|
{
|
|
using (System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.CreateNew))
|
|
{
|
|
long totalDownloadedByte = 0;
|
|
byte[] by = new byte[1024];
|
|
int osize = st.Read(by, 0, (int)by.Length);
|
|
while (osize > 0)
|
|
{
|
|
totalDownloadedByte = osize + totalDownloadedByte;
|
|
System.Windows.Forms.Application.DoEvents();
|
|
so.Write(by, 0, osize);
|
|
if (prog != null)
|
|
{
|
|
prog.Value = (int)totalDownloadedByte;
|
|
}
|
|
osize = st.Read(by, 0, (int)by.Length);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
|
|
private void btnLoad_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
btnLoad.Enabled = false;
|
|
DowloadFile();
|
|
}
|
|
catch (Exception ex) {
|
|
progressBar1.Visible = false;
|
|
MessageBox.Show("加载图片异常:" + ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
btnLoad.Enabled = true;
|
|
}
|
|
}
|
|
|
|
private void btnPre_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (this.lvList.Items.Count == 0) return;
|
|
int index = this.lvList.SelectedIndices.Count > 0 ?
|
|
this.lvList.SelectedIndices[0] - 1 : 0;
|
|
if (index >= 0 && index < this.lvList.Items.Count)
|
|
{
|
|
this.lvList.SelectedIndices.Clear();
|
|
this.lvList.SelectedIndices.Add(index);
|
|
this.lvList.Items[index].Focused = true;
|
|
RequestImage image= lvList.Items[index].Tag as RequestImage;
|
|
ShowImage(image);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("加载上一张异常:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
private void btnNext_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (this.lvList.Items.Count == 0) return;
|
|
int index = this.lvList.SelectedIndices.Count > 0 ?
|
|
this.lvList.SelectedIndices[0] + 1 : 0;
|
|
if (index >= 0 && index < this.lvList.Items.Count)
|
|
{
|
|
this.lvList.SelectedIndices.Clear();
|
|
this.lvList.SelectedIndices.Add(index);
|
|
this.lvList.Items[index].Focused = true;
|
|
RequestImage image = lvList.Items[index].Tag as RequestImage;
|
|
ShowImage(image);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("加载下一张异常:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
private void lvList_DoubleClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (this.lvList.Items.Count == 0) return;
|
|
int index = this.lvList.SelectedIndices.Count > 0 ?
|
|
this.lvList.SelectedIndices[0] : 0;
|
|
RequestImage image = lvList.Items[index].Tag as RequestImage;
|
|
ShowImage(image);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("双击加载图片异常:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|