Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Click here to copy the following block
' 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 height

Sub 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 width

Sub 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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.