PDA

View Full Version : Ứng dụng duyệt ảnh với C#



thietkewebsite
13-02-2009, 08:26
Trong ví dụ này tôi muốn chỉ 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;

}



Hình ảnh khi chạy:

IThumbails


Chúc may mắn
Nguồn http://xpt.vn - http://forum.xpt.vn/default.aspx?g=posts&t=76