How do I Automatically Resize a control Button Image?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How do I Automatically Resize a control Button Image?

Hybrid View

  1. #1
    James Graham Guest

    How do I Automatically Resize a control Button Image?


    I'm pulling my hair out over what should be a simple problem...

    I've placed a button (call it ctlButton1) on a form.

    I've inserted an image from an imagelist on the button, not as a background
    image but just as an image.

    I've figured out how to resize the button itself, and in fact I can easily
    "anchor" the image (centered) on the button so that regardless of the buttons
    size, the image is always centered.

    What I cannot do is cause the image to scale along with the size of the button.


    My original problem was that I had written an activex control (a virtual
    LED) under VB6. During conversion, I discovered that not only was the lightweight
    image control gone from vb.net (I like its "stretch" property), and even
    though the new picturebox control under vb.net now has this stretch property,
    I couldn't figure out how to get my old resource file full of bmps (images
    of various colors and shapes for the LED) to work under vb.net. So I decided
    to put all of my LED images from the VB6 resource file into an imagelist.

    But...guess what.....vb.net's picturebox doesn't support an imagelist, but
    label controls and button controls DO. That's why I chose the button control.
    I'm using it as a container for my LED image.

    But I simply cannot resize the image. When I attempt to do so, I have found
    a way that I think should work, but it only appears to resize the button,
    and actually makes the button's image disappear.

    That code looks like this:

    btnLED_ON.ImageList.ImageSize = btnLED_ON.Size

    btnLED_ON is the button on which I wish to place the image of a specified
    index...say imageindex(41).

    What I THINK should happen is that the image on the button should become
    the same size as the button itself. Instead, the image on the button disappears
    altogether and I'm left with a flat-black button.

    Would someone please tell me how stupid I am?

    Thanks,
    Jim Graham
    --
    Indiana University Cyclotron Facility
    graham@iucf.indiana.edu

  2. #2
    Rob Teixeira Guest

    Re: How do I Automatically Resize a control Button Image?



    Forget about the button.

    Use the picturebox control and set the sizing to Stretched.

    You can load your images from resource or resourceX, but using an ImageList
    is just as good.

    Whenever you want to display the image on the picturebox, just type the following
    (assuming PictureBox1 and ImageList1 are the control names):

    Dim Index As Integer = 2
    PictureBox1.Image = ImageList1.Images(Index)

    It'll be stretched for you as well.

    -Rob


    "James Graham" <graham@iucf.indiana.edu> wrote:
    >
    >I'm pulling my hair out over what should be a simple problem...
    >
    >I've placed a button (call it ctlButton1) on a form.
    >
    >I've inserted an image from an imagelist on the button, not as a background
    >image but just as an image.
    >
    >I've figured out how to resize the button itself, and in fact I can easily
    >"anchor" the image (centered) on the button so that regardless of the buttons
    >size, the image is always centered.
    >
    >What I cannot do is cause the image to scale along with the size of the

    button.
    >
    >
    >My original problem was that I had written an activex control (a virtual
    >LED) under VB6. During conversion, I discovered that not only was the lightweight
    >image control gone from vb.net (I like its "stretch" property), and even
    >though the new picturebox control under vb.net now has this stretch property,
    >I couldn't figure out how to get my old resource file full of bmps (images
    >of various colors and shapes for the LED) to work under vb.net. So I decided
    >to put all of my LED images from the VB6 resource file into an imagelist.
    >
    >But...guess what.....vb.net's picturebox doesn't support an imagelist, but
    >label controls and button controls DO. That's why I chose the button control.
    > I'm using it as a container for my LED image.
    >
    >But I simply cannot resize the image. When I attempt to do so, I have found
    >a way that I think should work, but it only appears to resize the button,
    >and actually makes the button's image disappear.
    >
    >That code looks like this:
    >
    >btnLED_ON.ImageList.ImageSize = btnLED_ON.Size
    >
    >btnLED_ON is the button on which I wish to place the image of a specified
    >index...say imageindex(41).
    >
    >What I THINK should happen is that the image on the button should become
    >the same size as the button itself. Instead, the image on the button disappears
    >altogether and I'm left with a flat-black button.
    >
    >Would someone please tell me how stupid I am?
    >
    >Thanks,
    >Jim Graham
    >--
    >Indiana University Cyclotron Facility
    >graham@iucf.indiana.edu



  3. #3
    James Graham Guest

    Re: How do I Automatically Resize a control Button Image?


    Rob,
    Thank you!

    This works flawlessly.

    And I requested in my previous post, I now feel
    really STOOPID! This was a big "Doh" from the beginning.

    I also discovered that I can't simply make the image size and the form size
    (the control form) identical. This tends to clip the image when I anchor
    the image to all 4 sides of the form.

    Instead, I initialize the form to be image x+15 and image y+15. It appears
    that the form x and image x do not correspond in a 1/1 ratio.
    I've not researched this yet.

    Again, thanks for your advice.

    Jim Graham
    --
    graham@iucf.indiana.edu


    "Rob Teixeira" <RobTeixeira@@msn.com> wrote:
    >
    >
    >Forget about the button.
    >
    >Use the picturebox control and set the sizing to Stretched.
    >
    >You can load your images from resource or resourceX, but using an ImageList
    >is just as good.
    >
    >Whenever you want to display the image on the picturebox, just type the

    following
    >(assuming PictureBox1 and ImageList1 are the control names):
    >
    >Dim Index As Integer = 2
    >PictureBox1.Image = ImageList1.Images(Index)
    >
    >It'll be stretched for you as well.
    >
    >-Rob
    >
    >
    >"James Graham" <graham@iucf.indiana.edu> wrote:
    >>
    >>I'm pulling my hair out over what should be a simple problem...
    >>
    >>I've placed a button (call it ctlButton1) on a form.
    >>
    >>I've inserted an image from an imagelist on the button, not as a background
    >>image but just as an image.
    >>
    >>I've figured out how to resize the button itself, and in fact I can easily
    >>"anchor" the image (centered) on the button so that regardless of the buttons
    >>size, the image is always centered.
    >>
    >>What I cannot do is cause the image to scale along with the size of the

    >button.
    >>
    >>
    >>My original problem was that I had written an activex control (a virtual
    >>LED) under VB6. During conversion, I discovered that not only was the

    lightweight
    >>image control gone from vb.net (I like its "stretch" property), and even
    >>though the new picturebox control under vb.net now has this stretch property,
    >>I couldn't figure out how to get my old resource file full of bmps (images
    >>of various colors and shapes for the LED) to work under vb.net. So I decided
    >>to put all of my LED images from the VB6 resource file into an imagelist.
    >>
    >>But...guess what.....vb.net's picturebox doesn't support an imagelist,

    but
    >>label controls and button controls DO. That's why I chose the button control.
    >> I'm using it as a container for my LED image.
    >>
    >>But I simply cannot resize the image. When I attempt to do so, I have

    found
    >>a way that I think should work, but it only appears to resize the button,
    >>and actually makes the button's image disappear.
    >>
    >>That code looks like this:
    >>
    >>btnLED_ON.ImageList.ImageSize = btnLED_ON.Size
    >>
    >>btnLED_ON is the button on which I wish to place the image of a specified
    >>index...say imageindex(41).
    >>
    >>What I THINK should happen is that the image on the button should become
    >>the same size as the button itself. Instead, the image on the button disappears
    >>altogether and I'm left with a flat-black button.
    >>
    >>Would someone please tell me how stupid I am?
    >>
    >>Thanks,
    >>Jim Graham
    >>--
    >>Indiana University Cyclotron Facility
    >>graham@iucf.indiana.edu

    >



  4. #4
    Join Date
    Jul 2013
    Posts
    1
    well its really a good info about resizing image.i have also share the unique information with my fellows.

  5. #5
    Rob Teixeira Guest

    Re: How do I Automatically Resize a control Button Image?


    "James Graham" <graham@iucf.indiana.edu> wrote:
    >
    >Rob,
    >Thank you!


    You're welcome.

    >This works flawlessly.
    >
    >And I requested in my previous post, I now feel
    >really STOOPID! This was a big "Doh" from the beginning.


    Don't worry about it. There's a lot of new stuff in there. It takes a bit
    to get used to, but when you figure it out, it's often really easy. That's
    part of what sold me on .NET to begin with.

    >I also discovered that I can't simply make the image size and the form size
    >(the control form) identical. This tends to clip the image when I anchor
    >the image to all 4 sides of the form.
    >
    >Instead, I initialize the form to be image x+15 and image y+15. It appears
    >that the form x and image x do not correspond in a 1/1 ratio.
    >I've not researched this yet.


    Yeah, I haven't seen that yet either. I'll take a look and see what's going
    on.

    -Rob

  6. #6
    Join Date
    Oct 2011
    Location
    South East England
    Posts
    1
    You can't use the PictureBox control as it doesn't scale the image.
    PictureBox.AutoSize=true changes the height and width of the control and not the image.
    PictureBox.AutoSize=false will crop the image.

    You can use the Image control with Image.Stretch=True
    As the image control dose not have a click event
    Put it behind a label with a transparent background.

  7. #7
    Join Date
    Jul 2013
    Posts
    2
    Just as Rob said above, forget about the button. The only thing concerned is to resize the image.
    Try the code below.
    Code:
    Private Shared Function ResizeImage(image As Image, newsize As Size) As Image
    End Function

  8. #8
    Join Date
    May 2013
    Location
    Sydney
    Posts
    6
    mark! Useful!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center