C#图片查看器实现方法
程序员文章站
2023-12-16 10:23:28
实现效果:
注意:using system.io; 往form1上添加控件picturebox,再添加imagelist,并设置imagelist的imagesize...
实现效果:
注意: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文件夹,并放置图片