资源描述:
《C#图片添加picturebox用法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C#关于picturebox和imagelist的用法usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceTExt1{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}inti;//系统当前有一张默认图片privat
2、evoidForm1_Load(objectsender,EventArgse){if(imageList1.Images.Count>=1){pictureBox1.Image=imageList1.Images[0];}i=-1;}//增加图片privatevoidbutton1_Click(objectsender,EventArgse){OpenFileDialogofd=newOpenFileDialog();ofd.InitialDirectory="E:";ofd.Filter="图像
3、*.jpg";ofd.RestoreDirectory=true;ofd.FilterInde
4、x=1;StringfName;if(ofd.ShowDialog()==DialogResult.OK){fName=ofd.FileName;ImagemyImage=Image.FromFile(fName,true);imageList1.Images.Add(myImage);}i++;pictureBox1.Image=imageList1.Images[i];}//删除当前图片privatevoidbutton2_Click(objectsender,EventArgse){//图片数量大于1if(imageList1.Images.Count>=1&&i>=0){imageLi
5、st1.Images.RemoveAt(i);//i--;//判断图片是否为空if(imageList1.Images.Count==0){pictureBox1.Image=null;i=-1;}else{pictureBox1.Image=imageList1.Images[i];}}//图片为空时不显示else{this.addTitle();}}//显示下一张图片privatevoidbutton3_Click(objectsender,EventArgse){if(imageList1.Images.Count>=1){if(imageList1.Images.Count==1){M
6、essageBox.Show("只有一张图片");}i++;if(i>imageList1.Images.Count-1){i=0;}pictureBox1.Image=imageList1.Images[i];}}//清空所有的图片privatevoidbutton4_Click(objectsender,EventArgse){DialogResultresult=MessageBox.Show("确定清空???","提示信息",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);if(DialogResult.OK==result
7、){imageList1.Images.Clear();pictureBox1.Image=null;i=-1;this.addTitle();}}//退出privatevoidbutton5_Click(objectsender,EventArgse){Application.Exit();}//定义函数——当图片为空时点击确定添加图片privatevoidaddTitle(){DialogResultsel=MessageBox.Show("图片为空,请选择一张图片,点击确定添加图片,否则点击取消!","信息提示",MessageBoxButtons.OKCancel,MessageBox
8、Icon.Information);if(sel==DialogResult.OK){OpenFileDialogofd=newOpenFileDialog();ofd.InitialDirectory="E:";ofd.Filter="图像
9、*.jpg";ofd.RestoreDirectory=true;ofd.FilterIndex=1;StringfName;if(ofd.ShowDial