Results 1 to 3 of 3

Thread: Change image size from text

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2021
    Posts
    41

    Change image size from text

    I am having a problem, I want to change the image size from text and so what I have is
    Code:
    Image.Size(WidthTXT.Text, HeightTXT.Text)
    but it says there are too many arguments, so I then tried
    Code:
    Image.Width(WidthTXT.Text)
    SourceImg.Height(HeightTXT.Text)
    But It gives me for both lines "Public Overloads ReadOnly Property Width As Integer" and "Public Overloads ReadOnly Property Height As Integer"
    I put in actual number instead of the textbox.txt and no error, but I want to be able to change the size manually from the textbox. How can I do this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Change image size from text

    There's all sorts of wrong that indicate that you don't understand the fundamentals of VB.NET programming. You should go back and work your way through a beginners tutorial and learn the basics. In this case, it's how properties and methods work. There's a tutorial link in my signature.

    If you understood properties and methods then you would know that you can't call properties like they are methods. Size, Height and Width are all properties so you either get a value from them or assign a value to them. In this case though, all three properties are read-only too, so you can only get a value from them.

    The fact is that you cannot change the dimensions of an Image object. If you had searched the web for information on the subject then you'd have found that what you have to do is create a new Bitmap object of the appropriate dimensions (Bitmap inherits Image) and then use GDI+ to draw the existing Image onto the new one. There's loads of information and examples around. I just searched the web for "vb.net resize image" and got plenty of results. You should have done that first, before posting here. Sites like this one should be for stuff that you can't work out for yourself but you haven't really tried to work it out for yourself if you haven't done a web search on the subject.

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

    Re: Change image size from text

    Hi YourMomsFire, it helps to give names to the bits and pieces of data that you use, especially when you are just learning. I suppose you already know how to use the Dim statement to give a name to something. And you must also pay attention to the Types such as Integer and Image.

    One way to get your original image is to load it into a picture box in the Forms Designer. In your code, give the image a name something like this:
    Code:
    Dim sourceImage As Image = PictureBox1.Image
    (Note: names beginning with a small letter are ones I have just made up as an example.)

    You have already have two text boxes named WidthTXT and HeightTXT. A number in a TextBox is purely text, so you have convert it into a numeric type, for example like this.
    Code:
    Dim newWidth As Integer = CInt(WidthTXT.text)
    Dim newHeight As Integer = Cint(HeightTXT.text)
    CInt will only work if the text boxes don't contain errors such 100A or whatever. To prevent crashes you should use the slightly more complex TryCast method.

    There are various ways to resize an image, including the one mentioned by JMcIlhinney. A relatively simple alternative is as follows. Then you can load the resized image into the same picture box or another one to see the result of resizing, for example:
    Code:
         Dim resizedImage As Image = New Bitmap(sourceImage, newWidth, newHeight)
         PictureBox2.Image = resizedImage
    You should put all the above lines of code in an event handler such as a Form1.Load or Button1.Click sub.

    BB

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