您的位置:首页 > 编程语言 > C#

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

2014-06-11 19:23 411 查看
(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 图片 软件
相关文章推荐