dassoubarna

Thumbnail Generator

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