How can I show image thumbnail??

T

Trint Smith

How can I show image thumbnail??
thanks,
Trint

..Net programmer
(e-mail address removed)
 
R

Rimu Atkinson

Trint Smith said:
How can I show image thumbnail??
thanks,
Trint

i made this class which uses the method that the other people suggested.

usage:

dim x as new ImageResizer

x.sourcefilename = "c:\whatever.jpg"
x.newwidth = 40 'you can specify either or both of newwidth and newheight
x.saveas("c:\filename.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

easy, huh?
Imports System.Drawing.Imaging

'this class was written by Rimu Atkinson on 2nd December 2002

'feel free to use this code for whatever purposes you like, but i'd
appreciate it if you kept this comment block in

Public Class ImageResizer

Private m_FileName As String

Public NewWidth As Integer = 0

Public NewHeight As Integer = 0

Property SourceFilename() As String 'the input file

Get

Return m_FileName

End Get

Set(ByVal Value As String)

Dim f As System.IO.File

If f.Exists(Value) Then

m_FileName = Value

Else

m_FileName = vbNullString

End If

End Set

End Property

Public Function SaveAs(ByVal ThumbFileName As String, ByVal FileType As
ImageFormat) As Boolean

Dim img As Bitmap

Dim ThumbNail As Bitmap

Dim callbackdata As IntPtr

Dim ratio As Single

Dim myImageCodecInfo As ImageCodecInfo, myEncoder As Encoder,
myEncoderParameters As EncoderParameters

Dim myEncoderParameter As EncoderParameter

If NewWidth = 0 And NewHeight = 0 Then Return False

If m_FileName = vbNullString Then Return False

myImageCodecInfo = GetEncoderInfo("image/jpeg")

myEncoder = Encoder.Quality

myEncoderParameters = New EncoderParameters(1)

myEncoderParameter = New EncoderParameter(myEncoder, 65)

myEncoderParameters.Param(0) = myEncoderParameter

img = New Bitmap(m_FileName)

'don't ask me why, but rotating the image 360 degrees improves the quality
no end!

img.RotateFlip(RotateFlipType.Rotate180FlipNone)

img.RotateFlip(RotateFlipType.Rotate180FlipNone)



If NewWidth <> 0 And NewHeight <> 0 Then

ThumbNail = img.GetThumbnailImage(NewWidth, NewHeight, AddressOf
AbortHandler, callbackdata)

ElseIf NewWidth <> 0 And NewHeight = 0 Then

ratio = NewWidth / img.Width

ThumbNail = img.GetThumbnailImage(NewWidth, img.Height * ratio, AddressOf
AbortHandler, callbackdata)

ElseIf NewWidth = 0 And NewHeight <> 0 Then

ratio = NewHeight / img.Height

ThumbNail = img.GetThumbnailImage(img.Width * ratio, NewHeight, AddressOf
AbortHandler, callbackdata)

End If

ThumbNail.Save(ThumbFileName, myImageCodecInfo, myEncoderParameters)

img.Dispose()

Return True

End Function

Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo

Dim j As Integer

Dim encoders() As ImageCodecInfo

encoders = imagecodecinfo.GetImageEncoders

For j = 0 To encoders.Length

If encoders(j).MimeType = mimeType Then

Return encoders(j)

End If

Next

Return Nothing

End Function

Private Function AbortHandler() As Boolean

'whatever.

End Function

End Class
 
T

Trint Smith

I haven't tried this yet, but there is also a sample in the help of
vs.net.
Thanks,
Trint

..Net programmer
(e-mail address removed)
 
J

Jon Paal

this thing fails to compile ...


Rimu Atkinson said:
i made this class which uses the method that the other people suggested.

usage:

dim x as new ImageResizer

x.sourcefilename = "c:\whatever.jpg"
x.newwidth = 40 'you can specify either or both of newwidth and newheight
x.saveas("c:\filename.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

easy, huh?
Imports System.Drawing.Imaging

'this class was written by Rimu Atkinson on 2nd December 2002

'feel free to use this code for whatever purposes you like, but i'd
appreciate it if you kept this comment block in

Public Class ImageResizer

Private m_FileName As String

Public NewWidth As Integer = 0

Public NewHeight As Integer = 0

Property SourceFilename() As String 'the input file

Get

Return m_FileName

End Get

Set(ByVal Value As String)

Dim f As System.IO.File

If f.Exists(Value) Then

m_FileName = Value

Else

m_FileName = vbNullString

End If

End Set

End Property

Public Function SaveAs(ByVal ThumbFileName As String, ByVal FileType As
ImageFormat) As Boolean

Dim img As Bitmap

Dim ThumbNail As Bitmap

Dim callbackdata As IntPtr

Dim ratio As Single

Dim myImageCodecInfo As ImageCodecInfo, myEncoder As Encoder,
myEncoderParameters As EncoderParameters

Dim myEncoderParameter As EncoderParameter

If NewWidth = 0 And NewHeight = 0 Then Return False

If m_FileName = vbNullString Then Return False

myImageCodecInfo = GetEncoderInfo("image/jpeg")

myEncoder = Encoder.Quality

myEncoderParameters = New EncoderParameters(1)

myEncoderParameter = New EncoderParameter(myEncoder, 65)

myEncoderParameters.Param(0) = myEncoderParameter

img = New Bitmap(m_FileName)

'don't ask me why, but rotating the image 360 degrees improves the quality
no end!

img.RotateFlip(RotateFlipType.Rotate180FlipNone)

img.RotateFlip(RotateFlipType.Rotate180FlipNone)



If NewWidth <> 0 And NewHeight <> 0 Then

ThumbNail = img.GetThumbnailImage(NewWidth, NewHeight, AddressOf
AbortHandler, callbackdata)

ElseIf NewWidth <> 0 And NewHeight = 0 Then

ratio = NewWidth / img.Width

ThumbNail = img.GetThumbnailImage(NewWidth, img.Height * ratio, AddressOf
AbortHandler, callbackdata)

ElseIf NewWidth = 0 And NewHeight <> 0 Then

ratio = NewHeight / img.Height

ThumbNail = img.GetThumbnailImage(img.Width * ratio, NewHeight, AddressOf
AbortHandler, callbackdata)

End If

ThumbNail.Save(ThumbFileName, myImageCodecInfo, myEncoderParameters)

img.Dispose()

Return True

End Function

Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo

Dim j As Integer

Dim encoders() As ImageCodecInfo

encoders = imagecodecinfo.GetImageEncoders

For j = 0 To encoders.Length

If encoders(j).MimeType = mimeType Then

Return encoders(j)

End If

Next

Return Nothing

End Function

Private Function AbortHandler() As Boolean

'whatever.

End Function

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top