[C#] Ứng dụng duyệt file và xem ảnh

Hiện tại, vì công việc quá bận rộn nên mình không còn thời gian để post bài và duy trì nội dung cho blog nữa. Do đó tại thời điểm này, mình quyết định ngừng phát triển blog. Mọi bài viết sẽ vẫn được lưu trữ và mình sẽ cố gắng hỗ trợ tất cả các bạn khi có comment hỏi. Cảm ơn các bạn đã ủng hộ blog suốt thời gian qua !
Ví dụ trong bài viết này sẽ hướng dẫn cho các bạn:
  • Cách xây dựng một cây thư mục các ổ đĩa
  • Trình duyệt ảnh

Xây dựng cây thư mục:


public void BuildTree() 

{ 

string[] drives = Directory.GetLogicalDrives(); 

TreeNode node = null; 

foreach (string drv in drives) 

{ 

node = new TreeNode(drv); 

folderTreeView.Nodes.Add(node); 

node.Nodes.Add(“TempForExpandedEvent”);//dùng để cho phép mở sau đó gọi sự kiện before expanded 

} 

}


Hàm này sẽ nạp các ổ đĩa của hệ thống và một “nút nhử” để có thể khi mở rộng node thì sẽ load danh sách các thư mục con. Ở đây chúng ta không xây dựng sẵn cây thư mục mà chỉ load những thư mục cần thiết mà thôi.

Nếu các bạn muốn xây dựng cây thư mục sẵn thì phương thức sau sẽ làm được điều đó. Tuy nhiên tốc độ rất chậm và chiếm nhiều bộ nhớ do chương trình phải xây dựng toàn bộ cấu trúc cây thư mục của tất cả các ổ đĩa


private void ExploreDirectory(DirectoryInfo dir,TreeNode node) 

{ 

DirectoryInfo[] directories = dir.GetDirectories(); 

foreach (DirectoryInfo newDir in directories) 

{ 

TreeNode x = new TreeNode(newDir.Name); 

node.Nodes.Add(x); 

try{ 

ExploreDirectory(newDir, x); 

} 

catch (Exception){} 

} 

}


Như vậy, khi mở một thư mục nào đó, chương trình sẽ nạp tiếp cấp của thư mục được chọn mà thôi:


private void folderTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) 

{ 

TreeNode node = e.Node; 

node.Nodes.Clear(); 

Adddir(node); 

} 

void Adddir(TreeNode node) 

{ 

string path = node.FullPath; 

try 

{ 

foreach (string dir in Directory.GetDirectories(path)) 

{ 

TreeNode n = node.Nodes.Add(Path.GetFileName(dir)); 

n.Nodes.Add(“TempForExpandedEvent”); 

} 

} 

catch { } 

}


Cứ tiếp tục như vậy ta sẽ xây dựng được 1 cây thư mục “open on demand”.


Trình duyệt ảnh


Ta sử dụng control FlowLayerPanel để nạp tất cả các hình ảnh có trong thư mục đang chọn. Ta sử dụng control này là bởi vì nó cho phép các control khác được thêm vào theo nguyên tắc Flowlayout.



///  

/// Nạp các hình ảnh từ 1 thư mục lên FlowLayerPanel 

///  

/// đường dẫn thư mục cần load 

private void LoadImage(string path) 

{ 

thumbnailsFLP.Controls.Clear(); 

displayPictureBox.Image = null; 

string[] Files = Directory.GetFiles(path); 

thumbnailsFLP.Controls.Clear(); 

int i=1; 

foreach (String fn in Files) 

{ 

//FileInfo f = new FileInfo(path + “\\” + fn); 

if (fn.ToLower().EndsWith(“.jpg”) || fn.ToLower().EndsWith(“.GIF”) || 

fn.ToLower().EndsWith(“.png”) || fn.ToLower().EndsWith(“.bmp”) || 

fn.ToLower().EndsWith(“.jpeg”)) 

{ 

PictureBox pic = new PictureBox(); 

pic.SizeMode = PictureBoxSizeMode.StretchImage; 

pic.Image = Image.FromFile(fn); 

pic.Height = 120; 

pic.Width = 80; 

pic.Cursor = Cursors.Hand; 

thumbnailsFLP.Controls.Add(pic); 

pic.Click += new EventHandler(pic_Click); 

} 

if(i++==10) 

Application.DoEvents(); 

} 

} 


void pic_Click(object sender, EventArgs e) 

{ 

PictureBox pic = (PictureBox)sender; 

displayPictureBox.Image = pic.Image; 

}

Hình ảnh khi chạy:


Và sau đây là code hoàn chỉnh ImagesThmbnails


Chúc các bạn thành công !

Theo Võ Văn Hải's Blog
 

Web Design Technology blogs [ itdl ] Auto Backlink

HomeBlog ArchiveServicesLink2MeContactSubmit your PostPost RSS

Copyright © 2012 [ itdl ] Just for Share. Designed by Ngoc Luong - Freelancer

Best view in Chrome 11+, Firefox 5+ with resolution 1024 x 768 pixel. Powered by Blogger.