-
Oct 1st, 2011, 12:02 AM
#1
Thread Starter
Hyperactive Member
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:
Public Function ScaleImage(ByVal OldImage As Image, ByVal TargetHeight As Integer,_ ByVal TargetWidth As Integer) As System.Drawing.Image Dim NewHeight As Integer = TargetHeight Dim NewWidth As Integer = NewHeight / OldImage.Height * OldImage.Width If NewWidth > TargetWidth Then NewWidth = TargetWidth NewHeight = NewWidth / OldImage.Width * OldImage.Height End If Return New Bitmap(OldImage, NewWidth, NewHeight) End Function
Example Usage:
VB.Net Code:
Picturebox1.Image = ScaleImage(Image.Fromfile(Picture.png),_ 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
-
Oct 1st, 2011, 05:26 PM
#2
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
-
Oct 1st, 2011, 05:46 PM
#3
Thread Starter
Hyperactive Member
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.
-
Oct 1st, 2011, 11:50 PM
#4
Re: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio
I made something similar ages ago:
vb Code:
Public Enum BestFitStyle
Zoom
Stretch
End Enum
Public Function GetBestFitRect(ByVal rectFrame As RectangleF, ByVal rect As RectangleF, Optional ByVal BestFitStyle As BestFitStyle = BestFitStyle.Zoom) As RectangleF
Dim origImageRatio = rect.Width / rect.Height
Dim canvasRatio = rectFrame.Width / rectFrame.Height
Dim DrawRect As RectangleF
If (origImageRatio > canvasRatio AndAlso BestFitStyle = Images.BestFitStyle.Stretch) OrElse (origImageRatio < canvasRatio AndAlso BestFitStyle = Images.BestFitStyle.Zoom) Then
DrawRect = New RectangleF(rectFrame.Left, rectFrame.Top, rectFrame.Width, rectFrame.Width * (1 / origImageRatio))
Else
DrawRect = New RectangleF(rectFrame.Left, rectFrame.Top, rectFrame.Height * origImageRatio, rectFrame.Height)
End If
DrawRect.X += ((rectFrame.Width - DrawRect.Width) / 2)
DrawRect.Y += ((rectFrame.Height - DrawRect.Height) / 2)
Return DrawRect
End Function
Public Sub DrawImageBestFit(ByVal g As Graphics, ByVal rect As RectangleF, ByVal image As Image, Optional ByVal BestFitStyle As BestFitStyle = BestFitStyle.Zoom)
Dim oldRegion = g.Clip.Clone
g.SetClip(rect, CombineMode.Intersect)
Dim DrawRect = GetBestFitRect(rect, New RectangleF(0, 0, image.Width, image.Height))
g.DrawImage(image, DrawRect)
g.Clip = oldRegion
End Sub
... Supports zoom and stretch (keping aspect) types
...draws the scaled image directly on the new graphics.
Kris
-
Oct 2nd, 2011, 07:17 AM
#5
Thread Starter
Hyperactive Member
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|