Results 1 to 5 of 5

Thread: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio

  1. #1

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio

    First Submission to the CodeBank!

    While I was working on a program I found the need to display pictures, and (since I'm OCD) I needed them to be the correct aspect ratio. This sounds (and is) quite simple, but sometimes things get over complicated. It actually took me a few tries to figure this out, but what I ended up with was a Very Simple, Extremely Portable Function which can scale any System.Drawing.Image to any Target Size.
    While I was working on it, I did multiple searches for simmilar code, but what I found was usually WAY over complicated, or only scaled down. I'm Submitting this after searching the CodeBank and not finding anything resembling a scaling snippet. I'm hoping other people can benefit from the simplicity of this.

    Forgive me for not commenting, I believe it too simple to require comments (they just get in the way without saying anything about it).
    Modified by Suggestion of boops boops. Credit where due.

    VB.Net Code:
    1. Public Function ScaleImage(ByVal OldImage As Image, ByVal TargetHeight As Integer,_
    2.                            ByVal TargetWidth As Integer) As System.Drawing.Image
    3.  
    4.         Dim NewHeight As Integer = TargetHeight
    5.         Dim NewWidth As Integer = NewHeight / OldImage.Height * OldImage.Width
    6.  
    7.         If NewWidth > TargetWidth Then
    8.             NewWidth = TargetWidth
    9.             NewHeight = NewWidth / OldImage.Width * OldImage.Height
    10.         End If
    11.  
    12.         Return New Bitmap(OldImage, NewWidth, NewHeight)
    13.  
    14. End Function

    Example Usage:

    VB.Net Code:
    1. Picturebox1.Image = ScaleImage(Image.Fromfile(Picture.png),_
    2.                                Picturebox1.Height, Picturebox1.Width)

    Note: Set the Picturebox's SizeMode to CenterImage
    Last edited by stepdragon; Oct 1st, 2011 at 05:49 PM. Reason: Great Suggestion by BB

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

    Re: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio

    Hi stepdragon,

    Congratulations on your first CodeBank contribution. That's a function that a lot of people should find useful.

    It's neat but you could make the second half even neater. I would replace the lines from 12 onwards by this:
    Code:
    Return New Bitmap(Old Image, NewWidth, NewHeight)
    That automatically scales the old image to the new size.

    BB

  3. #3

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio

    I knew I saw a better way of doing that somewhere (I couldn't remember how to do that, so I used another code snippet) now it'll truly be fool-proof as everything is easy to read. I'll amend it right now.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  4. #4
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio

    I made something similar ages ago:

    vb Code:
    1. Public Enum BestFitStyle
    2.             Zoom
    3.             Stretch
    4.         End Enum
    5.  
    6.         Public Function GetBestFitRect(ByVal rectFrame As RectangleF, ByVal rect As RectangleF, Optional ByVal BestFitStyle As BestFitStyle = BestFitStyle.Zoom) As RectangleF
    7.             Dim origImageRatio = rect.Width / rect.Height
    8.             Dim canvasRatio = rectFrame.Width / rectFrame.Height
    9.             Dim DrawRect As RectangleF
    10.             If (origImageRatio > canvasRatio AndAlso BestFitStyle = Images.BestFitStyle.Stretch) OrElse (origImageRatio < canvasRatio AndAlso BestFitStyle = Images.BestFitStyle.Zoom) Then
    11.                 DrawRect = New RectangleF(rectFrame.Left, rectFrame.Top, rectFrame.Width, rectFrame.Width * (1 / origImageRatio))
    12.             Else
    13.                 DrawRect = New RectangleF(rectFrame.Left, rectFrame.Top, rectFrame.Height * origImageRatio, rectFrame.Height)
    14.             End If
    15.             DrawRect.X += ((rectFrame.Width - DrawRect.Width) / 2)
    16.             DrawRect.Y += ((rectFrame.Height - DrawRect.Height) / 2)
    17.             Return DrawRect
    18.         End Function
    19.  
    20.         Public Sub DrawImageBestFit(ByVal g As Graphics, ByVal rect As RectangleF, ByVal image As Image, Optional ByVal BestFitStyle As BestFitStyle = BestFitStyle.Zoom)
    21.             Dim oldRegion = g.Clip.Clone
    22.             g.SetClip(rect, CombineMode.Intersect)
    23.             Dim DrawRect = GetBestFitRect(rect, New RectangleF(0, 0, image.Width, image.Height))
    24.             g.DrawImage(image, DrawRect)
    25.             g.Clip = oldRegion
    26.         End Sub

    ... Supports zoom and stretch (keping aspect) types
    ...draws the scaled image directly on the new graphics.

    Kris

  5. #5

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio

    I was going to add some extra features to my code, but I have since decided against it. I like the simplicity of my code and how it is a useful way to properly display programs for programmers who don't work with graphics and want a simple easy and effective code snippet. If I make the code less simple to accomodate middle level programming needs, then I'm probably going to both not make it effective enough for the power user, and make it too complicated for the entry level user. Therefore I've decided to leave the code as is so that people who don't want to fuss with graphics can display images properly without thinking about it.
    Last edited by stepdragon; Oct 3rd, 2011 at 02:19 PM.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

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