Archive for January 22nd, 2006

Class to create thumbnail images (VB.Net, GDI.Net)

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()

2 comments January 22nd, 2006


Calendar

January 2006
M T W T F S S
« Sep   May »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Posts by Month

Posts by Category