"Trint Smith" <tr********@hot mail.com> wrote in message
news:OP******** ******@TK2MSFTN GP10.phx.gbl...
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.sourcefilenam e = "c:\whatever.jp g"
x.newwidth = 40 'you can specify either or both of newwidth and newheight
x.saveas("c:\fi lename.jpg", System.Drawing. Imaging.ImageFo rmat.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 myImageCodecInf o As ImageCodecInfo, myEncoder As Encoder,
myEncoderParame ters As EncoderParamete rs
Dim myEncoderParame ter As EncoderParamete r
If NewWidth = 0 And NewHeight = 0 Then Return False
If m_FileName = vbNullString Then Return False
myImageCodecInf o = GetEncoderInfo( "image/jpeg")
myEncoder = Encoder.Quality
myEncoderParame ters = New EncoderParamete rs(1)
myEncoderParame ter = New EncoderParamete r(myEncoder, 65)
myEncoderParame ters.Param(0) = myEncoderParame ter
img = New Bitmap(m_FileNa me)
'don't ask me why, but rotating the image 360 degrees improves the quality
no end!
img.RotateFlip( RotateFlipType. Rotate180FlipNo ne)
img.RotateFlip( RotateFlipType. Rotate180FlipNo ne)
If NewWidth <> 0 And NewHeight <> 0 Then
ThumbNail = img.GetThumbnai lImage(NewWidth , NewHeight, AddressOf
AbortHandler, callbackdata)
ElseIf NewWidth <> 0 And NewHeight = 0 Then
ratio = NewWidth / img.Width
ThumbNail = img.GetThumbnai lImage(NewWidth , img.Height * ratio, AddressOf
AbortHandler, callbackdata)
ElseIf NewWidth = 0 And NewHeight <> 0 Then
ratio = NewHeight / img.Height
ThumbNail = img.GetThumbnai lImage(img.Widt h * ratio, NewHeight, AddressOf
AbortHandler, callbackdata)
End If
ThumbNail.Save( ThumbFileName, myImageCodecInf o, myEncoderParame ters)
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. GetImageEncoder s
For j = 0 To encoders.Length
If encoders(j).Mim eType = mimeType Then
Return encoders(j)
End If
Next
Return Nothing
End Function
Private Function AbortHandler() As Boolean
'whatever.
End Function
End Class