当前位置:文档之家› 图片管理器课程设计

图片管理器课程设计

学号天津城建大学可视化编程课程报告学生姓名班级成绩计算机与信息工程学院目录第1章设计任务与目标 0第2章设计方案 (1)2.1管理器结构设计 (1)第3章设计实现 (2)3.1程序主窗体设计 (2)3.1.1主窗体代码 (2)3.2程序目录设计 (12)3.2.1目录代码 (12)3.3导入图片窗体设计 (14)3.3.1导入图片窗体代码 (14)3.4所需的类代码 (18)第4章设计结果与分析 (24)4.1设计分析计 (24)4.2设计结构 (24)第5章总结心得 (25)第1章设计任务及目标1.1 设计任务及目标1、设计图片管理器,对图片实现分目录管理,用户可以自行创建并删除存放图片的目录,并且可以方便将各种图片存放于相应的目录中。

2、可以对目录下的图片以缩略图的形式进行浏览,方便用户进行查找图片,并且在浏览过程中删除一张或者多种图片。

3、可以以实际大小或者适合窗体的大小对单张图片浏览并切换到同目录中的上一张或者下一张图片进行浏览。

也可以对同目录中的图片以自动播放形式进行浏览,还可以调整播放时间间隔。

第2章设计方案2.1 管理器结构设计通过对结构的分析,以及考虑到要满足的功能,将管理器分为如下几个模块。

1.主窗体:用来实现对图片的浏览,删除,导入,保存等功能。

2.目录窗体:用来创建图片的管理目录,整理图片。

3.导入图片:用来将图片导入目录中,整理到相应文件夹。

第3章设计实现3.1程序的主窗体1、设计程序的主窗体用来浏览和查看图片并且对图片进行管理。

通过规划,首先设计窗体页面,然后添加相应的控件。

