windows explore在查看文件中有查看缩略图这一选项,他的实质就是从thumbs文件中读取出已经生成的缩略图文件 然后做为image显示出来
在网上有如何读出thumbs文件的类库,今天使用这个类库模仿一下window explorer 类库的源地址http://www.petedavis.net/MySite/DynPageView.aspx?pageid=31
原理读出thumbs文件中的图像并生成一个picturebox数组,显示图像并放置到窗体上,在picturebox被单击时,击发一个事件 将原图显示出来
private
string
[] allfilename; ThumbDBLib.ThumbDB test;
private
PictureBox[] allimage;
private
void
button1_Click(
object
sender, System.EventArgs e)
...
{ this .Invalidate(); test = new ThumbDB( this .textBox1.Text); allfilename = test.GetThumbfiles(); allimage = new PictureBox[allfilename.Length]; int x = 0 ,y = 50 ; int i = 0 ; foreach ( string bb in allfilename) ... { allimage[i] = new PictureBox(); allimage[i].Location = new Point(x,y); Image gotimg = test.GetThumbnailImage(bb); x += 80 ; if (x > this .Width) ... { x = 0 ; y += 80 ; } allimage[i].Image= gotimg; allimage[i].Size = new Size( 75 , 75 ); allimage[i].SizeMode = PictureBoxSizeMode.StretchImage; allimage[i].Click += new EventHandler(ImageControl_Click); allimage[i].Tag = bb; this .Controls.Add(allimage[i]); // this.Refresh(); i++ ; } }
/**/
/// <summary> /// 当用户单击时显示大图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>
private
void
ImageControl_Click(
object
sender, EventArgs e)
...
{ Form showbig = new Form(); string filename = Utitlity.GetDirFromFileName(textBox1.Text) + " / " + (sender as PictureBox).Tag; PictureBox pb = new PictureBox(); Bitmap showmap = new Bitmap(filename); pb.Image = showmap; pb.Size = new Size(showmap.Width,showmap.Height); showbig.Size = new Size(showmap.Width,showmap.Height); pb.Location = new Point( 0 , 0 ); showbig.Controls.Add(pb); if (showbig.ShowDialog() == DialogResult.OK) ... { showbig.Dispose(); } }
public
class
Utitlity
...
{ /**/ /// <summary> /// 从文件名中取出文件路径 /// </summary> /// <param name="filename"> 文件名 </param> /// <returns> 文件所在的路径 </returns> public static string GetDirFromFileName( string filename) ... { string [] allpart = filename.Split( ' / ' ); return filename.Replace(allpart[allpart.Length - 1 ], string .Empty); } }