1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
private ImageList imageList1 = new ImageList();
public Form1() /// Mise en place des éléments qui composent la fenêtre
{
InitializeComponent();
}
private void ouvrirToolStripButton_Click(object sender, EventArgs e)
{
/*// Displays an OpenFileDialog so the user can select a Cursor.
OpenFileDialog opfd = new OpenFileDialog();
opfd.Filter = "Fichiers JPEG|*.jpg";
opfd.Title = "Choisir le fichier";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a .jpg file was selected, open it.
if (opfd.ShowDialog() == DialogResult.OK)
{
string filename = opfd.FileName;
ChargerImage(filename);
}*/
FolderBrowserDialog fld = new FolderBrowserDialog();
fld.Description = "Choisir le repertoire d'image";
fld.RootFolder = Environment.SpecialFolder.MyPictures;
if (fld.ShowDialog() == DialogResult.OK)
{
string filename = fld.SelectedPath;
foreach (string f in Directory.GetDirectories(fld.SelectedPath))
{
TreeNode tn = new TreeNode(f);
repertoire.Nodes.Add(tn);
}
}
}
private void repertoire_AfterSelect(object sender, TreeViewEventArgs e)
{
ChercherImage(e.Node.Text);
}
private void ChargerImage(string filename) /// Charge l'image dans la "boîte à image"
{
pictureBox1.Image = Image.FromFile(filename);
this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
this.pictureBox1.Text = filename;
}
private void ChercherImage(string path) /// Affiche l'ensemble des fichiers et leurs icônes lorsqu'un dossier est sélectionné
{
fichiers.View = View.LargeIcon;
imageList1.ImageSize = new Size(80, 60);
fichiers.LargeImageList = imageList1;
string[] files = Directory.GetFiles(path); /// Stocke la liste des fichiers
foreach (string s in files)
{
if (s.ToUpper().EndsWith("jpg")) /// Contrôle qu'il sagit bien d'une image
{
Image im = Image.FromFile(s);
imageList1.Images.Add(im.GetThumbnailImage(106, 120, null, IntPtr.Zero)); /// Création des miniatures
im.Dispose();
ListViewItem lw = new ListViewItem(s, imageList1.Images.Count - 1);
fichiers.Items.Add(lw);
}
}
}
private void fichiers_Click(object sender, EventArgs e) /// Sélection du fichier à afficher
{
string f;
if (fichiers.SelectedIndices.Count > 0)
{
f = fichiers.SelectedItems[0].Text;
ChargerImage(f);
}
}
}
} |