Ứ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

 
Phần 1 - Custom Image Search với Solr, Filefield Sources và CtoolsPhần 1 - Custom Image Search với Solr, Filefield Sources và CtoolsPhần 1 - Custom Image Search với Solr, Filefield Sources và Ctools

Phần 1 - Custom Image Search với Solr, Filefield Sources và Ctools

As I mentioned above, I also wanted to index metadata about the image to allow for better searching, so I needed to find a way to store that data.

Embarking

Giới thiệu Embarking on a Drupal 8 adventure

We know Drupal 7 pretty well, we know what it can do but we also know what its limitations are.

100 mạng xã hội lớn nhất Việt Nam

100 mạng xã hội lớn nhất Việt Nam

Mạng xã hội ngày nay trở thàng công cụ marketing online và SEO không thể thiếu cho bất cứ doanh nghiệp tổ chứ online nào. Nhưng để biết được thứ hạng cũng như số lượng người dùng vào những mạng xã hội nào để có sự đầu tư hợp lý thì không phải ai cũng biết.

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

 

Diet con trung