Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class ThumbnailGenerate
- Dim CtrlWidth As Integer
- Dim CtrlHeight As Integer
- Dim PicWidth As Integer
- Dim PicHeight As Integer
- Dim XLocation As Integer
- Dim YLocation As Integer
- Dim XcLocation As Integer
- Dim YcLocation As Integer
- Private Sub ThumbnailGenerate_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
- CtrlHeight = Me.Height
- CtrlWidth = Me.Width
- End Sub
- Private _Directory_Path As String
- Public Property Directorypath() As String
- Get
- Return _Directory_Path
- End Get
- Set(ByVal value As String)
- _Directory_Path = value
- XLocation = 25
- YLocation = 25
- XcLocation = -111
- YcLocation = 25
- PicWidth = 117
- PicHeight = 109
- CreateGallery()
- End Set
- End Property
- Dim i As Integer = 0
- Public Sub DrawPictureBox(ByVal _filename As String, ByVal _displayname As String)
- Dim Pic1 As New PictureBox
- Dim checkBox = New CheckBox()
- Pic1.Location = New System.Drawing.Point(XLocation, YLocation)
- XLocation = XLocation + PicWidth + 20
- XcLocation = XcLocation + PicWidth + 20
- If XLocation + PicWidth >= CtrlWidth Then
- XLocation = 25
- YLocation = YLocation + PicHeight + 20
- End If
- If XcLocation + PicWidth >= CtrlWidth Then
- XcLocation = 25
- YcLocation = YcLocation + PicHeight + 20
- End If
- Pic1.Name = "PictureBox" & i
- i += 1
- Pic1.Size = New System.Drawing.Size(PicWidth, PicHeight)
- Pic1.TabIndex = 0
- Pic1.TabStop = False
- Pic1.BorderStyle = BorderStyle.Fixed3D
- Dim original As Image = Image.FromFile(_filename)
- Dim resized As Image = ResizeImage(original, New Size(78, 78))
- Dim memStream As IO.MemoryStream = New IO.MemoryStream()
- resized.Save(memStream, Imaging.ImageFormat.Jpeg)
- Me.ToolTip1.SetToolTip(checkBox, _displayname)
- AddHandler checkBox.MouseEnter, AddressOf checkBox_MouseEnter
- AddHandler checkBox.MouseLeave, AddressOf checkBox_MouseLeave
- 'Me.Controls.Add(Pic1)
- Pic1.Image = Image.FromFile(_filename)
- Pic1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
- Me.Controls.Add(checkBox)
- checkBox.Size = New Size(117, 109)
- checkBox.Text = (_displayname)
- checkBox.Image = resized
- checkBox.BackColor = Color.Gray
- checkBox.ForeColor = Color.White
- checkBox.TextAlign = ContentAlignment.BottomCenter
- checkBox.Location = New System.Drawing.Point(XcLocation, YcLocation)
- End Sub
- Private Sub CreateGallery()
- i = 0
- RemoveControls()
- If Directorypath IsNot Nothing Then
- Dim di As New IO.DirectoryInfo(Directorypath)
- Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg").Concat(di.GetFiles("*.bmp")).Concat(di.GetFiles("*.png")).Concat(di.GetFiles("*.gif")).ToArray
- Dim dra As IO.FileInfo
- For Each dra In diar1
- DrawPictureBox(dra.FullName, dra.Name)
- Next
- End If
- End Sub
- Private Sub checkBox_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
- 'Dim Pic As PictureBox
- 'Pic = sender
- 'Pic.BorderStyle = BorderStyle.FixedSingle
- End Sub
- Private Sub checkBox_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs)
- 'Dim Pic As PictureBox
- 'Pic = sender
- 'Pic.BorderStyle = BorderStyle.Fixed3D
- End Sub
- Public Shared Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
- Dim newWidth As Integer
- Dim newHeight As Integer
- If preserveAspectRatio Then
- Dim originalWidth As Integer = image.Width
- Dim originalHeight As Integer = image.Height
- Dim percentWidth As Single = CSng(Size.Width) / CSng(originalWidth)
- Dim percentHeight As Single = CSng(Size.Height) / CSng(originalHeight)
- Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
- newWidth = CInt(originalWidth * percent)
- newHeight = CInt(originalHeight * percent)
- Else
- newWidth = Size.Width
- newHeight = Size.Height
- End If
- Dim newImage As Image = New Bitmap(newWidth, newHeight)
- Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
- 'graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
- graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
- End Using
- Return newImage
- End Function
- Private Sub RemoveControls()
- Again: For Each ctrl As Control In Me.Controls
- If TypeOf (ctrl) Is PictureBox Then
- Me.Controls.Remove(ctrl)
- End If
- Next
- If Me.Controls.Count > 0 Then
- GoTo Again
- End If
- End Sub
- End Class
Add Comment
Please, Sign In to add comment