Show / Hide Table of Contents

MSDN.WhiteKnight - Stack Overflow answers

Ответ на "Анимация gif в winforms c#"

Answer 1174047

Link

Реализуйте анимацию вручную вместо использования PictureBox. Класс Image поддерживает выбор кадра анимации:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Image img;
        System.Drawing.Imaging.FrameDimension dim;
        int frame = 0;

        public Form1()
        {
            InitializeComponent();            
        }
                
        private void bNext_Click(object sender, EventArgs e)
        {
            int count = img.GetFrameCount(dim);
            frame++;
            if (frame >= count) frame = 0;
            img.SelectActiveFrame(dim, frame);
            panel1.Invalidate();
            panel1.Update();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            img = Bitmap.FromFile(@"...");
            Guid[] dims = img.FrameDimensionsList;

            if (dims.Length == 0)
            {
                MessageBox.Show("Error - image does not have frame dimensions!");
                return;
            }

            dim = new System.Drawing.Imaging.FrameDimension(dims[0]);
        }        

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (img != null) e.Graphics.DrawImage(img, 0.0f, 0.0f);
        }
    }
}


Content is retrieved from StackExchange API.

Auto-generated by ruso-archive tools.

Back to top Stack Overflow answers (published from sources in GitHub repository). Copyright (c) 2020, MSDN.WhiteKnight. Content licensed under BSD 3-Clause License.
Generated by DocFX