System.ArgumentException: '引數無效。'
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < myGifList.Count; i )
{
Bitmap bmp = new Bitmap(myGifList[i]);
pictureBox1.Image = bmp;
bmp.Dispose();
}
}
回圈結束后拋出例外。
在此之前我做了:
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < myGifList.Count; i )
{
pictureBox1.Image = new Bitmap(myGifList[i]);
}
}
但是隨后它拋出了記憶體不足例外。
在這之前,我第一次嘗試這樣做:
int counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if(counter == 10)
{
counter = 0;
}
for (int i = 0; i < myGifList.Count; i )
{
Bitmap bmp = new Bitmap(myGifList[counter]);
pictureBox1.Image = bmp;
}
counter ;
}
這是作業影像在pictureBox1 中回圈,但一段時間后它拋出記憶體不足例外。
完整代碼:
我正在下載影像,然后將影像讀回串列,然后嘗試將它們顯示在 picutreBox1 中,并使用 trackBar1 使用計時器更改 pictureBox1 中影像的回圈速度。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Extract
{
public partial class Form1 : Form
{
List<string> myGifList = new List<string>();
public Form1()
{
List<string> times = new List<string>();
string t = "";
InitializeComponent();
using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
{
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");
client.DownloadFile("https://myimages/", @"D:\localfile.html");
var file = File.ReadAllText(@"D:\localfile.html");
int idx = file.IndexOf("arrayImageTimes.push");
int idx1 = file.IndexOf("</script>", idx);
string results = file.Substring(idx, idx1 - idx);
var statements = results.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < statements.Length; i )
{
if (i == 10)
{
break;
}
string number = statements[i].Split('\'')[1];
times.Add(number); // add to a list instead
var link = "https://myimages" number;
client.DownloadFile(link, @"D:\Images\Image" i ".jpeg");
}
}
FileInfo[] fi;
DirectoryInfo dir1 = new DirectoryInfo(@"D:\Images");
fi = dir1.GetFiles("*.jpeg");
for (int i = 0; i < fi.Length; i )
{
myGifList.Add(fi[i].FullName);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
int counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if(counter == 10)
{
counter = 0;
}
for (int i = 0; i < myGifList.Count; i )
{
Bitmap bmp = new Bitmap(myGifList[counter]);
pictureBox1.Image = bmp;
}
counter ;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
timer1.Interval = 100 * trackBar1.Value;
}
}
}
uj5u.com熱心網友回復:
您似乎正在嘗試遍歷myGifList,但是您已經10在方法中硬編碼了一個值。
然而,更大的問題是你甚至沒有使用i回圈定義的,所以你只是覆寫了相同的Bitmap myGifList.Count時間,一無所獲 - 使你的代碼效率低下。
嘗試:
private void timer1_Tick(object sender, EventArgs e)
{
if (counter == myGifList.Count)
counter = 0;
pictureBox1.Image?.Dispose();
Bitmap bmp = new Bitmap(myGifList[counter]);
pictureBox1.Image = bmp;
counter ;
}
請注意,您應該在呼叫時使用null 條件運算子 ?來防止可能的NullReferenceException情況.Dispose():
pictureBox1.Image?.Dispose();
這會在第一次按原樣設定影像時pictureBox1.Image發生null,導致.Dispose()拋出例外。
更好的是使用PictureBox.ImageLocation允許您顯示影像而無需Bitmap每次都創建物件的屬性:
private void timer1_Tick(object sender, EventArgs e)
{
if (counter == myGifList?.Count)
counter = 0;
pictureBox1.ImageLocation = myGifList[counter];
counter ;
}
旁注:該Timer.Interval屬性也是以毫秒為單位的,因此如果您希望實際能夠看到影像回圈,我會提高100您擁有的值trackBar1_Scroll(...)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/318262.html
