C#简单的图片浏览器

public partial class Form1 : Form
    {
        private int ImageCount=0;
        private List<string> ImagePaths = new List<string>();
        private int nowCount = 0;
        Boolean flag = false;
        Thread t;
        public Form1()
        {
            InitializeComponent();
        }


        private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {


        }


       


        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();




            if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string n = fd.FileName.Replace(fd.SafeFileName,"");
                foreach (string Path in Directory.GetFiles(@n))
                {


                    if (Path.Contains(".jpg") || Path.Contains(".bmp")
                    || Path.Contains(".png") || Path.Contains(".gif")
                    || Path.Contains(".ico"))
                    {
                        ImagePaths.Add(Path);
                        ImageCount++;
                    }
                    if (Path.Contains(fd.SafeFileName))
                    {
                        nowCount = ImageCount - 1;
                    }
                }
                ImageCount--;
                this.pictureBox1.Image = new Bitmap(fd.FileName);
            }
        }


        private void 反转ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int width = this.pictureBox1.Width;
                int height = this.pictureBox1.Height;
                Graphics g = this.pictureBox1.CreateGraphics();
                g.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);
                    g.DrawImage(this.pictureBox1.Image, DestRect, SrcRect, GraphicsUnit.Pixel);


                }
                g.Dispose();
            }
            catch { }
        }


        private void 旋转ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Image myImage = pictureBox1.Image;
            myImage.RotateFlip(RotateFlipType.Rotate90FlipXY); //调用RotateFlip方法将JPG格式图像进行旋转
            pictureBox1.Image = myImage;
        }


        private void 下一张ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            nowCount++;
            if (nowCount <= ImageCount)
            {
                this.pictureBox1.Image = new Bitmap(ImagePaths[nowCount]);
            }
            else
            {
                MessageBox.Show("已经是最后一张了");
            }
        }


        private void 上一张ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            nowCount--;
            if (nowCount <= ImageCount && nowCount>=0)
            {
                this.pictureBox1.Image = new Bitmap(ImagePaths[nowCount]);
            }
            else
            {
                MessageBox.Show("已经是第一张了");
            }
        }


        private void 幻灯片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!flag)
            {
               t = new Thread(OpenImage);
                t.IsBackground = true;
                t.Start();
                flag = true;
            }
            else
            {
                t.Abort();
                flag = false;
            }
        }
        public void OpenImage() {
            foreach (string s in ImagePaths)
            {
                AddImage(s);
                Thread.Sleep(1000);
            }
            t.Abort();
        }
        private delegate void AddImageDelegate(string path);
        public void AddImage(string path) {
            if (pictureBox1.InvokeRequired)
            {
                AddImageDelegate d = new AddImageDelegate(AddImage);
                pictureBox1.Invoke(d, path);
            }
            else
            {
                pictureBox1.Image = new Bitmap(path);
            }
        }


        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择图片保存的位置!";
            try
            {
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    this.pictureBox1.Image.Save(@fbd.SelectedPath+"\\text.jpeg",System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            catch { }
        }

        

    }


窗体类的设计如图,form中有一个menustrip,一个panel,panel中有一个picturebox


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值