Thank you in advance
UserControl code
Public Class Thumbnail
Private strFileName As String = ""
Private Sub DrawThumbNail()
Dim dAspectRatio As Double
Dim bm As Bitmap
Dim iTNHeight As Integer, iTNWidth As Integer
Try
' Display the filename on the label
lblFileName.Text = IO.Path.GetFileName(strFileName)
' Check that the variable strFileName contains something
If strFileName <> "" Then
' Load the photo into a bitmap
bm = New Bitmap(strFileName)
' Calculate the aspect ration of the image
dAspectRatio = bm.Width / bm.Height
' Calculate the width and height of the thumbnail to be created
If dAspectRatio > 1 Then
' If the image is wider than it is tall, set it's width to be the width of the
' picturebox and calculate it's height using the aspect ratio
iTNWidth = picThumbnail.Width
iTNHeight = CInt(iTNWidth / dAspectRatio)
Else
' If the image is taller than it is wide, set it's height to be the height of the
' picturebox and calculate it's width using the aspect ratio
iTNHeight = picThumbnail.Height
iTNWidth = CInt(iTNHeight * dAspectRatio)
End If
' Check we have valid heights and widths
If iTNHeight > 0 And iTNWidth > 0 Then
' Generate the thumbnail
picThumbnail.Image = bm.GetThumbnailImage(iTNWidth, iTNHeight, Nothing, IntPtr.Zero)
End If
' Dispose of the bitmap
bm.Dispose()
Else
' Tf strFileName is empty, clear the picturebox
picThumbnail.Image = Nothing
End If
Catch ex As Exception
MsgBox("An exception occured when trying to render the thumbnail for the image '" & _
strFileName & "' with the message:" & vbCrLf & vbCrLf & ex.ToString, _
MsgBoxStyle.Exclamation)
End Try
End Sub
Public Property FileName() As String
Get
Return strFileName
End Get
Set(ByVal Value As String)
strFileName = Value
DrawThumbnail()
End Set
End Property
Private Sub Thumbnail_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
DrawThumbnail()
End Sub
Public Event ThumbnailMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Public Event ThumbnailDoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Public Event ThumbnailClick(ByVal sender As Object, ByVal e As System.EventArgs)
Private Sub Control_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblFilename.Click, picThumbnail.Click
RaiseEvent ThumbnailClick(Me, e)
End Sub
Private Sub Control_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblFilename.DoubleClick, picThumbnail.DoubleClick
RaiseEvent ThumbnailDoubleClick(Me, e)
End Sub
Private Sub picThumbnail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picThumbnail.Click
End Sub
Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblFilename.MouseDown, Panel1.MouseDown
Dim eNew As System.Windows.Forms.MouseEventArgs
' Convert the sender to a Control so we can access its position
Dim c As Control = DirectCast(sender, Control)
'Create the new MouseEventArgs
eNew = New System.Windows.Forms.MouseEventArgs(e.Button, e.Clicks, e.X + c.Top, e.Y + c.Left, e.Delta)
RaiseEvent ThumbnailMouseDown(Me, eNew)
End Sub
Public Sub New(ByVal strFN As String)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Initialize our thumbnail control
strFileName = strFN ' assign the filename for the thumbnail
DrawThumbnail() ' draw it
End Sub
Private Sub picCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picCheck.CheckedChanged
If picCheck.CheckState = CheckState.Checked Then
Me.Select
End If
End Sub
End Class
Form Code
Imports System.IO
Imports System.Drawing
Public Class Form2
' Constants to define the size and spacing of our thumbnails
Const iTNWidth As Integer = 120
Const iTNHeight As Integer = 120
Const iHPadding As Integer = 5
Const iVPadding As Integer = 5
' Sets the initial zoom factor to 100%
Dim dZoomFactor As Double = 1
' Specifies that the zoom factor will be changed in increments of 15%
Const dZoomIncrements As Double = 0.15
Private Sub SetThumbnailPosition(ByRef tnThumbnail As Thumbnail, ByRef cntlThumbnailContainer As ScrollableControl, ByVal iThumbNailNo As Integer)
Dim iColumns As Integer, iRow As Integer, iCol As Integer
Dim iLeft As Integer, iTop As Integer
' Calculate the number of thumbnails we can fit across the control
iColumns = (cntlThumbnailContainer.Width - 30) \ (iTNWidth + iHPadding)
' If the number if less then 1, set it to 1 so the thumbnails will always be displayed
If iColumns < 1 Then iColumns = 1
' Calculate the position of our thumbnail in our control in terms of the
' row and column of grid of displayed thumbnails
iRow = (iThumbNailNo \ iColumns) + 1
iCol = (iThumbNailNo Mod iColumns) + 1
' Now calculate the position in Pixels. We use the defined constants to calculate the
' position of the thumbnail and add the AutoScrollPosition of the control we are
' going to be arranging it on - this is necessary to ensure that the thumbnails are
' displayed in the correct location if the control is not currently scrolled to the top
' of its available area.
iLeft = (iCol * iHPadding) + ((iCol - 1) * iTNWidth)
iLeft += cntlThumbnailContainer.AutoScrollPosition.X()
iTop = (iRow * iVPadding) + ((iRow - 1) * iTNWidth)
iTop += cntlThumbnailContainer.AutoScrollPosition.Y()
' Set the thumbnails position
tnThumbnail.Left = iLeft
tnThumbnail.Top = iTop
End Sub
Private Sub OnThumbnailDoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim tnThumbnail As Thumbnail
If sender.GetType Is GetType(Thumbnail) Then
tnThumbnail = DirectCast(sender, Thumbnail)
picPhoto.Image = Bitmap.FromFile(tnThumbnail.FileName)
FitPhotoToPanel()
ReDrawPhoto(0, 0)
End If
End Sub
Public Function AddThumbnail(ByRef cntlThumbnailContainer As ScrollableControl, ByVal strFileName As String) As Thumbnail
Dim tnNewThumbnail As Thumbnail
' Create a new thumbnail
tnNewThumbnail = New Thumbnail(strFileName)
' Assign its height, width and make sure it is visible
With tnNewThumbnail
If .Width <> iTNWidth Then .Width = iTNWidth
If .Height <> iTNHeight Then .Height = iTNHeight
.Visible = True
End With
' Add the thumbnail to the specified control
cntlThumbnailContainer.Controls.Add(tnNewThumbnail)
' Set up the event handler
AddHandler tnNewThumbnail.ThumbnailDoubleClick, AddressOf OnThumbnailDoubleClick
' Return the thumbnail we just created
Return tnNewThumbnail
End Function
Public Sub ClearThumbnails(ByRef cntlThumbnailContainer As ScrollableControl)
Dim i As Integer = 0
Dim c As Control
While i < cntlThumbnailContainer.Controls.Count
c = cntlThumbnailContainer.Controls(i)
If c.GetType Is GetType(Thumbnail) Then
cntlThumbnailContainer.Controls.Remove(c) ' Remove the current thumbnail
c.Dispose() ' Dispose of it
Else
i += 1
End If
End While
End Sub
Private Sub GenerateThumbnailsForFolder(ByRef cntlThumbnailContainer As ScrollableControl)
Dim dlg As FolderBrowserDialog
Dim iThumbnailCount As Integer = 0
Dim tnNewThumbnail As Thumbnail
' Create a new instance of the folder browser dialog
dlg = New FolderBrowserDialog
' Show the dialog and check that the user pressed OK to close it - not cancel
If dlg.ShowDialog = DialogResult.OK Then
Me.Cursor = Cursors.WaitCursor ' Set the Forms cursor to a hour glass
' Clear all thumbnails currently on the control
ClearThumbnails(cntlThumbnailContainer)
' Get all the JPEG files in the specified folder and loop through them
For Each objFile As FileInfo In New DirectoryInfo(dlg.SelectedPath).GetFiles("*.jpg")
' Create a new thumbnail to display the current file
tnNewThumbnail = AddThumbnail(cntlThumbnailContainer, objFile.FullName)
' Arrange the thumbnail
SetThumbnailPosition(tnNewThumbnail, cntlThumbnailContainer, iThumbnailCount)
iThumbnailCount += 1 ' Increment the count of the thumbnails on the control
Next
' Set the title of the application to reflect the folder name being browsed
Me.Text = Application.ProductName & " - " & dlg.SelectedPath
Me.Cursor = Cursors.Arrow ' Return the cursor to an arrow
End If
' Dispose of the dialog
dlg.Dispose()
End Sub
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileBrowse.Click
GenerateThumbnailsForFolder(DirectCast(pnlThumbnails, ScrollableControl))
End Sub
Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
Me.Close()
End Sub
Private Sub ReDrawPhoto(ByVal xPos As Integer, ByVal yPos As Integer)
Dim dPicStaticXPos, dPicStaticYPos As Double
Dim dPanelStaticXPos, dPanelStaticYPos As Double
Dim iNewXPos, iNewYPos As Integer
If Not picPhoto.Image Is Nothing Then
' Get the position on the picture in pixels of
' the cordinates specified in the function, we will use this to
' ensure these cordinates stay in the same position each time we redraw the photo
dPicStaticXPos = xPos / (picPhoto.Width / picPhoto.Image.Width)
dPicStaticYPos = yPos / (picPhoto.Height / picPhoto.Image.Height)
dPanelStaticXPos = xPos + pnlPhoto.AutoScrollPosition.X
dPanelStaticYPos = yPos + pnlPhoto.AutoScrollPosition.Y
' Set the new image height and width using the zoom factor
picPhoto.Height = CInt(picPhoto.Image.Height * dZoomFactor)
picPhoto.Width = CInt(picPhoto.Image.Width * dZoomFactor)
picPhoto.Refresh()
' Set the AutoScrollPosition so that the point clicked on is in the same position on the screen after zooming
iNewXPos = CInt((dPicStaticXPos * dZoomFactor) - dPanelStaticXPos)
iNewYPos = CInt((dPicStaticYPos * dZoomFactor) - dPanelStaticYPos)
pnlPhoto.AutoScrollPosition = New Drawing.Point(iNewXPos, iNewYPos)
End If
End Sub
Private Sub FitPhotoToPanel()
' Assumes that the Panel is approximately square
' Calculates the maximum zoom factor that can be used and still
' fit the image on the panel used to contain it
If picPhoto.Image.Height > picPhoto.Image.Width Then
dZoomFactor = (pnlPhoto.Height - 5) / picPhoto.Image.Height
Else
dZoomFactor = (pnlPhoto.Width - 5) / picPhoto.Image.Width
End If
End Sub
Private Sub picPhoto_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles picPhoto.MouseWheel
If e.Delta > 0 Then
dZoomFactor *= (e.Delta / 120) * (1 + dZoomIncrements)
ElseIf e.Delta < 0 Then
dZoomFactor *= (e.Delta / -120) * (1 - dZoomIncrements)
End If
ReDrawPhoto(e.X, e.Y)
End Sub
Private Sub picPhoto_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles picPhoto.Click
picPhoto.Focus()
End Sub
Private Sub ImageZoomIn(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuImageZoomIn.Click
' Increases the zoom factor and redraws the image.
' Passes the cordinates of the middle of the screen into the ReDrawPhoto function so
' That the center of the image at present remains in the center of the screen.
dZoomFactor *= 1 + dZoomIncrements
ReDrawPhoto(CInt(pnlPhoto.Width / 2) - pnlPhoto.AutoScrollPosition.X, _
CInt(pnlPhoto.Height / 2) - pnlPhoto.AutoScrollPosition.Y)
End Sub
Private Sub ImageZoomOut(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuImageZoomOut.Click
dZoomFactor *= 1 - dZoomIncrements
ReDrawPhoto(CInt(pnlPhoto.Width / 2) - pnlPhoto.AutoScrollPosition.X, _
CInt(pnlPhoto.Height / 2) - pnlPhoto.AutoScrollPosition.Y)
End Sub
Private Sub ImageFitToScreen(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuImageFitToScreen.Click
FitPhotoToPanel()
ReDrawPhoto(0, 0)
End Sub
Private Sub ImageFlipVeritcal(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuImageFlipVertical.Click
If Not picPhoto.Image Is Nothing Then
picPhoto.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
End If
End Sub
Private Sub ImageFlipHorizontal(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuImageFlipHorizontal.Click
If Not picPhoto.Image Is Nothing Then
picPhoto.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
End If
End Sub
Private Sub ImageRotateLeft(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuRotateLeft.Click
If Not picPhoto.Image Is Nothing Then
picPhoto.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
End If
End Sub
Private Sub ImageRotateRight(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuRotateRight.Click
If Not picPhoto.Image Is Nothing Then
picPhoto.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
End If
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub ΑντιγραφήToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ΑντιγραφήToolStripMenuItem.Click
End Sub
End Class

New Topic/Question
Reply



MultiQuote






|