Question Image stretch or reduced to fit inside button

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
i'm using VB.Net Application program.

i'm creating 64 button dynamically during run time. and from database i'm getting the description, font color, back color and picture path. and if the picture path is not nothing, then i need to display that picture in that button. if picture path is nothing, then need to display the description as button text using font color and back color.

this button values are not loaded only once. using button click Addhandler, each button values keeps on changing each time.

The Code i used to display is

VB.NET:
       For Each c As Control In Controls
            If c.Name = "ItemBtn-" & x Then
               c.Text = ButtonText
               c.ForeColor = System.Drawing.ColorTranslator.FromOle(btnFontColor)
               c.BackColor = System.Drawing.ColorTranslator.FromOle(btnBackColor)
               c.Tag = "ItemPageBtn-" & i
               c.Location = New System.Drawing.Point(gLeft, gTop)
	       If btnPicture <> "" And btnPicture <> Nothing Then
                   gButton.Image = Image.FromFile(btnPicture)
               End If
               c.Visible = True
               c.Visible = True
               Exit For
           End If
        Next

while i run the program, the image is diplaying in correct button if picture path exist and button that didn't have the picture path is displaying the text.

but the image is not fitting inside the Button. i can see only half of the image. i need a way to make the Image stretched or reduced to fit inside button.

is it possible??? if anyone have any idea how to do this, please help me. and if you can provide an example, then it will be great help for me.

Thanks in advance.
 
Use BackgroundImage instead of the image property and set the BackgroundImageLayout Property to Stretch. This will scale the image appropriately.
 
Thanks VIS781, thank you for your help.

Code i used to to display picture is

VB.NET:
dim img as new bitmap(filename)
dim img2 as new bitmap(img, mybutton.width, mybutton.height) 
mybutton.Image = img2

How can I remove the image from the button? is there a way to set the button image to "None"???

if anyone have any idea how to do this, please help me. and if you can provide an example, then it will be great help for me.

Thanks in advance.
 
VB.NET:
dim img as new bitmap(filename)
dim img2 as new bitmap(img, mybutton.width, mybutton.height) 
mybutton.Image = img2

Should be...

VB.NET:
dim img as new bitmap(filename)
mybutton.BackgroundImageLayout = ImageLayout.Stretch
mybutton.BackgroundImage = img
 
Back
Top