Advertisement
dassoubarna

Thumbnail Generator

Apr 3rd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.13 KB | None | 0 0
  1. Public Class ThumbnailGenerate
  2.     Dim CtrlWidth As Integer
  3.     Dim CtrlHeight As Integer
  4.     Dim PicWidth As Integer
  5.     Dim PicHeight As Integer
  6.  
  7.     Dim XcLocation As Integer
  8.     Dim YcLocation As Integer
  9.     Private Sub ThumbnailGenerate_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
  10.         CtrlHeight = Me.Height
  11.         CtrlWidth = Me.Width
  12.     End Sub
  13.     Private _Directory_Path As String
  14.     Public Property Directorypath() As String
  15.         Get
  16.             Return _Directory_Path
  17.         End Get
  18.         Set(ByVal value As String)
  19.  
  20.             _Directory_Path = value
  21.            
  22.             XcLocation = -111
  23.             YcLocation = 25
  24.             PicWidth = 117
  25.             PicHeight = 109
  26.             CreateGallery()
  27.         End Set
  28.     End Property
  29.     Dim i As Integer = 0
  30.     Public Sub DrawPictureBox(ByVal _filename As String, ByVal _displayname As String)
  31.  
  32.         Dim checkBox = New CheckBox()
  33.        
  34.         XcLocation = XcLocation + PicWidth + 20
  35.  
  36.        
  37.         If XcLocation + PicWidth >= CtrlWidth Then
  38.             XcLocation = 25
  39.             YcLocation = YcLocation + PicHeight + 20
  40.         End If
  41.        
  42.  
  43.         Dim original As Image = Image.FromFile(_filename)
  44.         Dim resized As Image = ResizeImage(original, New Size(78, 78))
  45.         Dim memStream As IO.MemoryStream = New IO.MemoryStream()
  46.         resized.Save(memStream, Imaging.ImageFormat.Jpeg)
  47.  
  48.         Me.ToolTip1.SetToolTip(checkBox, _displayname)
  49.         AddHandler checkBox.MouseEnter, AddressOf checkBox_MouseEnter
  50.         AddHandler checkBox.MouseLeave, AddressOf checkBox_MouseLeave
  51.  
  52.         Me.Controls.Add(checkBox)
  53.         checkBox.Size = New Size(117, 109)
  54.         checkBox.Text = (_displayname)
  55.         checkBox.Image = resized
  56.         checkBox.BackColor = Color.Gray
  57.         checkBox.ForeColor = Color.White
  58.         checkBox.TextAlign = ContentAlignment.BottomCenter
  59.         checkBox.Location = New System.Drawing.Point(XcLocation, YcLocation)
  60.     End Sub
  61.  
  62.     Private Sub CreateGallery()
  63.         i = 0
  64.         RemoveControls()
  65.         If Directorypath IsNot Nothing Then
  66.             Dim di As New IO.DirectoryInfo(Directorypath)
  67.             Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg").Concat(di.GetFiles("*.bmp")).Concat(di.GetFiles("*.png")).Concat(di.GetFiles("*.gif")).ToArray
  68.             Dim dra As IO.FileInfo
  69.             For Each dra In diar1
  70.                 DrawPictureBox(dra.FullName, dra.Name)
  71.             Next
  72.         End If
  73.     End Sub
  74.  
  75.     Private Sub checkBox_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
  76.  
  77.     End Sub
  78.     Private Sub checkBox_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs)
  79.  
  80.     End Sub
  81.     Public Shared Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
  82.         Dim newWidth As Integer
  83.         Dim newHeight As Integer
  84.         If preserveAspectRatio Then
  85.             Dim originalWidth As Integer = image.Width
  86.             Dim originalHeight As Integer = image.Height
  87.             Dim percentWidth As Single = CSng(Size.Width) / CSng(originalWidth)
  88.             Dim percentHeight As Single = CSng(Size.Height) / CSng(originalHeight)
  89.             Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
  90.             newWidth = CInt(originalWidth * percent)
  91.             newHeight = CInt(originalHeight * percent)
  92.         Else
  93.             newWidth = Size.Width
  94.             newHeight = Size.Height
  95.         End If
  96.         Dim newImage As Image = New Bitmap(newWidth, newHeight)
  97.         Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
  98.  
  99.             graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
  100.         End Using
  101.         Return newImage
  102.     End Function
  103.     Private Sub RemoveControls()
  104. Again:  For Each ctrl As Control In Me.Controls
  105.             If TypeOf (ctrl) Is PictureBox Then
  106.                 Me.Controls.Remove(ctrl)
  107.             End If
  108.         Next
  109.         If Me.Controls.Count > 0 Then
  110.             GoTo Again
  111.         End If
  112.     End Sub
  113. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement