通过图片框中的图像进行过滤

本文关键字:图像 过滤 | 更新日期: 2023-09-27 18:06:34

不幸的是,我没有足够的声誉来发布我的windows窗体的图像,但我会尝试解释。

我有一个pictureBox与8导入图像到项目资源文件和两个按钮,我想用它来通过8个图像来回循环,一个按钮称为"下一个",另一个按钮称为"上一个"。我在网上看过多个视频,解释如何按下按钮并显示分配给该按钮的图像,但似乎无法找到任何帮助,使用按钮来循环使用已导入的图像与pictureBox

我希望我的问题是明确的,我期待着阅读你的建议,提前谢谢你!

通过图片框中的图像进行过滤

您可以使用ImageList并在表单下设置一个Int变量作为Index进行循环。你的按钮动作是改变索引和更新pictureBox图像。

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 imagesPictureBox
{
    public partial class Form1 : Form
    {
        int _currentIndex = 0;
        int currentIndex {
            get {
                return _currentIndex;
            }
            set {
                if (value >= imageList1.Images.Count) {
                    //    if index is out of range in the large side, reset to 0
                    _currentIndex = 0;
                }
                else if (value < 0) {
                    //    if index is out of range in the samll side, reset to number of images - 1 (it's 0 based)
                    _currentIndex = imageList1.Images.Count - 1;
                } else {
                    _currentIndex = value;
                }
                //    update image after index is updated
                pictureBox1.Image = imageList1.Images[_currentIndex];
            }
        }
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Image = imageList1.Images[currentIndex];
        }
        private void nextBTN_Click(object sender, EventArgs e)
        {
            currentIndex++;
        }
        private void prevBTN_Click(object sender, EventArgs e)
        {
            currentIndex--;
        }
    }
}
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;
using System.Resources;
using System.Collections;
namespace PicBoxIteration
{
    public partial class Form1 : Form
    {
        ResourceSet resourceSet;
        IDictionaryEnumerator iDict;
        Dictionary<int, Bitmap> imgDict = new Dictionary<int, Bitmap>();
        int currentIndex = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            resourceSet =     Properties.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
            iDict = resourceSet.GetEnumerator();
            foreach (DictionaryEntry entry in resourceSet)
            {
                imgDict.Add(currentIndex, (Bitmap) entry.Value);  
                currentIndex ++;
            }
            currentIndex = 0;
            pictureBox1.Image = imgDict[currentIndex];
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentIndex < imgDict.Count - 1)
            {
                currentIndex++;

            }
            else
            {
                currentIndex = 0;
            }
            pictureBox1.Image = imgDict[currentIndex];
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            if (currentIndex > 0)
            {
                currentIndex--;
            }
            else
            {
                currentIndex = imgDict.Count -1;
            }
            pictureBox1.Image = imgDict[currentIndex];
        }
    }
}