这次作业比较有意思! 立马给人高大上的感觉~

作业要求:

1. 制作一个图片浏览器;

2. 能自由缩放;

3. 拉取目录下所有图片文件。

特性:

1. 简约大方,窗口有最小尺寸限制;

2. 初始目录定位到计算机图库;

3. 相应键盘上下左右切换图片。 点击下载>>

上图:

代码:

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.Drawing.Imaging;
using System.IO;
namespace PhotoBrowser
{
	public partial class Form1 : Form
	{
		string		imgPath		= null, directory = null;
		List<string>	imgArray	= null;
		public Form1()
		{
			InitializeComponent();
		}


		private void refresh( string fName )
		{
		}


		private void button1_Click( object sender, EventArgs e )
		{
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			openFileDialog1.InitialDirectory	= Environment.GetFolderPath( Environment.SpecialFolder.MyPictures );
			openFileDialog1.Filter			= "(*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.wmf;*.tiff)|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.wmf;*.tiff";
			openFileDialog1.FilterIndex		= 1;
			openFileDialog1.RestoreDirectory	= true;
			if ( openFileDialog1.ShowDialog() == DialogResult.OK )
			{
				imgPath			= openFileDialog1.FileName;
				pictureBox1.Image	= Image.FromFile( imgPath );
				directory		= Path.GetDirectoryName( imgPath );
				string[]	imgarray	= Directory.GetFiles( directory );
				var		result		= from imgstring in imgarray
								  where imgstring.EndsWith( "jpg", StringComparison.OrdinalIgnoreCase ) ||
								  imgstring.EndsWith( "jpeg", StringComparison.OrdinalIgnoreCase ) ||
								  imgstring.EndsWith( "png", StringComparison.OrdinalIgnoreCase ) ||
								  imgstring.EndsWith( "bmp", StringComparison.OrdinalIgnoreCase ) ||
								  imgstring.EndsWith( "gif", StringComparison.OrdinalIgnoreCase ) ||
								  imgstring.EndsWith( "wmf", StringComparison.OrdinalIgnoreCase ) ||
								  imgstring.EndsWith( "tiff", StringComparison.OrdinalIgnoreCase )
								  select imgstring;


				imgArray	= result.ToList();
				button2.Enabled = button3.Enabled = true;
			}
		}


		private void Form1_Load( object sender, EventArgs e )
		{
			Size newSize = new Size( 300, 300 );
			this.MinimumSize	= newSize;
			this.Size		= newSize;
			button2.Enabled		= button3.Enabled = false;
		}


		private int GetIndex( string imagepath )
		{
			for ( int i = 0; i < imgArray.Count; i++ )
				if ( imgArray[i].Equals( imagepath ) )
					return(i);
			return(0);
		}


		private void button3_Click( object sender, EventArgs e )
		{
			int index = GetIndex( imgPath );
			++index;
			if ( index == imgArray.Count )
				index = 0;
			imgPath			= imgArray[index];
			pictureBox1.Image	= Image.FromFile( imgArray[index] );
		}


		private void button2_Click( object sender, EventArgs e )
		{
			int index = GetIndex( imgPath );
			--index;
			if ( index == -1 )
				index = imgArray.Count - 1;
			imgPath			= imgArray[index];
			pictureBox1.Image	= Image.FromFile( imgArray[index] );
		}


		protected override bool ProcessDialogKey( Keys keyData )
		{
			if ( button2.Enabled == false || button3.Enabled == false )
				return(base.ProcessDialogKey( keyData ) );
			int idx = GetIndex( imgPath );
			switch ( keyData )
			{
			case Keys.Up:
				--idx;
				break;
			case Keys.Down:
				++idx;
				break;
			case Keys.Left:
				--idx;
				break;
			case Keys.Right:
				++idx;
				break;
			}
			if ( idx == imgArray.Count )
				idx = 0;
			if ( idx == -1 )
				idx = imgArray.Count - 1;
			imgPath			= imgArray[idx];
			pictureBox1.Image	= Image.FromFile( imgArray[idx] );
			return(base.ProcessDialogKey( keyData ) );
		}
	}
}

转载保留版权:http://haipz.com/blog/i/869 - 海胖博客