如下:3.1.1主窗体代码编写:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Collections;namespace 图片管理器3{public partial class MainForm : Form{public MainForm(){InitializeComponent();private string path = Application.StartupPath + "\\图片目录";private Pen boundPen = new Pen(Color.Gainsboro);private Pen selPen = new Pen(Color.Blue, 3);private SolidBrush textBrush = new SolidBrush(Color.Black);private SolidBrush bgBrush;private StringFormat format = new StringFormat();private Bitmap bmpInPb;private Point mousePoint = new Point();private Point pbPoint = new Point();private bool canDrag;private bool isDraging;private int bmpIndex;private void MainForm_Load(object sender, EventArgs e){lvView.Dock = DockStyle.Fill;tscbInterval.SelectedIndex = 1;//图像自动播放时间间隔2秒ShowView();//处于浏览图片状态bgBrush = new SolidBrush(lvView.BackColor);statusStrip1.Items[0].Visible = false;//状态栏上的进度为不可见format.Alignment = StringAlignment.Center;try{if (!Directory.Exists(path)){Directory.CreateDirectory(path);}}catch (Exception ex){MessageBox.Show(ex.Message, "错误",MessageBoxButtons.OK, MessageBoxIcon.Error);return;}DirectoryInfo dir = new DirectoryInfo(path);foreach (DirectoryInfo d in dir.GetDirectories()){Folder folder = new Folder(Application.StartupPath, );lstFolder.Items.Add(folder);}}private void tsbtnCreatFolder_Click(object sender, EventArgs e){FrmCreatFolder frmCreatFolder = new FrmCreatFolder(this.lstFolder);{frmCreatFolder.ShowDialog(this);}finally{frmCreatFolder.Dispose();}}private void tsbtnLoad_Click(object sender, EventArgs e){FrmLoadPic frmLoadPic = new FrmLoadPic(this.lstFolder, this.statusStrip1);try{if (frmLoadPic.ShowDialog(this) == DialogResult.OK){LoadToListView();}}finally{frmLoadPic.Dispose();}}private void lvView_DrawItem(object sender, DrawListViewItemEventArgs e) {if (lvView.Items.Count == 0){return;}Graphics g = e.Graphics;Folder folder = (Folder)lstFolder.SelectedItem;Bitmap bmp = folder.GetThumnail(e.Item.Text);Rectangle bmpRect = Folder.GetRectFromBounds(bmp, e.Bounds);bmpRect.Offset(0, 1);Rectangle boundRect = Folder.GetRectFromBounds(101, 101, e.Bounds);Rectangle textRect = new Rectangle(e.Bounds.X + 4,e.Bounds.Y + 109, e.Bounds.Width - 8, 16);g.DrawRectangle(boundPen, boundRect);if ((e.State & ListViewItemStates.Selected) != 0){g.DrawImage(bmp, bmpRect);//画图片的缩略图g.DrawRectangle(selPen, boundRect);//画处于选中状态的边框}else{g.DrawImage(bmp, bmpRect);}g.FillRectangle(bgBrush, textRect);//填充文字背景g.DrawString(e.Item.Text, lvView.Font, textBrush, textRect, format);//绘制文字}private void LoadToListView()//自定义方法{Folder folder = (Folder)lstFolder.SelectedItem;lvView.BeginUpdate();lvView.Items.Clear();if (!folder.IsLoaded){folder.LoadImage();}foreach (DictionaryEntry de in folder.bmps){lvView.Items.Add((string)de.Key);}lvView.EndUpdate();}private void lstFolder_SelectedIndexChanged(object sender, EventArgs e){if (lstFolder.SelectedItems.Count == 0){return;}LoadToListView();}//让浏览图像缩略图所需控件可见private void ShowView()//自定义方法{tsMain.Visible = true;lstFolder.Visible = true;splitter1.Visible = true;lvView.Visible = true;pbPic.Visible = false;tsViewPic.Visible = false;}//让浏览图像所需控件可见private void ShowImage()//自定义方法{tsMain.Visible = false;lstFolder.Visible = false;splitter1.Visible = false;lvView.Visible = false;pbPic.Visible = true;tsViewPic.Visible = true;}private void lvView_DoubleClick(object sender, EventArgs e) {Point p = Control.MousePosition;p = lvView.PointToClient(p);ListViewHitTestInfo info = lvView.HitTest(p);ShowImage();PaintImageInPb(info.Item.Text);bmpIndex = info.Item.Index;}private void PaintImageInPb(string bmpName)//自定义方法{Folder folder = (Folder)lstFolder.SelectedItem;if (bmpInPb != null){bmpInPb.Dispose();}bmpInPb = folder.GetImage(bmpName);statusStrip1.Items[1].Text = "名称:" + bmpName + "尺寸:" + bmpInPb.Width.ToString() + "×" +bmpInPb.Height.ToString();pbPic.Image = bmpInPb;MatchImage();}private void MatchImage()//自定义方法{if (tsbtnNormol.Checked){//正常显示模式pbPic.SizeMode = PictureBoxSizeMode.AutoSize;pbPic.Left = (panel1.Width - pbPic.Width) / 2;pbPic.Top = (panel1.Height - pbPic.Height) / 2;if (pbPic.Width > panel1.Width || pbPic.Height > panel1.Height){canDrag = true;pbPic.Cursor = Cursors.Hand;//改变鼠标指针样式}else{//图像小于显示边框是,则不允许拖动canDrag = false;pbPic.Cursor = Cursors.Default;}}else{//图像使用PictureBox的大小显示模式canDrag = false;//设置禁止拖动图像状态pbPic.Cursor = Cursors.Default;if (bmpInPb.Width > panel1.Width || bmpInPb.Height > panel1.Height){pbPic.Dock = DockStyle.Fill;pbPic.SizeMode = PictureBoxSizeMode.Zoom;}else{//图像小于显示边框时,按原尺寸显示pbPic.Dock = DockStyle.None;pbPic.SizeMode = PictureBoxSizeMode.AutoSize;pbPic.Left = (panel1.Width - pbPic.Width) / 2;pbPic.Top = (panel1.Height - pbPic.Height) / 2;}}}private void tsbtnShowMode_Click(object sender, EventArgs e){ToolStripButton btn = (ToolStripButton)sender;{return ;}tsbtnNormol.Checked =false ;tsbtnMatch.Checked=false ;btn .Checked =true ;MatchImage ();}private void pbPic_MouseDown(object sender, MouseEventArgs e) {if (e.Button !=MouseButtons .Left ){//判断是否鼠标左键return ;}isDraging=true ;mousePoint.X =e.X ;mousePoint.Y=e.Y;pbPoint.X =pbPic.Left ;pbPoint.Y=pbPic.Top;}private void pbPic_MouseMove(object sender, MouseEventArgs e) {if (!isDraging || !canDrag){return;}//左右移动int x = pbPic.Left;if (pbPic.Width > panel1.Width){x += e.X - mousePoint.X;if (x > 0){x = 0;}else if (x + pbPic.Width < panel1.Width){x = panel1.Width - pbPic.Width;}//上下移动int y = pbPic.Top;if (pbPic.Height > panel1.Height ){y += e.Y - mousePoint.Y;if (y > 0){y = 0;}else if (y + pbPic.Height < panel1.Height ){y = panel1.Height - pbPic.Height ;}}pbPic.Left = x;pbPic.Top = y;}private void pbPic_MouseUp(object sender, MouseEventArgs e) {if (e.Button != MouseButtons.Left){return;}isDraging = false;//设置为禁止拖动状态}private void tsbtnReturn_Click(object sender, EventArgs e) {timer1.Stop();//停止计时器pbPic.Image = null;if (bmpInPb != null){bmpInPb.Dispose();//释放图像}ShowView();//调整控件可见性}private void tsbtnPeriod_Click(object sender, EventArgs e) {if (bmpIndex == 0)bmpIndex = lvView.Items.Count - 1;}else{bmpIndex--;}ListViewItem item = lvView.Items[bmpIndex];PaintImageInPb (item .Text);}private void tsbtnNext_Click(object sender, EventArgs e){if (bmpIndex ==lvView.Items .Count -1){bmpIndex = 0;}else{bmpIndex++;}ListViewItem item = lvView.Items[bmpIndex];PaintImageInPb (item .Text);}private void tsbtnAutoPlay_Click(object sender, EventArgs e){timer1.Enabled = !timer1.Enabled;tsbtnAutoPlay.Checked = timer1.Enabled;}private void tscbInterval_SelectedIndexChanged(object sender, EventArgs e) {timer1.Interval = (int)(Math.Pow(2, tscbInterval.SelectedIndex) * 1000); }private void timer1_Tick(object sender, EventArgs e){tsbtnNext_Click(null, null);}private void tsbtnDelFolder_Click(object sender, EventArgs e){if (lstFolder .SelectedItems .Count==0){MessageBox.Show ("请选择一个目录再进行删除!","消息",MessageBoxButtons .OK ,rmation);return ;}DialogResult dr=MessageBox .Show ("删除目录将导致该目录下的图片删除", "并且该操作不可恢复,是否真的要删除“"+lstFolder .Text +"确认",MessageBoxButtons .YesNo ,MessageBoxIcon .Question );string delFolderName="";//用于存放被删除目录的名称if (dr == DialogResult.Yes){lvView.Clear();delFolderName = lstFolder.Text;((Folder)lstFolder.SelectedItem).RemoveAll();lstFolder.Items.Remove(lstFolder.SelectedItem);}statusStrip1.Items[1].Text="目录“"+delFolderName+"”已经被删除!";//在状态栏显示提示}private void tsbtnDel_Click(object sender, EventArgs e){if (lstFolder.SelectedItems.Count == 0 || lvView.Visible == false){return;}Folder folder = (Folder)lstFolder.SelectedItem;try{lvView.BeginUpdate();//禁止ListView的刷新while (lvView.SelectedItems .Count > 0){//删除选中项ListViewItem item = lvView.SelectedItems [0];lvView.Items.Remove(item);folder.Remove(item.Text);}}catch (Exception ex){MessageBoxButtons.OK, MessageBoxIcon.Error);return;}finally{lvView.EndUpdate();}}private void tsBtnClose_Click(object sender, EventArgs e){Close();}}}3.2 程序的目录创建目录窗体,用于创建图片的管理目录。

相关主题