Results 1 to 8 of 8

Thread: Image resize?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    6

    Image resize?

    Hi

    I am still a beginner in this, so sorry if it is a silly question.
    In my project I need to change the size of an image (which btw, is in a picturebox) but I can't manage to simply change the width and height of it like I did with the picturebox or some buttons.

    I would like something that would do something similar to:

    ' x is a variable, eg. 500
    picturebox1.image.width=x*0.5
    picturebox1.image.height=x*0.4

    Is there any command to do this?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Image resize?

    Nope. If you want to resize an image in a picturebox you have to resize the box (and use the appropriate SizeMode of course). The control is designed for display only, not graphic manipulation.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Image resize?

    You can't change the size of an existing Image. What you need to do is create a new Bitmap object of the desired size, create a Grpahics object from that and then use the Graphics to draw the original Image onto the new Bitmap. There are lots of examples of that already on the web. I just searched Bing for vb.net resize image and the first three matches, which were the only ones I looked at, all demonstrate this technique. That just shows that you should always be searching the web first. Forums are great for the hard questions but this is not one of the hard questions.

    http://www.bing.com/search?q=vb.net+resize+image
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Image resize?

    Quote Originally Posted by dunfiddlin View Post
    Nope. If you want to resize an image in a picturebox you have to resize the box (and use the appropriate SizeMode of course). The control is designed for display only, not graphic manipulation.
    Good point. I was concentrating on the size of the Image itself. Depending on the SizeMode of the PictureBox, resizing the PictureBox will change what the user sees of the Image without changing the Image itself. One issue with that is the quality of the resized image.

    If you actually want an Image with a different size then use the technique I mentioned.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Image resize?

    I agree, the PictureBox SizeMode property is the first thing you need to learn. It applies to the Image. The BackgroundImageLayout property applies to the BackgroundImage but can also be useful (for example for tiling).

    But to make a new Image with a different size, for most purposes it only takes a single statement:
    Code:
    Dim resizedImage As New Bitmap(sourceImage, width, height)
    The more complicated methods involving redrawing the image are useful when you want to enlarge the original with a higher quality than default. In that case you can use DrawImage in combination with one of the high quality Interpolation modes, Bilinear or Bicubic. Which of these looks best depends on the image itself. Bilinear can preserve linear detail better, but otherwise Bicubic is preferable. The low-quality modes can also be useful, for example NearestNeighbor (which enlarges each pixel as a square block and is also faster than anything else).

    BB

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Image resize?

    Quote Originally Posted by boops boops View Post
    I agree, the PictureBox SizeMode property is the first thing you need to learn. It applies to the Image. The BackgroundImageLayout property applies to the BackgroundImage but can also be useful (for example for tiling).

    But to make a new Image with a different size, for most purposes it only takes a single statement:
    Code:
    Dim resizedImage As New Bitmap(sourceImage, width, height)
    The more complicated methods involving redrawing the image are useful when you want to enlarge the original with a higher quality than default. In that case you can use DrawImage in combination with one of the high quality Interpolation modes, Bilinear or Bicubic. Which of these looks best depends on the image itself. Bilinear can preserve linear detail better, but otherwise Bicubic is preferable. The low-quality modes can also be useful, for example NearestNeighbor (which enlarges each pixel as a square block and is also faster than anything else).

    BB
    I've never actually tried to do this for myself so I've never researched it thoroughly and I was therefore unaware of that constructor. I learned something new.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Image resize?

    PictureBox1.Image = New Bitmap(PictureBox1.Image, 20, 20)
    Make me out to be a liar then. See if I care!

    PictureBox1.Image = New Bitmap(PictureBox1.Image, 50, 50)

    Image resized in its own picture box!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Image resize?

    Quote Originally Posted by dunfiddlin View Post
    Make me out to be a liar then. See if I care!

    PictureBox1.Image = New Bitmap(PictureBox1.Image, 50, 50)

    Image resized in its own picture box!
    Whenever you declare something is impossible, we can be sure it means there is an easy way to do it. (I tried to +rep but it's too soon!)

Tags for this Thread

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