Display images one by one in the Windows Form folder on PictureBox

Asked 2 years ago, Updated 2 years ago, 75 views

Nice to meet you.
C#I'm a beginner and I don't know many things, so I'd like to ask you a question.

Currently, I am trying to retrieve the images in the folder with C# and display them one by one on PicutreBox like a video, but I am having a hard time.

What I've tried now is to convert it to GIF and then display it on PictureBox, but I gave up because it doesn't play GIF and eats a lot of memory.

I would appreciate it if you could give me your advice.

c# winforms

2022-09-30 17:47

1 Answers

Failed to read the requested answer from the question statement.
Here are three sample codes that you often see similar questions:

Paste PictureBox(pictureBox1) and Timer(timer1) into Windows Forms for the sample code to work.

You must add the Load and Shown events to the Form, the MouseClick events to the PictureBox, and the Tick events to the Timer.

using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form 1:Form
    {
        private int Index;
        private bool IsMouseMode;

        public Form 1()
        {
            InitializeComponent();
            IsMouseMode=false;
        }

        private void Form1_Load(object sender, EventArgse)
        {
            // Create 64 original animation images
            CreateSampleImage();
        }

        private void CreateSampleImage()
        {
            var bmp = new Bitmap(64,64);
            range=Enumerable.Range(0,64);
            // Fill out the background appropriately
            var zip = from x in range
                      from y in range
                      select new {x,y};
            zip.ToList().ForEach(p=>{bmp.SetPixel(p.x, p.y, Color.Black);});
            //Sine painting
            range.Select(x=>
            {
                varrad=System.Math.PI*x/12d;
                return new { x = x, y = (int) (Math.Sin(rad)*5+bmp.Height/2)};
            }).ToList().ForEach(p=>
            {
                bmp.SetPixel(p.x, p.y, Color.White);
                bmp.Save(string.Format("{0}.png", p.x), ImageFormat.Png);
            });
        }

        private voidForm1_Shown(object sender, EventArgse)
        {
            Text = "Fastest Animation";
            DrawRapidly();
            Text="10 FPS Animation";
            DrawOnTimer(100);// Ends Click Animation Mode when exiting
        }

        /// <summary>
        /// Anyway, I'm going to draw an animation at the fastest speed.
        /// </summary>
        private void DrawRapidly()
        {
            pictureBox1.WaitOnLoad=true;
            Enumerable.Range(0,64).ToList().ForEach(i=>
            {
                pictureBox1.Load(string.Format("{0}.png", i));
                pictureBox1.Update();
            });
        }

        /// <summary>
        /// draw according to the interval between
        /// </summary>
        /// <param name="interval"></param>
        private void DrawOnTimer (int interval)
        {
            timer1.Interval=interval;
            pictureBox1.WaitOnLoad=false;
            Index = 0;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgse)
        {
            pictureBox1.LoadAsync(string.Format("{0}.png", Index));
            pictureBox1.Update();
            Index++;
            if (64<=Index)
            {
                timer1.Stop();
                DrawOnClick();
            }
        }

        private void DrawOnClick()
        {
            Text="Click Animation";
            IsMouseMode=true;
            Index = 0;
        }

        /// <summary>
        /// Left-click to stretch the line.draw a line so that it shrinks with other than that
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseClick(object sender, MouseEventArgse)
        {
            if(!IsMouseMode) return;
            Index+=e.Button==MouseButtons.Left?1:-1;
            Index = Math.Max (Math.Min (Index, 63), 0);
            pictureBox1.LoadAsync(string.Format("{0}.png", Index));
        }
    }
}

If you would like to ask the following questions, please consider adding a specific question or sample code.


2022-09-30 17:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.