-1

I have a .gif file which i wish to use as the background image for a button. The main reason i want to do this (as i know some of you will wonder why i want to do it this way) is because, a button relates to an instance of a class, and i want to monitor these classes. when a class (and therefore button) goes into an "alarmed" state, i wish i use these animated gif. i've tried;

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

But it is to my understanding that .BackgroundImage does not support animated gifs. i've tried a few suggestions throughout stackoverflow and non of these seem to work for me, such as setting BackgroundImageLayout to center and such.

I suppose as a last resort i could get it to cycle through the images manually as if it were a gif, but this would create more work and i'd like to keep things simple. Any suggestions?

edit: sorry, forgot to add, this is in Winforms.

edit 2:

for example. when i create my buttons dynamically, like so;

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

I can still access:

 btnAdd.Image  <- note i can access image.

then when i come to modify them later from another thread, i find them like so;

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

however i CANNOT access;

 mainUI.Controls["btn" + device.deviceButtonNumber].Image
Kestami
  • 2,045
  • 3
  • 32
  • 47
  • Can't you make the whole button an animated gif? – Clowerweb Jun 12 '12 at 13:00
  • Also see possible duplicate: http://stackoverflow.com/questions/825990/how-to-add-animated-gif-to-a-button – Clowerweb Jun 12 '12 at 13:01
  • as said in the question, i've ran through these questions similar and the answers on them do not solve my problem. as for the first comment - how? – Kestami Jun 12 '12 at 13:02
  • You can use photoshop to make the button graphic and add the animated gif to the background of it, and export as animated gif. – Clowerweb Jun 12 '12 at 13:05
  • this is possible, although i'd have to change considerate amounts of my project, as these buttons have event handlers / onclick popup-boxes and such. – Kestami Jun 12 '12 at 13:08

2 Answers2

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

        }
MMK
  • 3,673
  • 1
  • 22
  • 30
  • sorry, forgot to add this is in winforms. – Kestami Jun 12 '12 at 13:14
  • for some reason when i reference them like this: "mainUI.Controls["btn" + device.deviceButtonNumber].whatever", .Image isn't in the intelisense.. – Kestami Jun 12 '12 at 14:38
  • http://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls – MMK Jun 12 '12 at 15:01
  • my problem isn't finding the control. i have access to .backgroundimage and all other options, but not just .image – Kestami Jun 12 '12 at 15:08
  • Error 3 'System.Windows.Forms.Control' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'System.Windows.Forms.Control' could be found (are you missing a using directive or an assembly reference?) actually looking at this exception makes sense a bit. perhaps i need to get the button by another means that casts it as a button. that way i can use the right parameter .image – Kestami Jun 12 '12 at 16:03
  • Use casting ((Button)mainUI.Controls["button1"]).Image – MMK Jun 12 '12 at 16:19
  • i tried (button) but i didn't encase the entire control in it. it's always silly things like that. – Kestami Jun 12 '12 at 16:21
0

I would recommend to use a PictureBox instead of Button , PictureBox supports .gif images as well as click event

Ravi Jain
  • 1,452
  • 2
  • 17
  • 41
  • just tried this, the gif still doesn't want to work. perhaps this is an error with my file itself. – Kestami Jun 12 '12 at 13:28