用C#做的一个简单的图片浏览器

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-31 23:06   143   0


(1)参考现有图片浏览软件的功能,实现一个自己的图片浏览器;

(2)该文档浏览器,至少完成以下功能;

文件操作: 目录打开,指定文件打开;

图片显示方式: normal,stretch;

图片旋转操作;

图片浏览功能:幻灯片演示;

(3)界面美观,操作方便

设计思路

(1)首先,无论是进行哪一步的操作,获取本地图片的path都是至关重要的,所以第一步是实现“目录打开,指定文件打开”,这里用到的控件是folderBrowserDialog,利用他可以轻松的获取本地的相应地址,然后用imageList保存,下面是实现代码:

目录获取:

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

{

filePath = folderBrowserDialog1.SelectedPath;//获取目录path

}

if (!string.IsNullOrEmpty(filePath)) //判空

{

DirectoryInfo dir = new DirectoryInfo(filePath);

foreach (FileInfo dChild in dir.GetFiles("*.jpg"))//获取jpg图片,并压入imageList

{

Image img = Image.FromFile(dChild.FullName, true);

list.Add(dChild.FullName);

imageList1.Images.Add(img);

imageList2.Images.Add(img);

}

//

//中间夹杂着重复性的对不同图片格式的相同处理,因为不关键,所以不想占字数了。

//

for (int i = 0; i < imageList1.Images.Count; i++)//用listView显示缩略图

{

ListViewItem lvi = new ListViewItem();

lvi.Name = list[i];

lvi.ImageIndex = i;

listView1.Items.Add(lvi);

}

单个文件获取:

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;

PictureView a = new PictureView(path2);//将文件导入单独文件处理模块

a.Show();

}

(2)获取了图片文件后,我首先完成的是幻灯片的放映,所利用的原理很简单,就是利用timer 控件,循环的切换image,以达到自动幻灯片的效果:

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;

}

}

在这里小小地补充了一个设置播放时速的功能:

int tmp;

if (textBox1.Text.Length != 0)

{

tmp = Convert.ToInt32(textBox1.Text);

if (tmp > 0)

{

timer1.Interval = tmp;

}

}

(3)接下来是针对单独一张图片的处理,包括扩大,缩小,旋转操作

首先是放大缩小

width1 = Convert.ToInt32( width1/1.2);

height1 = Convert.ToInt32( height1/1.2);

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

pictureBox1.Size = new Size(width1,height1);

width1 =Convert.ToInt32( width1 * 1.2);

height1 = Convert.ToInt32( height1 * 1.2);

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

pictureBox1.Size = new Size(width1, height1);

然后是旋转,这里是用RotateFlipType的自带方法完成的

pheight = pictureBox1.Width;

pwidth = pictureBox1.Height;

pictureBox1.Size = new Size(pwidth, pheight);

pictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);

pictureBox1.Refresh();

int p = pictureBox1.Width;

pwidth = pictureBox1.Height;

pheight = p;

pictureBox1.Size = new Size(pwidth,pheight);

pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);

pictureBox1.Refresh();

然后是调节显示的模式,这里直接设置就好了

Normal

pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

Stretch

pictureBox1.Size = new Size(713, 405);

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

再然后就是一些小的附加功能,比如说将图片强制生成为123.png,当然是自选路径的。

folderBrowserDialog1.Description = "选?择?文?件t夹D";

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

{

filePath = folderBrowserDialog1.SelectedPath;

}

bmp = (Bitmap)Bitmap.FromFile(path);

pictureBox1.Image = bmp;

filePath = filePath + "/123.png";

bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);

反转演示:

gra = this.pictureBox1.CreateGraphics();

gra.Clear(this.BackColor);

for (int x = -width / 2; x <= width / 2; x++)

{

Rectangle DestRect = new Rectangle(0, height / 2 - x, width, 2 * x);

Rectangle SrcRect = new Rectangle(0, 0, this.pictureBox1.Image.Width, this.pictureBox1.Image.Height);

gra.DrawImage(this.pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);

}

gra.Dispose();


程序运行效果图

1. 目录获取:

2.获取后缩略图显示

3.幻灯片播放:

4.单张处理:


下面附上demo的下载地址http://download.csdn.net/detail/u010669765/7484375


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP