Class to create thumbnail images (VB.Net, GDI.Net)
January 22nd, 2006
A class written in VB.Net using GDI.net to create thumbnail images and stream or save them as JPG.
Public Class JPGThumbnail
Private Enum Method
File
Stream
End Enum
Private Sub New()
End Sub
Public Shared Sub CreateThumbnail(ByVal InputFilePath As String, _
ByVal intMaxLen As Integer, _
ByVal OutputStream As IO.Stream)
CreateThumbnail(InputFilePath, intMaxLen, Method.Stream, , OutputStream)
End Sub
Public Shared Sub CreateThumbnail(ByVal InputFilePath As String, _
ByVal intMaxLen As Integer, _
ByVal OutputFilePath As String)
CreateThumbnail(InputFilePath, intMaxLen, Method.File, OutputFilePath)
End Sub
Private Shared Sub CreateThumbnail(ByVal InputFilePath As String, _
ByVal intMaxLen As Integer, _
ByVal ThumbnailMethod As Method, _
Optional ByVal OutputFilePath As String = "", _
Optional ByVal OutputStream As IO.Stream = Nothing)
Dim fileInfo As New IO.FileInfo(InputFilePath)
If fileInfo.Exists() Then
Dim bmp As System.Drawing.Bitmap
Dim bmpThumb As System.Drawing.Bitmap
Try
bmp = New System.Drawing.Bitmap(InputFilePath)
Dim intWidth As Integer = bmp.Width
Dim intHeight As Integer = bmp.Height
Dim intThumbWidth As Integer
Dim intThumbHeight As Integer
If intWidth > intHeight Then
intThumbWidth = intMaxLen
intThumbHeight = (intHeight / intWidth) * intMaxLen
Else
intThumbHeight = intMaxLen
intThumbWidth = (intWidth / intHeight) * intMaxLen
End If
Dim thumbSize As New Drawing.Size(intThumbWidth, intThumbHeight)
bmpThumb = New System.Drawing.Bitmap(bmp, thumbSize)
If intMaxLen >= intHeight Or intMaxLen >= intWidth Then
‘ Save the original
If ThumbnailMethod = Method.File Then
bmp.Save(OutputFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
Else
bmp.Save(OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
Else
‘ Save the thumbnail version
If ThumbnailMethod = Method.File Then
bmpThumb.Save(OutputFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
Else
bmpThumb.Save(OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
End If
Catch ex As Exception
Throw
‘WriteErrorMsg(OutputFilePath)
Finally
‘clean-up…
bmp.Dispose()
bmpThumb.Dispose()
End Try
Else
Throw New ApplicationException(”Input file [” & InputFilePath & ”] does not exist.”)
End If
End Sub
‘* This can be used to return an image even in an error condition, if that’s what you want.
‘Private Shared Sub WriteErrorMsg(ByVal OutputFilePath As String)
‘ Dim bmp As New System.Drawing.Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format16bppRgb565)
‘ ’ Get the underlying Graphics object.
‘ Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
‘ ’ Clear its background.
‘ gr.Clear(System.Drawing.Color.WhiteSmoke)
‘ ’ create a font
‘ Dim fnt As New System.Drawing.Font(”Arial”, 8, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
‘ gr.DrawString(”Not”, fnt, System.Drawing.Brushes.Blue, 36, 30)
‘ gr.DrawString(”Available”, fnt, System.Drawing.Brushes.Blue, 23, 50)
‘ Dim penRect As New System.Drawing.Pen(Color.LightBlue, 2)
‘ gr.DrawRectangle(penRect, 1, 1, 98, 98)
‘ ’ Release resources.
‘ fnt.Dispose()
‘ gr.Dispose()
‘ bmp.Save(OutputFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
‘ bmp.Dispose()
‘End Sub
End Class
Note: ‘Code to make use of it…
JPGThumbnail.CreateThumbnail(”C:\DCP02396.JPG”, 100, “C:\DCP02396_small.JPG”)Dim outputStream As New IO.FileStream(”C:\DCP02396_small2.jpg”, IO.FileMode.CreateNew)
JPGThumbnail.CreateThumbnail(”C:\DCP02396.JPG”, 100, outputStream)
outputStream.Flush()
outputStream.Close()
Entry Filed under: Programming


2 Comments Add your own
1. PSC | January 13th, 2007 at 11:40 am
should change from
intThumbHeight = (intHeight / intWidth) * intMaxLen
to
intThumbHeight =CType( (intHeight / intWidth) * intMaxLen, integer)
?
just because option explicit is switched on.
going to test your code soon..thanks.
2. Rob | October 7th, 2008 at 2:40 am
Just used your class in one of my sites. Works like a dream. Thanks.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed