欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C#图片查看器实现方法

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

实现效果:

注意:using system.io; 往Form1上添加控件picturebox,再添加imagelist,并设置imagelist的imagesize大小

Form1.cs代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ImageCheck
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  int index;
  private void button1_Click(object sender, EventArgs e)
  {
   index--;
   if (index<0)
   {
    MessageBox.Show("去往最后一张图片");
    index = imageList1.Images.Count - 1;
   }
   this.pictureBox1.Image = this.imageList1.Images[index];
  }

  private void button2_Click(object sender, EventArgs e)
  {
   index++;
   if (index>imageList1.Images.Count-1)
   {
    MessageBox.Show("回到第一张图片");
    index = 0;
   }
   this.pictureBox1.Image = this.imageList1.Images[index];
  }

  private void LoadImage()
  {
   string rootPath = Application.StartupPath;
   string filePath = rootPath + @"\image";
   DirectoryInfo rootDir = new DirectoryInfo(filePath);
   FileInfo[] file = rootDir.GetFiles();
   for (int i=0;i<=file.Length-1;i++)
   {
    Image img = Image.FromFile(file[i].FullName);
    this.imageList1.Images.Add(img);
   }
  }

  private void Form1_Load(object sender, EventArgs e)
  {
   LoadImage();
   this.pictureBox1.Image = this.imageList1.Images[index];
  }
 }
}

注意:在C#的工作目录Debug下创建image文件夹,并放置图片

上一篇:C#如何操作Excel数据透视表

栏    目:C#教程

下一篇:WPF如何绘制光滑连续贝塞尔曲线示例代码

本文标题:C#图片查看器实现方法

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5206.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有