Hello,

I'm still pretty new at this so all suggestions are much appreciated. When I set the button background image in properties, it looks great. When the button is clicked, the image needs to change. I tried to use an image list like this:

btnSample.BackgroundImage = ImlStimuli.Images.Item(0)

and I tried loading them from a file. I can get them to read, but the image always turns out blurry or distorted. I've tried all variations of alignment, BackgroundImageLayout like stretch/zoom/centered, and I changed flat style, but nothing has worked. Is there a setting or something that I'm missing in properties?

Please help and thanks in advance!

Dani AI

Generated

If you are doing this in WinForms (your code suggests System.Drawing and MouseEnter/Leave), the blurriness usually comes from ImageList defaults. ImageList resizes everything to its ImageSize (16x16 by default) and may downsample to 8-bit color, so assigning those images back to a button will look soft or distorted. Fix it by configuring the list before adding images: set ColorDepth = Depth32Bit and ImageSize to exactly the button’s size. Also pick a BackgroundImageLayout that preserves aspect ratio (Zoom) or supply images that already match the button’s dimensions.

If you embed the files as Resources (recommended so paths do not break), each image gets a strongly-typed property under My.Resources. If you only see one, check that you added them under the Images tab and that their names are valid identifiers. For dynamic selection by key, you can bypass name issues (spaces, leading digits) using the resource manager:

Dim key As String = "Pressed"  ' e.g., "Idle", "Hover", "Pressed"
Dim img As Image = DirectCast(My.Resources.ResourceManager.GetObject(key), Image)
Button1.BackgroundImage = img
Button1.BackgroundImageLayout = ImageLayout.Zoom

Notes:

  • Do not dispose images returned by My.Resources while the form is alive.
  • If you must load from disk at runtime, clone the bitmap to avoid file locks.
  • For pixel-perfect results, size the button to the image rather than stretching it.

Recommended Answers

All 4 Replies

Hello,

I'm still pretty new at this so all suggestions are much appreciated. When I set the button background image in properties, it looks great. When the button is clicked, the image needs to change. I tried to use an image list like this:

btnSample.BackgroundImage = ImlStimuli.Images.Item(0)

and I tried loading them from a file. I can get them to read, but the image always turns out blurry or distorted. I've tried all variations of alignment, BackgroundImageLayout like stretch/zoom/centered, and I changed flat style, but nothing has worked. Is there a setting or something that I'm missing in properties?

Please help and thanks in advance!

Here's the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Image = System.Drawing.Image.FromFile("someimage.bmp")
    End Sub

Thanks, James, I appreciate your help.

Using this code, if I move the file or email the program to someone who doesn't save it in the same location, will the program not work correctly?

You can always add images to your Application's Resources.
..File Menu / Project / "your application's name" Properties.
....Resources Tab, top Button for "Add Resource", click Drop-Down arrow, select "Add Existing File...", and load your images.

To access them, view the following code sample.
This code is from me adding 2 images to my Resources, "green.png" and "red.png".

Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
        Button1.BackgroundImage = My.Resources.green
    End Sub

    Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
        Button1.BackgroundImage = My.Resources.red
    End Sub

Adding images as a Resource will keep them stored within your Application.

It looks like adding them to Resources will be better, but I added three images to my resources and it only allows me to use one of them. I can see them listed in the Solution Explorer window and I found them in the VB project folder, so I know they are there...

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.