实验内容
(1)参考现有图片浏览软件功能,实现一个自己的图片浏览软件
(2)该图片浏览器,至少完成以下功能:
文件操作:目录打开,指定文件打开;
图片显示方式:normal,stretch;
图片旋转操作;
图片浏览功能:幻灯片演示;
....
(3)界面美观,操作方便
部分代码
单个文件打开
private void button1_Click_1(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "图?片?文?件t(*.bmp,jpg,gif,jpeg)|*.bmp;*.jpg;gif;jpeg";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
List<string> keylist = new List<string>();
string filename = openFileDialog1.FileName;
path2 = filename;
Show a = new Show(path2);
a.Show();
}
}
路径打开
private void button2_Click(object sender, EventArgs e)
{
listView1.Clear();
imageList1.Images.Clear();
imageList2.Images.Clear();
list.Clear();
folderBrowserDialog1.Description = "选?择?文?件t夹D";
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
path = folderBrowserDialog1.SelectedPath;
}
if (!string.IsNullOrEmpty(path))
{
DirectoryInfo dir = new DirectoryInfo(@path);
foreach (FileInfo dChild in dir.GetFiles("*.jpg"))
{
Image img = Image.FromFile(dChild.FullName, true);
list.Add(dChild.FullName);
imageList1.Images.Add(img);
imageList2.Images.Add(img);
}
foreach (FileInfo dChild in dir.GetFiles("*.bmp"))
{
Image img = Image.FromFile(dChild.FullName, true);
list.Add(dChild.FullName);
imageList1.Images.Add(img);
imageList2.Images.Add(img);
}
foreach (FileInfo dChild in dir.GetFiles("*.png"))
{
Image img = Image.FromFile(dChild.FullName, true);
list.Add(dChild.FullName);
imageList1.Images.Add(img);
imageList2.Images.Add(img);
}
foreach (FileInfo dChild in dir.GetFiles("*.jpeg"))
{
Image img = Image.FromFile(dChild.FullName, true);
list.Add(dChild.FullName);
imageList1.Images.Add(img);
imageList2.Images.Add(img);
}
foreach (FileInfo dChild in dir.GetFiles("*.gif"))
{
Image img = Image.FromFile(dChild.FullName, true);
list.Add(dChild.FullName);
imageList1.Images.Add(img);
imageList2.Images.Add(img);
}
listView1.LargeImageList = imageList1;
for (int i = 0; i < imageList1.Images.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.Name = list[i];
lvi.ImageIndex = i;
listView1.Items.Add(lvi);
}
button3.Enabled = true;
}
else
{
MessageBox.Show("请?先è选?择?路·径?");
}
}
图片操作
①显示方式改变
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string a = comboBox1.SelectedItem.ToString();
if (a.Equals("normal"))
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
if (a.Equals("stretch"))
{
pictureBox1.Size = new Size(713,405);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
}
②旋转
private void button1_Click_1(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
int p = pictureBox1.Width;
pwidth = pictureBox1.Height;
pheight = p;
pictureBox1.Size = new Size(pwidth,pheight);
pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox1.Refresh();
}
}
private void button2_Click_1(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
int p = pictureBox1.Width;
pwidth = pictureBox1.Height;
pheight = p;
pictureBox1.Size = new Size(pwidth, pheight);
pictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
pictureBox1.Refresh();
}
}
③缩放
private void button3_Click(object sender, EventArgs e)
{
width1 = Convert.ToInt32( width1/1.2);
height1 = Convert.ToInt32( height1/1.2);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Size = new Size(width1,height1);
}
private void button4_Click(object sender, EventArgs e)
{
width1 =Convert.ToInt32( width1 * 1.2);
height1 = Convert.ToInt32( height1 * 1.2);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Size = new Size(width1, height1);
}
④幻灯放映
private void timer1_Tick(object sender, EventArgs e)
{
try
{
if (i < allpath.Count())
{
Image img;
img = Image.FromFile(allpath[i], true);
int width = img.Width;
int height = img.Height;
if (width > pictureBox1.Width || height > pictureBox1.Height)
{
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
else
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
pictureBox1.Image = img;
GC.Collect();
i++;
if (i == allpath.Count())
{
i = 0;
}
}
}
catch { }
}
本次实验设计出了一个简单的图片浏览器。实验中遇到的难题有两个:缩略图的显示以及图片操作部分。缩略图部分我请教了几个同学,到网上查了不少资料后找到了解决方法。在图片操作方面,
刚开始做时就出现了图片显示不全的问题,之后又在进行图片操作(缩放、旋转)时出现了一点小Bug,但最终还是解决了。
本次实验让我更深刻地了解到C#的编程方法,以及在可视化编程时不同控件的相互组合使用的方法。很多时候一个控件解决不了的问题,只要配合另一个控件就可以迎刃而解。本次实验做出的图片浏览器在显示方面还是有不少地方不尽人意,美观度也比不上一般的图片浏览器,相信经过修改后定能变得更好。