tags:

views:

167

answers:

2

Hi,

I search a button control with a image, which auto size the image - the normal button don't do this.

It's C#.Net 2.0.

E.g.: Button is 200x 50px and the image is 800x100, I will resize the Image that is little left near the text of the button. With a Pic-Box I can do this. But when I lay a picBox over the button its very ugly because you can#t click there.

+1  A: 

You can do this as follows:

button.Image = Image.FromFile(path);
button.AutoSize = true;

E.g: Or, You can create a new Button type that will change the size of the image:

public class AutoSizeButton : Button
{

    public new Image Image
    {
        get { return base.Image; }
        set 
        {
            Image newImage = new Bitmap(Width, Height);
            using (Graphics g = Graphics.FromImage(newImage))
            {
                g.DrawImage(value, 0, 0, Width, Height);
            }
            base.Image = newImage;
        }
    }
}

Test:

AutoSizeButton button = new AutoSizeButton();
button.Location = new Point(27, 52);
button.Name = "button";
button.Size = new Size(75, 23);
button.Text = "Test";
button.UseVisualStyleBackColor = true;
button.Image = Image.FromFile(path);
Controls.Add(button);
mykhaylo
taht Autosize the Button to the Image, not the Image to the Button.To make it clear I will edit my question.
Kovu
A: 

I originally proposed using a standard ImageButton but then read your comment that you are trying to size the button to the image. For that use a LinkButton:

<asp:LinkButton ID="foo" runat="server" OnClick="LinkButton1_Click">   
    <asp:Image ID="imagefoo" runat="server" ImageUrl="~/Foo.jpg" />
</asp:LinkButton>
Thomas