Monday, December 15, 2008

Resizing a Picture with VB.NET

This is a little snippet of code that I wrote in order to resize a picture. This code should run in a shared environment just fine as long as your web application has write permission on your directory where the picture resides.

A few things to know about this code:

1) The parameters that this code takes are as follows: FolderPath, FileName, ConstrainSize, NewWidth, NewHeight

  1. FolderPath as String - This is the path portion of the file location. Please note that the path must have the final slash after the last directory. For example: "C:\Documents And Settings\"

  2. FileName as String - This is the name of the file. Please include the extension. Also, please note that this function will only accept JPGs. For example: "MyPicture.jpg"

  3. ConstrainSize as Boolean - This value tells the function whether or not to constrain the size of the picture so that it can resize proportionally.

  4. Optional NewWidth as Int32 - This value is the integer value of the new width for the image. This is in pixels.

  5. Optional NewHeight as Int32 - This value is the integer value of the new height for the image. This is in pixels


2) There are some tricks to this code.

  1. Even though the NewWidth and NewHeight values are optional, you MUST enter a value in at least one of them.

  2. If you enter the value of 0 as the NewWidth or NewHeight property, it is the same thing as entering nothing into the function call.

  3. When setting the ConstrainSize property to True, you can only enter a value for EITHER the width or the height of the picture, but not both. If you just think about it, it will make sense.

  4. When setting the ConstrainSize property to False, you MUST have BOTH values for the new height and the new width.


Some examples of calling this function:

Dim picResized as Boolean = ResizePicture("C:\Documents and Settings\Horton\Images\", "HelloWorld.jpg", True, 150)
In the example above, the picture HelloWorld.jpg will be resized with the new width = 150 pixels and the new height determined proportionally from the original picture

Dim picResized as Boolean = ResizePicture("C:\Documents and Settings\Horton\Images\", "HelloWorld.jpg", False, 150, 200)
In the example above, the picture HelloWorld.jpg will be resized with the new width = 150 pixels and the new height = 200 pixels

Dim picResized as Boolean = ResizePicture("C:\Documents and Settings\Horton\Images\", "HelloWorld.jpg", True, , 200)
In the example above, the picture HelloWorld.jpg will be resized with the new height = 200 pixels and the new width determined proportioally from the original picture

Anyway, enjoy this code block! Post a comment about it if you want!

PLEASE NOTE: I wrote this code myself. If you have any problems or issues with it, I am going to assume that you are knowledgeable to help yourself out. If you've really got a perplexing situation / question, leave me a comment.

With that said, I am not responsible for this being used in production type environments. USE AT YOUR OWN RISK!

Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Microsoft.VisualBasic

Public Class Functions

Public Shared Function ResizePicture(ByVal FolderPath As String, ByVal FileName As String, ByVal ConstrainSize As Boolean, Optional ByVal NewWidth As Int32 = 0, Optional ByVal NewHeight As Int32 = 0) _
As Boolean
'Checking to see if numbers were provided for EITHER newWidth OR newHeight
If newWidth = 0 And newHeight = 0 Then Throw New ApplicationException("You must supply EITHER a newWidth OR a newHeight in order to resize a picture")
'The following code resizes the picture
Dim bm As New Bitmap(FolderPath & FileName)
Dim curWidth As Integer = bm.Width
Dim curHeight As Integer = bm.Height
Dim x As Int32, y As Int32
If ConstrainSize Then 'Checking to see if it is supposed to constrain the sizes
If Not newWidth = 0 And Not newHeight = 0 Then
Throw New ApplicationException("ResizePicture cannot contain both a newWidth and a newHeight if the ConstrainSize property is set to True.")
Else
If Not newWidth = 0 Then
x = newWidth 'variable for new width size
y = CInt(x * curHeight / curWidth) 'variable for new height size
End If
If Not newHeight = 0 Then
y = newHeight 'variable for new height size
x = CInt(y * curWidth / curHeight) 'variable for new height size
End If
End If
Else 'If the ConstrainSize property is set to False
If Not newWidth = 0 And Not newHeight = 0 Then
x = newWidth 'variable for new width size
y = newHeight 'variable for new height size
Else
Throw New ApplicationException("ResizePicture cannot have Null or ""0"" values for newWidth and newHeight if the ConstrainSize property is set to False.")
End If
End If
Dim width As Integer = Val(x) 'image width.
Dim height As Integer = Val(y) 'image height.
Dim thumb As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(thumb)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(bm, New Rectangle(0, 0, width, height), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
g.Dispose()
bm.Dispose()
File.Delete(FolderPath & FileName)
thumb.Save(FolderPath & Left(FileName, Len(FileName) - 3) & "jpg", ImageFormat.Jpeg) 'can use any image format
thumb.Dispose()
End Function 'ResizePicture

End Class

No comments: