Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports Telerik.FileManager
Partial Class MyControls_ImageGallery
Inherits System.Web.UI.UserControl
'The title of our gallery
Dim _galleryTitle As String
Public Property GalleryTitle() As String
Get
Return _galleryTitle
End Get
Set(ByVal value As String)
_galleryTitle = value
End Set
End Property
'How many columns do we want the gallery to have
Dim _repeatColumns As Integer
Public Property RepeatColumns() As Integer
Get
Return _repeatColumns
End Get
Set(ByVal value As Integer)
_repeatColumns = value
End Set
End Property
'What is the maximum width of the thumbnail we are going to allow
Dim _maxThumbWidth As Integer
Public Property MaxThumbWidth() As Integer
Get
Return _maxThumbWidth
End Get
Set(ByVal value As Integer)
_maxThumbWidth = value
End Set
End Property
'The folder which contains image files for this gallery
Dim _galleryFolder As String
Public Property GalleryFolder() As String
Get
Return _galleryFolder
End Get
Set(ByVal value As String)
_galleryFolder = value
End Set
End Property
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'make sure path end with a slash
If (_galleryFolder.IndexOf("/") <> (_galleryFolder.Length - 1)) Then
_galleryFolder += "/"
End If
'set gallery title
lblGalleryTitle.Text = GalleryTitle
'Make sure folder exists
If (Directory.Exists(Server.MapPath(GalleryFolder))) Then
Dim fInfo As FolderInformation = New FolderInformation(Server.MapPath(GalleryFolder))
Dim arlImages As ArrayList = New ArrayList()
For Each imgFile As FileInformation In fInfo.GetFiles()
If (File.Exists(imgFile.FullName)) Then
'get the image in order to determine actual width and height
Dim currentImage As System.Drawing.Image = System.Drawing.Image.FromFile(imgFile.FullName)
Dim img As MyImage = New MyImage()
img.ImageUrl = GalleryFolder & imgFile.Name
img.ActualWidth = currentImage.Width.ToString()
img.ActualHeight = currentImage.Height.ToString()
'get scale factor to keep the dimension ratio
Dim scaleFactor As Double = GetScaleFactor(currentImage.Width)
img.ThumbWidth = currentImage.Width * scaleFactor
img.ThumbHeight = currentImage.Height * scaleFactor
arlImages.Add(img)
End If
Next
'apply repeate columns property and bind data list
dlstImages.RepeatColumns = RepeatColumns
dlstImages.DataSource = arlImages
dlstImages.DataBind()
Else
'tell the user that folder does not exist
lblGalleryTitle.Text = GalleryTitle & " (GalleryFolder does not exist!)"
End If
End Sub
Private Function GetScaleFactor(ByVal actualWidth As Integer) As Double
'if MaxThumbWidth is not set or 0, make it 100 by default
If (MaxThumbWidth = 0) Then
MaxThumbWidth = 100
End If
If actualWidth > MaxThumbWidth Then
Return (MaxThumbWidth / actualWidth)
Else
Return 1
End If
End Function
End Class
Public Class MyImage
Dim _imageUrl As String
Public Property ImageUrl() As String
Get
Return _imageUrl
End Get
Set(ByVal value As String)
_imageUrl = value
End Set
End Property
Dim _actualWidth As String
Public Property ActualWidth() As String
Get
Return _actualWidth
End Get
Set(ByVal value As String)
_actualWidth = value
End Set
End Property
Dim _actualHeight As String
Public Property ActualHeight() As String
Get
Return _actualHeight
End Get
Set(ByVal value As String)
_actualHeight = value
End Set
End Property
Dim _thumbWidth As Integer
Public Property ThumbWidth() As Integer
Get
Return _thumbWidth
End Get
Set(ByVal value As Integer)
_thumbWidth = value
End Set
End Property
Dim _thumbHeight As Integer
Public Property ThumbHeight() As Integer
Get
Return _thumbHeight
End Get
Set(ByVal value As Integer)
_thumbHeight = value
End Set
End Property
End Class
Colorized by: CarlosAg.CodeColorizer