C#中PictureBox异步加载图片

private void button1_Click(object sender, EventArgs e) { //模拟 String[] st=new String[13]; for (int i = 0; i < 13; i++) { //这里我用的是比较大的图片 桌面背景 用于测试 可以根据自己的实际情况来添加地址 可以是url等形式 st[i]=@"D:/备份/桌面背景/"+i+".jpg"; } //为了节约时间 简洁代码 创建图片容器时没多做处理 for (int j = 0; j < st.Length; j++) { //创建pictureBox PictureBox pi = new PictureBox(); pi.InitialImage = global::MainForm.Properties.Resources._36_1_;//等待时显示其他图片 pi.Location = new System.Drawing.Point(22, 25); pi.Name = "pictureBox" + j; pi.Size = new System.Drawing.Size(185, 99); pi.TabIndex = 0; pi.TabStop = false; pi.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.pictureBox1_LoadCompleted); //图片异步加载完成后的处理事件 pi.LoadCompleted += new AsyncCompletedEventHandler(pictureBox_LoadCompleted); //图片加载时,显示等待光标 pi.UseWaitCursor = true; //采用异步加载方式 pi.WaitOnLoad = false; //开始异步加载 pi.LoadAsync(st[j].ToString()); flowLayoutPanel1.Controls.Add(pi); } } //通用事件 private void pictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e) { //转换成PictureBox PictureBox pi = (PictureBox)sender; //图片加载完成后,将光标恢复 pi.UseWaitCursor = false; }

你可能感兴趣的:(WinForm)