将按钮背景/图像设置为动画 GIF

本文关键字:动画 GIF 设置 图像 按钮 背景 | 更新日期: 2023-09-27 18:36:50

我有一个.gif文件,我希望将其用作按钮的背景图像。我想这样做的主要原因(因为我知道你们中的一些人会想知道我为什么要这样做)是因为按钮与类的实例相关,我想监视这些类。当一个类(因此按钮)进入"警报"状态时,我希望我使用这些动画 GIF。我试过了;

mainUI.Controls["btn" + device.deviceButtonNumber].BackgroundImage = (System.Drawing.Image)Properties.Resources.red_orange;

但据我了解.背景图像不支持动画 GIF。我已经在整个StackOverflow中尝试了一些建议,这些似乎都不适合我,例如将BackgroundImageLayout设置为居中等等。

我想作为最后的手段,我可以让它像 gif 一样手动循环浏览图像,但这会产生更多的工作,我想让事情变得简单。有什么建议吗?

编辑:对不起,忘了添加,这是在Winforms中。

编辑2:

例如,当我动态创建按钮时,就像这样;

  Button btnAdd = new Button();
  btnAdd.Text = mDevices[i].deviceDescription;
  btnAdd.Location = new Point(mDevices[i].deviceXPos.Value, mDevices[i].deviceYPos.Value);

我仍然可以访问:

 btnAdd.Image  <- note i can access image.

然后当我稍后从另一个线程修改它们时,我发现它们是这样的;

 mainUI.Controls["btn" + device.deviceButtonNumber].text = blah blah
 mainUI.Controls["btn" + device.deviceButtonNumber].Location = blah blah

但是我无法访问;

 mainUI.Controls["btn" + device.deviceButtonNumber].Image

将按钮背景/图像设置为动画 GIF

/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //  this.button1.Image = new Bitmap(@"C:'images'dance.gif");
            // if you import the local resource 
            this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
            this.button1.ImageAlign = System.Drawing.ContentAlignment.TopRight;
            this.button1.Location = new System.Drawing.Point(109, 73);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(523, 437);
            this.button1.TabIndex = 1;
            this.button1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 17F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(807, 640);
            this.Controls.Add(this.button1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "Form1";
            this.Text = "Image In Button Control";
            this.ResumeLayout(false);
        }

我建议使用PictureBox而不是ButtonPictureBox支持.gif图像以及click事件