过滤图片框中的图像?

时间:2015-08-05 15:08:15

标签: c# image forms visual-studio picturebox

遗憾的是,我没有足够的+声誉来发布我的Windows窗体的图像,但我会尝试解释。

我有一个带有8个导入图像的pictureBox到项目资源文件和两个按钮,我想用来在8个图像中来回循环,一个按钮叫做“next”,另一个按钮叫做“previous”。我在网上看过多个视频,解释了如何按下按钮并显示分配给该按钮的图像,但似乎找不到任何帮助,使用按钮循环导入用于pictureBox的图像

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

2 个答案:

答案 0 :(得分:0)

您可以使用ImageList并在表单下设置一个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;

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 :(得分:-1)

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

        }
    }
}