Results 1 to 6 of 6

Thread: Simple Image Resize Function

  1. #1

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Resolved Simple Image Resize Function

    vb.net Code:
    1. Public Shared Function ResizeImage(ByVal InputImage As Image, ByVal NewWidth As Integer, ByVal NewHeight As Integer) As Image
    2.         Return New Bitmap(InputImage, New Size(NewWidth, NewHeight))
    3. End Function

    Use it like this:

    vb.net Code:
    1. ResizeImage(YourImage, Width, Height)

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Simple Image Resize Function

    Quote Originally Posted by berny22 View Post
    [HIGHLIGHT=vb.net]
    ...
    Use it like this:
    vb.net Code:
    1. ResizeImage(YourImage, Width, Height)
    Shouldn't that be somthing like
    Code:
    YourImage = ResizeImage(YourImage, Width, Height)
    or
    NewImage = ResizeImage(YourImage, Width, Height)
    Of course since you can use the constructor directly with the same parameters, is there really a reason to slow it down by wrapping it in another function?
    Code:
    NewImage = New Bitmap(YourImage, Width, Height)
    An extra function call does really add a relative fair amount of overhead.

  3. #3

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Re: Simple Image Resize Function

    Quote Originally Posted by passel View Post
    Shouldn't that be somthing like
    Code:
    YourImage = ResizeImage(YourImage, Width, Height)
    or
    NewImage = ResizeImage(YourImage, Width, Height)
    Of course since you can use the constructor directly with the same parameters, is there really a reason to slow it down by wrapping it in another function?
    Code:
    NewImage = New Bitmap(YourImage, Width, Height)
    An extra function call does really add a relative fair amount of overhead.
    You don't need the New Image = .... thingy
    Because the function will automatically change the image.

    vb.net Code:
    1. Return New Bitmap

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Simple Image Resize Function

    Quote Originally Posted by berny22 View Post
    You don't need the New Image = .... thingy
    Because the function will automatically change the image.

    vb.net Code:
    1. Return New Bitmap
    I don't know what you're saying.
    You're calling a function and passing an image to it.
    The function will return a new image with a resized Bitmap in it.
    If you don't assign the return value of the function to something, then the new image is lost (should be a resource leak since you can't call Dispose on it).

    The function will not modify the original image you passed to it.

    Even if you did
    Picturebox1.Image = ResizeImage(Picturebox1.Image, 100, 100)

    that seems "dangerous" to me, since you're replacing Picturebox1.Image with a new Image, but not disposing of the previous image, and no way to dispose of it since you've lost your reference.
    To dispose of the image, I guess there would be several ways of getting a copy of the original reference and disposing of it once you return from the function. The "cleanest" approach might be
    Code:
        Using PictureBox1.Image                                         'Get a copy of the existing reference
          PictureBox1.Image = ResizeImage(PictureBox1.Image, 100, 100)  '  Get a new image reference
        End Using                                                       'Dispose of the old reference
    The second best alternative, I guess, would be
    Code:
        Dim a As Image = PictureBox1.Image            'Get a copy of the existing reference
        PictureBox1.Image = ResizeImage(a, 100, 100)  'Get a new image reference
        a.Dispose()                                   'Dispose of the old
    Last edited by passel; Aug 15th, 2014 at 11:57 AM.

  5. #5

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Re: Simple Image Resize Function

    Quote Originally Posted by passel View Post
    I don't know what you're saying.
    You're calling a function and passing an image to it.
    The function will return a new image with a resized Bitmap in it.
    If you don't assign the return value of the function to something, then the new image is lost (should be a resource leak since you can't call Dispose on it).

    The function will not modify the original image you passed to it.

    Even if you did
    Picturebox1.Image = ResizeImage(Picturebox1.Image, 100, 100)

    that seems "dangerous" to me, since you're replacing Picturebox1.Image with a new Image, but not disposing of the previous image, and no way to dispose of it since you've lost your reference.
    To dispose of the image, I guess there would be several ways of getting a copy of the original reference and disposing of it once you return from the function. The "cleanest" approach might be
    Code:
        Using PictureBox1.Image                                         'Get a copy of the existing reference
          PictureBox1.Image = ResizeImage(PictureBox1.Image, 100, 100)  '  Get a new image reference
        End Using                                                       'Dispose of the old reference
    The second best alternative, I guess, would be
    Code:
        Dim a As Image = PictureBox1.Image            'Get a copy of the existing reference
        PictureBox1.Image = ResizeImage(a, 100, 100)  'Get a new image reference
        a.Dispose()                                   'Dispose of the old
    Don't know, you know more stuff so you're probably right, although it worked for me just the way I typed it. I don't think there is any need to remove the old image because Images don't get stacked. The image will just change to the resized one??

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Simple Image Resize Function

    You should post some short example code showing the function in action so we can see and test an implementation.
    There might be something else going on that makes it appear to work, such as having the zoom or stretch option selected on the picturebox you use for display.

    As far as disposing of the previous image before replacing it, I did some further reading on the IDisposable interface, and I guess I was misled somewhat in the past.
    My impression was that the Garbage Collector (GC) would not clean up Unmanaged Resources, so if you didn't do it yourself by calling .Dispose, you would orphan some unmanaged resources creating a resource leak.

    But, while the GC can't release the Unmanaged Resources directly, it will call the Finalize method of the IDisposable interface, which will call Dispose which should release those Unmanaged Resources.
    Also, if you call .Dispose, both the managed and unmanaged resources are released at that point, so the GC will have not have to "collect" those managed resource in the future.

    I guess the reason that calling .Dispose yourself when dealing with known unmanaged resource hogs, such as Graphics and I/O methods is emphasized to the point of being a necessary thing to do, is that while the GC will at some point in the future release the managed resources and, through the Finalize method, at the same time release the unmanaged resources, it doesn't actually track the unmanaged resources. Therefore while in a graphic intensive application, where you could be using up unmanaged resources at a faster rate than you're using up managed resources, the GC may "feel" you have plenty of managed resources available, so no pressure to have to collect any garbage yet so put off the collection until later, while at the same time the windows system as a whole could becoming sluggish, because unmanaged resources are getting tight.

    In a graphical environment where you may be constantly creating pens, and brushes, and bitmaps, etc. which all take unmanaged resources, it is in your best interest to call Dispose on those objects as soon as you're done with them, to free up both managed and unmanaged resources immediately to keep the system more responsive overall, rather than let the system becomes sluggish, because GC is unaware of how much unmanaged resources you've tied up with the uncollected garbage.
    Last edited by passel; Aug 16th, 2014 at 06:04 AM.

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