pictrue类的练习
这里遇到一个问题,因为导入的文件格式很大,所以内存占用特别高,就会在运行一段时间过后报错:内存不够
然后用了一个GC.collect();的方法,加在了timer控件里,让程序每隔1000ms换图时也让垃圾回收器清理一下内存,问题解决
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; namespace picture控件练习 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //播放音乐 clsMCI runMusic = new clsMCI(); runMusic.FileName = @"F:\影音\音乐\周杰伦\范特西\周杰伦 - 简单爱.mp3"; runMusic.play(); pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox6.SizeMode = PictureBoxSizeMode.StretchImage; //在窗体加载的时候 给每个picturebox都赋值一个初始图 pictureBox1.Image = Image.FromFile(@"C:\Users\11849\Desktop\壁纸\插画\1.jpg"); pictureBox2.Image = Image.FromFile(@"C:\Users\11849\Desktop\壁纸\插画\2.jpg"); pictureBox3.Image = Image.FromFile(@"C:\Users\11849\Desktop\壁纸\插画\3.jpg"); pictureBox4.Image = Image.FromFile(@"C:\Users\11849\Desktop\壁纸\插画\4.jpg"); pictureBox5.Image = Image.FromFile(@"C:\Users\11849\Desktop\壁纸\插画\5.jpg"); pictureBox6.Image = Image.FromFile(@"C:\Users\11849\Desktop\壁纸\插画\6.jpg"); } string[] path = System.IO.Directory.GetFiles(@"C:\Users\11849\Desktop\壁纸\插画"); int i = 0; Random r = new Random(); private void timer1_Tick(object sender, EventArgs e) { //每隔一秒钟 就换一张图片 i++; if (i == path.Length) { i = 0; } pictureBox1.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox2.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox3.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox4.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox5.Image = Image.FromFile(path[r.Next(0, path.Length)]); pictureBox6.Image = Image.FromFile(path[r.Next(0, path.Length)]); GC.Collect(); } } }