我正在使用TWAIN控制来扫描图像,这很好用,我可以扫描图像并在pictureBox1
中显示。我正在努力将多个图像添加到流中,然后使用Previous
和Next
按钮逐步执行它们。
有人可以帮忙,下面的代码不起作用,我现在处于困惑之中。感谢
//The axTwain1_OnAcquire event runs on each scanned page.
private void axTwain1_OnAcquire(object sender, EventArgs e)
{
images = new List<Image>();
pageCount = pageCount + 1;
axTwain1.SaveToFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + pageCount +".jpg");
FileStream stream = File.OpenRead(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + pageCount + ".jpg");
images.Add(Image.FromStream(stream));
pictureBox1.Image = System.Drawing.Bitmap.FromStream(stream);
}
private void btnNext_Click(object sender, EventArgs e)
{
index++;
if (index < 0 || index >= images.Count)
index = 0;
pictureBox1.Image = images[index];
int count = index + 1;
//labelCount.Text = "Showing " + count.ToString() + " of " + images.Count;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
index++;
if (index < 0 || index >= images.Count)
index = 0;
pictureBox1.Image = images[index];
int count = index + 1;
}
答案 0 :(得分:1)
你的逻辑看起来有点棘手。在上一个和下一个函数中,您都有条件语句:
index < 0 || index >= images.Count
接下来,您的逻辑应该类似于:
index < images.Count -- then increment.
以及之前的:
index > 0 -- then decrement.
基本上,你的功能应该如下:
private void btnNext_Click(object sender, EventArgs e)
{
if(index < images.Count) {
index++;
pictureBox1.Image = images[index];
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if(index > 0)
{
index--;
pictureBox1.Image = images[index];
}
}