CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question How to use images in imagelist as background images for buttons [C#]

    Okay - as many people suggested I created an imagelist and added my 3 pictures to it (as shown in the code below).
    Problem I am having now, no clue how to get these now added images as background images to my buttons.
    (...if only ImageButtons existed...)

    Specifically - I have 3 buttons (btnStart, btnPause, btnStop) and I want to display the corresponding START, PAUSE, STOP images as their background.
    But I have no clue how to a) use the imagelist images I just added and b) set a background image to a button via code.

    Code:
    private System.Windows.Forms.Button btnStart;
    private System.Windows.Forms.Button btnPause;
    private System.Windows.Forms.Button btnStop;
    private System.Windows.Forms.ImageList imglst;
    ...
    
    // Add Images to ImageList and display on buttons
    imglst.Images.Add(Image.FromFile("C:\\img\\Play.png"));
    imglst.Images.Add(Image.FromFile("C:\\img\\Pause.png"));
    imglst.Images.Add(Image.FromFile("C:\\img\\Stop.png"));
    btnStart.?
    btnPause.?
    btnStop.?
    As you can see, I loaded my images... now what? How do I use them? How to I set them as the background image for the buttons?
    Thanks - I know I am asking for a lot but can't see to figure this part out, any help/hints would be appreciated.

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: How to use images in imagelist as background images for buttons [C#]

    The Images property that you added the Images to is a collection, so just like any other collection you index it to get the item at that location. You can then assign that Image to the BackgroundImage property of the Button. You can set the foreground image of the Button by setting the ImageList and ImageIndex properties of the Button. The advantage of that is that you can simply increment and decrement the ImageIndex property to change the Image.

    I'm would ask though, how do you know that these images will exist in that location on any machine that your app gets installed on? I would think that you would just set the BackgroundImage property in the designer, which will then simply incorporate those images into your executable.

  3. #3
    Join Date
    Oct 2004
    Posts
    429

    Re: How to use images in imagelist as background images for buttons [C#]

    Problem is I needed the IMAGES to fit the background color of my application - so while looking around everyone told me to use an IMAGELIST and then put those images as the background image to my buttons.

    Now I see what you mean (given the indexes and all that) I just have no clue how to actually DO IT, thats my problem...

  4. #4
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: How to use images in imagelist as background images for buttons [C#]

    If by "it" you mean your original question, you just index the Images collection and assign to the BackgroundImage property, e.g.:
    Code:
    myButton.BackgroundImage = myImageList.Images(0)
    If you mean setting the image in the designer, you simply go to the Properties window for the button in question and set the BackgroundImage property by browsing, which will then automatically incorporate that image into your executable so you need do nothing more. If you mean something else, you'll have to specify.

  5. #5
    Join Date
    Oct 2004
    Posts
    429

    Re: How to use images in imagelist as background images for buttons [C#]

    Okay - that bit of code worked great but I have one last question...
    The size of the .PNG is a lot smaller then that of the actual button, currently there are like 6 copies of the START/PAUSE/STOP images in each button.

    I need to FIT/FILL/STRETCH the .PNG to fix the size of the button so there is only ONE start/pause/stop image per button (as it should be logically).

    Any clues?
    Last edited by Shaitan00; August 30th, 2005 at 12:21 AM.

  6. #6
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: How to use images in imagelist as background images for buttons [C#]

    If the background image is smaller than the button then it's tiled and that's the way it is. You would need to work with an Image object and scale it to the size of the button if you want it stretched. If you want a single image that is smaller than the button then you would need to set it as a foreground image, by either setting the Image property or the ImageList and ImageIndex properties. You can then select where the image will be located by setting the ImageAlignment property.

  7. #7
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: How to use images in imagelist as background images for buttons [C#]

    As jmcilhinney said, the background image will be tiled if it is smaller than the button.
    In order to fix this you can do as follows:
    Create a new class, and inherit from Button.
    Add an event handler for the Resize event.
    code this:
    Code:
    private void MyControl_Resize(object sender,EventArgs e)
    {
        if (this.BackgroundImage == null)
             return;
        Bitmap pic = new Bitmap(this.BackgroundImage , this.Width, this.Height)
        this.BackgroundImage = pic;
    }
    Now, this works for Bitmap, but I am not sure if it will work for PNG, but I am sure you can work it out from here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured