C# 在pictureBox中过滤图像?

C# 在pictureBox中过滤图像?,c#,image,forms,visual-studio,picturebox,C#,Image,Forms,Visual Studio,Picturebox,不幸的是,我没有足够的声誉来发布我的windows窗体的图片,但我会尽力解释 我有一个pictureBox,其中有8个导入到项目资源文件的图像和两个按钮,我想用它们在8个图像之间来回循环,一个按钮称为next,另一个按钮称为previous。我在网上看过多个视频,解释如何按下按钮并显示分配给该按钮的图像,但似乎找不到任何帮助,可以使用按钮循环浏览已导入用于pictureBox的图像 我希望我的问题是明确的,我期待着阅读您的建议,提前谢谢您 您可以在表单下使用并设置一个Int变量作为循环的索引。您

不幸的是,我没有足够的声誉来发布我的windows窗体的图片,但我会尽力解释

我有一个pictureBox,其中有8个导入到项目资源文件的图像和两个按钮,我想用它们在8个图像之间来回循环,一个按钮称为next,另一个按钮称为previous。我在网上看过多个视频,解释如何按下按钮并显示分配给该按钮的图像,但似乎找不到任何帮助,可以使用按钮循环浏览已导入用于pictureBox的图像

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

您可以在表单下使用并设置一个Int变量作为循环的索引。您的按钮操作将是更改索引和更新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;
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];

        }
    }
}
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--;
        }
    }
}

您需要一次遍历一项资源集合。几分钟后我会尝试在这里举一个例子。基本上,您需要获取资源集合的索引如果它还不是索引集合,那么您需要使用资源作为源创建一个索引集合,只需将当前索引增加或减少1,即可获得字符串路径并将其设置为pictureBox图像路径。这是一个粗略的示例,可能有一些冗余/昂贵的方法。我只是尽可能快地把它放在一起,给你一个如何创建这些文件的示例。另一种方法是将文件存储在单独的文件夹中,并使用System.IO命名空间获取文件。只是一个想法。