星期日, 8月 16, 2009

.Net Draw thumbnail using VB.Net

' called when the thumbnail needs to be drawn
Private Sub DrawItem(ByVal g As Graphics, ByVal index As Integer)
' get the item that needs to be drawn
Dim item As ListViewItem = Me.Items(index)

' init values used for drawing
Const size As Integer = ThumbnailSize.Large
Dim textHeight As Integer = Me.Font.Height

' calculate area to draw thumbnail, usually would center the vertical position
' but this moves the thumbnail down when the title contains a very long
' string, instead, center the horizontal position, but always draw
' the vertical position from a set amount from the top
Dim bounds As New Rectangle( _
item.Bounds.Left + (item.Bounds.Width - size) \ 2, _
item.Bounds.Top + 2, _
size, size)

Dim image As Bitmap
Try
' draw the thumbnail image
' could cache the thumbnail to be more efficient
Dim photo As Photo = DirectCast(item.Tag, Photo)
image = New Bitmap(photo.ThumbnailPath)
g.DrawImage(image, _
bounds.Left + (size - image.Width) \ 2, _
bounds.Top + (size - image.Height) \ 2, _
image.Width, image.Height)
Catch ex As Exception
' could not draw the thumbnail
Finally
If Not (image Is Nothing) Then image.Dispose()
End Try

' erase the thicker selected border
If Not item.Selected Then
g.DrawRectangle(_penBack, bounds)
End If

' border
g.DrawRectangle(DirectCast(IIf( _
item.Selected, _penSelected, _penFrame), Pen), bounds)

' title, calculate the area to draw the title
Dim rc As New RectangleF(bounds.Left, _
bounds.Bottom + 4, _
bounds.Width, textHeight + 1)

' draw the title, different background if selected or not
g.FillRectangle(DirectCast(IIf(item.Selected, _
_brushSelected, _brushBack), SolidBrush), rc)
g.DrawString(item.Text, Me.Font, Brushes.Black, rc, _format)
End Sub

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails