【问题标题】:setting button background / image to animated gif将按钮背景/图像设置为动画 gif
【发布时间】:2012-06-12 12:57:25
【问题描述】:

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

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

但据我了解,.BackgroundImage 不支持动画 gif。我在整个 stackoverflow 中尝试了一些建议,但其中没有一个似乎对我有用,例如将 BackgroundImageLayout 设置为 center 等。

我想作为最后的手段,我可​​以让它手动循环浏览图像,就像它是一个 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 动画吗?
  • 正如问题中所说,我已经解决了这些类似的问题,但它们的答案并不能解决我的问题。至于第一条评论 - 怎么样?
  • 您可以使用photoshop制作按钮图形并将动画gif添加到它的背景中,并导出为动画gif。
  • 这是可能的,尽管我必须更改我的项目的相当数量,因为这些按钮具有事件处理程序/onclick 弹出框等。

标签: c# .net button controls animated-gif


【解决方案1】:
/// <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);

        }

【讨论】:

  • 抱歉,忘记在winforms中添加了。
  • 出于某种原因,当我这样引用它们时:“mainUI.Controls["btn" + device.deviceButtonNumber].whatever", .Image 不在智能感知中..
  • 我的问题是找不到控件。我可以访问 .backgroundimage 和所有其他选项,但不仅仅是 .image
  • 错误 3“System.Windows.Forms.Control”不包含“Image”的定义,并且没有扩展方法“Image”接受“System.Windows.Forms.Control”类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)实际上查看此异常是有道理的。也许我需要通过另一种将其转换为按钮的方式来获取该按钮。这样我就可以使用正确的参数 .image
【解决方案2】:

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

【讨论】:

  • 刚试过这个,gif还是不行。也许这是我的文件本身的错误。
猜你喜欢
  • 2013-03-28
  • 2020-12-13
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
相关资源
最近更新 更多