Ứng dụng duyệt hình ảnh với C#

Ứng dụng duyệt hình ảnh với C#

Ứng dụng duyệt hình ảnh với C#

Trong ví dụ này mình sẽ hướng dẫn cho các bạn:

  1. Cách xây dựng một cây thư mục các ổ đĩa
  2. 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.

 /// <summary>

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

/// </summary>

/// <param name=”path”>đường dẫn thư mục cần load</param>

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;

} 

Và đây là sản phẩm của chúng ta…^^

 

Bạn thấy bài viết này như thế nào?: 
Average: 10 (1 vote)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
SEO

Bạn đang SEO quá mức và sẽ ăn phạt

Bạn đang lo lắng về việc website mình sẽ bị phạt bởi Google phát hiện trang đang SEO quá mức. Bạn đang tìm thông tin về các dấu hiệu, các hiện tượng SEO quá mức. Sau đây là 8 dấu hiệu seo quá mức mà bạn nên tham khảo.

Tính năng REST Export trong Drupal 8 View

Tính năng REST Export trong Drupal 8 View

This blog is all about REST Export feature of Drupal 8’ view. Let me first brief you about what is the REST api and than I will explain you how REST Export

HTC Ville siêu mỏng chạy Android 4.0 Ice Cream Sandwich

HTC Ville siêu mỏng chạy Android 4.0 Ice Cream Sandwich

Hình ảnh và thông tin mới nhất trên trang PocketNow cho thấy HTC Ville sử dụng màn hình Super AMOLED 4,3 inch với độ phân giải qHD

Công ty diệt chuột T&C

 

Diet con trung