這篇文章是我在之前那篇文章上的深入,可以獲取到列表後,在右邊將圖片顯示出來,並添加了上傳功能,好啦,不多說了,代碼如下
首先是winform的佈局代碼
namespace TreeviewTest
{
partial class Form1
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應釋放託管資源,爲 true;否則爲 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(13, 13);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(217, 415);
this.treeView1.TabIndex = 0;
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Location = new System.Drawing.Point(237, 42);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(559, 386);
this.panel1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(237, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "全選";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(331, 13);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "反選";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(721, 12);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 4;
this.button3.Text = "上傳";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(808, 440);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
接下是後臺功能實現的代碼
namespace TreeviewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CpuMessage();
this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
}
public void CpuMessage()
{
ImageList imglist = new ImageList();
imglist.Images.Add(Image.FromFile(Application.StartupPath + "//iamges//1.jpg"));
imglist.Images.Add(Image.FromFile(Application.StartupPath + "//iamges//2.jpg"));
imglist.Images.Add(Image.FromFile(Application.StartupPath + "//iamges//3.jpg"));
this.treeView1.ImageList = imglist;
DriveInfo[] dr = DriveInfo.GetDrives();
string driveName = "";
foreach (DriveInfo d in dr)
{
TreeNode tn = new TreeNode();
tn.Name = d.Name;
switch (d.DriveType)
{
case DriveType.Fixed:
tn.ImageIndex = 0;
driveName = "本地磁盤(" + d.Name.Substring(0,2) + ")";
break;
case DriveType.Removable:
tn.ImageIndex = 0;
driveName = "可移動磁盤(" + d.Name.Substring(0, 2) + ")";
break;
case DriveType.CDRom:
tn.ImageIndex = 1;
driveName = "DVD驅動器(" + d.Name.Substring(0, 2) + ")";
break;
case DriveType.Network:
driveName = "網絡驅動器(" + d.Name.Substring(0, 2) + ")";
break;
default :
driveName = "未知(" + d.Name + ")";
break;
}
tn.Text = driveName;
this.treeView1.Nodes.Add(tn);
}
}
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Nodes.Count > 0)
{
if (e.Node.IsExpanded)
{
e.Node.Collapse();
}
else
{
e.Node.Expand();
}
}
else
{
if (Directory.Exists(e.Node.Name))
{
panel1.Controls.Clear();
try
{
string[] allDirectory = Directory.GetDirectories(e.Node.Name);
foreach (string s in allDirectory)
{
TreeNode tn = new TreeNode();
tn.Name = s;
tn.Text = s.Remove(0, s.LastIndexOf("//") + 1);
tn.ImageIndex = 2;
e.Node.Nodes.Add(tn);
}
List<string> allFiles = Directory.GetFiles(e.Node.Name,"*.jpg").ToList<string>();
allFiles.AddRange(Directory.GetFiles(e.Node.Name, "*.gif").ToList<string>());
int numindex = 1;
foreach (string sf in allFiles)
{
ImgButton ib = new ImgButton();
PictureBox pb = new PictureBox();
Image img = Bitmap.FromFile(sf);
pb.Image = img;
pb.SizeMode = PictureBoxSizeMode.CenterImage;
pb.BorderStyle = BorderStyle.FixedSingle;
ib.pictureBox.Image = img;
ib.pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
ib.Name = "picselect";
if (numindex % 4 == 0)
{
ib.Location = new Point(ib.Width * (numindex % 4 + 3) + 20 * 4, ib.Height * (numindex / 4 - 1) + 20 * (numindex / 4 - 1));
}
else
{
ib.Location = new Point(ib.Width * (numindex % 4 - 1) + 20 * (numindex % 4), ib.Height * (numindex / 4) + 20 * (numindex / 4));
}
ib.label.Text = sf.Remove(0, sf.LastIndexOf("//") + 1);
ib.path.Text = sf;
panel1.Controls.Add(ib);
numindex++;
}
}
catch
{
}
}
e.Node.Expand();
}
}
private void button3_Click(object sender, EventArgs e)
{
List<Control> conlist = this.panel1.Controls.Find("picselect", true).ToList<Control>();
foreach (Control ctr in conlist)
{
ImgButton imgButton = ctr as ImgButton;
if (imgButton.checkBox.Checked)
{
FileUpload(imgButton.path.Text, @"F:/Test/myTestWeb/uploads/", imgButton.label.Text);
}
}
}
void FileUpload(string localPathFile, string webPhysicalPathFile, string filename)
{
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
FileStream fs = new FileStream(localPathFile, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(webPhysicalPathFile + filename, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();
}
catch
{
MessageBox.Show("文件上傳失敗");
}
}
private void button1_Click(object sender, EventArgs e)
{
List<Control> conlist = this.panel1.Controls.Find("picselect", true).ToList<Control>();
foreach (Control ctr in conlist)
{
ImgButton imgButton = ctr as ImgButton;
imgButton.checkBox.Checked = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
List<Control> conlist = this.panel1.Controls.Find("picselect", true).ToList<Control>();
foreach (Control ctr in conlist)
{
ImgButton imgButton = ctr as ImgButton;
imgButton.checkBox.Checked = false;
}
}
}
}
這樣就是實現了本地圖片批量上傳的功能,大家再結合那篇c# active控件開發的技術,就能完成圖片上傳控件了