ResizeImage – Resize an image, and optionally keep the original ratio automatically

' Resize the specified image file - The new dimensions are expressed in ' percentage of the original size.' The resized image overwrites the original file.' Note: requires the GetImageFormat function'' Example:'    ResizeImage("D:sample.gif", 50.0, 50.0) ' make the image half the ' original size'    ResizeImage("D:sample.gif", 200.0, 300.0) ' double the width and triple ' the heightSub ResizeImage(ByVal imgPath As String, ByVal widthPerc As Double, _    ByVal heightPerc As Double)    ' throw an exception if not both arguments are positive values    If widthPerc <= 0 OrElse heightPerc <= 0 Then        Throw New ArgumentException("widthPerc AND heightPerc arguments must be " _            & "positive values")    End If    Dim currWidth, currHeight As Integer    Dim newWidth, newHeight As Integer    ' get the image's current width/height    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(imgPath)    currWidth = img.Width    currHeight = img.Height    img.Dispose()    ' calculate the new size in pixel    newWidth = currWidth * widthPerc / 100    newHeight = currHeight * heightPerc / 100    ' call the overloaded method that takes explicit width/height in pixels    ResizeImage(imgPath, newWidth, newHeight)End Sub' This overloaded version takes the new width/height in pixel,'  instead of the percentage of the original size.' Either the width/height (but not both) can be 0,'  and the size will be calculate to maintain the ratio of the original image' Example:'    ResizeImage("D:sample.gif", 100, 50) ' resize to 100x50'    ResizeImage("D:sample.gif", 100, 0)  ' the height will be calculated ' according to the new widthSub ResizeImage(ByVal imgPath As String, ByVal width As Integer, _    ByVal height As Integer)    ' throw an exception if both arguments are not positive integers    If width <= 0 AndAlso height <= 0 Then        Throw New ArgumentException("Width and/or Height arguments must be " _            & "positive integers")    End If    ' select the format of the image to write according to the current extension    Dim imgFormat As System.Drawing.imaging.ImageFormat = GetImageFormat _        (imgPath)    ' open the image file    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(imgPath)    ' if either the specified height or width are 0, calculate it to maintain     ' the same ratio of the original image    If width <= 0 OrElse height <= 0 Then        If width <= 0 Then            width = img.Width / (img.Height / height)        ElseIf height <= 0 Then            height = img.Height / (img.Width / width)        End If    End If    ' create a new empty bitmpat with the specified size    Dim bmp As New System.Drawing.Bitmap(width, height)    ' retrieve a canvas object that allows to draw on the empty bitmap    Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage( _        DirectCast(bmp, System.Drawing.Image))    ' copy the original image on the canvas, and thus on the new bitmap,    '  with the new size    g.DrawImage(img, 0, 0, width, height)    ' close the original image    img.Dispose()    ' save the new image with the proper format    bmp.Save(imgPath, imgFormat)    bmp.Dispose()End Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS