Google+ Tools
Make Google+ profile picture
Make Google plus banners for profile
Create and share your Google Plus profile banners.

Profile image for mrk studios Charles Cherry on February 25, 2009
Creates a thumbnail from a bitmap stored in a bytearray
Language
VB.NET
Tags

Create Thumbnail


	Private Function CreateThumbnail(ByVal PageImageBytes As Byte()) As Bitmap

		If PageImageBytes.Length = 0 Then
			Throw New ArgumentNullException("PageImage is required.")
		End If

		Dim thumbW As Integer = 100
		Dim sf As Double = 0
		Dim imgSource As System.Drawing.Bitmap = Nothing
		Dim imgThumb As System.Drawing.Bitmap = Nothing
		Dim msBmpTmp As Bitmap = Nothing
		Dim msSource As IO.MemoryStream = Nothing
		Dim msDest As IO.MemoryStream = Nothing
		Dim recDest As Rectangle = Nothing
		Dim gphCrop As Graphics = Nothing
		Dim myEncoder As System.Drawing.Imaging.Encoder = Nothing
		Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter = Nothing
		Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters = Nothing
		Dim arrayICI() As System.Drawing.Imaging.ImageCodecInfo = Nothing
		Dim jpegICI As System.Drawing.Imaging.ImageCodecInfo = Nothing

		Try

			msSource = New IO.MemoryStream(PageImageBytes)

			msDest = New IO.MemoryStream()

			imgSource = DirectCast(System.Drawing.Bitmap.FromStream(msSource), Bitmap)

			If (imgSource.Width > thumbW) Then

				sf = imgSource.Width / thumbW

				imgThumb = New System.Drawing.Bitmap(thumbW, CInt(imgSource.Height / sf))

				recDest = New Rectangle(0, 0, thumbW, imgThumb.Height)

				gphCrop = Graphics.FromImage(imgThumb)
				gphCrop.SmoothingMode = SmoothingMode.HighQuality
				gphCrop.CompositingQuality = CompositingQuality.HighQuality
				gphCrop.InterpolationMode = InterpolationMode.High
				gphCrop.DrawImage(imgSource, recDest, 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel)

			Else

				imgThumb = imgSource

			End If

			arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
			jpegICI = Nothing

			For x As Integer = 0 To arrayICI.Length - 1
				If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
					jpegICI = arrayICI(x)
					Exit For
				End If
			Next

			myEncoder = System.Drawing.Imaging.Encoder.Quality
			myEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
			myEncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, 60L)
			myEncoderParameters.Param(0) = myEncoderParameter

			imgThumb.Save(msDest, jpegICI, myEncoderParameters)

		Finally

			If Not imgSource Is Nothing Then
				imgSource.Dispose()
				imgSource = Nothing
			End If

			If Not gphCrop Is Nothing Then
				gphCrop.Dispose()
				gphCrop = Nothing
			End If

			If Not msBmpTmp Is Nothing Then
				msBmpTmp.Dispose()
				msBmpTmp = Nothing
			End If

			msSource = Nothing
			msDest = Nothing
			recDest = Nothing
			myEncoder = Nothing
			myEncoderParameter = Nothing
			myEncoderParameters = Nothing
			arrayICI = Nothing
			jpegICI = Nothing

		End Try

		Return imgThumb

	End Function

Comments

blog comments powered by Disqus