Home
Manage Your Code
Snippet: Create Thumbnail (VB.NET)
Title: Create Thumbnail Language: VB.NET
Description: Creates a thumbnail from a bitmap stored in a bytearray Views: 1270
Author: Charles Cherry Date Added: 2/25/2009
Copy Code  
1	Private Function CreateThumbnail(ByVal PageImageBytes As Byte()) As Bitmap
2
3		If PageImageBytes.Length = 0 Then
4			Throw New ArgumentNullException("PageImage is required.")
5		End If
6
7		Dim thumbW As Integer = 100
8		Dim sf As Double = 0
9		Dim imgSource As System.Drawing.Bitmap = Nothing
10		Dim imgThumb As System.Drawing.Bitmap = Nothing
11		Dim msBmpTmp As Bitmap = Nothing
12		Dim msSource As IO.MemoryStream = Nothing
13		Dim msDest As IO.MemoryStream = Nothing
14		Dim recDest As Rectangle = Nothing
15		Dim gphCrop As Graphics = Nothing
16		Dim myEncoder As System.Drawing.Imaging.Encoder = Nothing
17		Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter = Nothing
18		Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters = Nothing
19		Dim arrayICI() As System.Drawing.Imaging.ImageCodecInfo = Nothing
20		Dim jpegICI As System.Drawing.Imaging.ImageCodecInfo = Nothing
21
22		Try
23
24			msSource = New IO.MemoryStream(PageImageBytes)
25
26			msDest = New IO.MemoryStream()
27
28			imgSource = DirectCast(System.Drawing.Bitmap.FromStream(msSource), Bitmap)
29
30			If (imgSource.Width > thumbW) Then
31
32				sf = imgSource.Width / thumbW
33
34				imgThumb = New System.Drawing.Bitmap(thumbW, CInt(imgSource.Height / sf))
35
36				recDest = New Rectangle(0, 0, thumbW, imgThumb.Height)
37
38				gphCrop = Graphics.FromImage(imgThumb)
39				gphCrop.SmoothingMode = SmoothingMode.HighQuality
40				gphCrop.CompositingQuality = CompositingQuality.HighQuality
41				gphCrop.InterpolationMode = InterpolationMode.High
42				gphCrop.DrawImage(imgSource, recDest, 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel)
43
44			Else
45
46				imgThumb = imgSource
47
48			End If
49
50			arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
51			jpegICI = Nothing
52
53			For x As Integer = 0 To arrayICI.Length - 1
54				If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
55					jpegICI = arrayICI(x)
56					Exit For
57				End If
58			Next
59
60			myEncoder = System.Drawing.Imaging.Encoder.Quality
61			myEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
62			myEncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, 60L)
63			myEncoderParameters.Param(0) = myEncoderParameter
64
65			imgThumb.Save(msDest, jpegICI, myEncoderParameters)
66
67		Finally
68
69			If Not imgSource Is Nothing Then
70				imgSource.Dispose()
71				imgSource = Nothing
72			End If
73
74			If Not gphCrop Is Nothing Then
75				gphCrop.Dispose()
76				gphCrop = Nothing
77			End If
78
79			If Not msBmpTmp Is Nothing Then
80				msBmpTmp.Dispose()
81				msBmpTmp = Nothing
82			End If
83
84			msSource = Nothing
85			msDest = Nothing
86			recDest = Nothing
87			myEncoder = Nothing
88			myEncoderParameter = Nothing
89			myEncoderParameters = Nothing
90			arrayICI = Nothing
91			jpegICI = Nothing
92
93		End Try
94
95		Return imgThumb
96
97	End Function
98
Usage
'assumes pageImageBytes is a byte array containing an image
Dim myThumbnail as Image = CreateThumbnail(pageImageBytes)