Solved

Creating thumbnails from the selection of images in a folder

Posted on 2004-10-19
4
1,002 Views
Last Modified: 2007-12-19
Hello,
  I am writing a small program for displaying the images as thumbnails. I need to display all the images as thumbnails which i have selected from the File dialog box. I have set the property of the filechooser as multiselect and i am trying to display all the images i have selected as Thumbnails.
What my outcome is i am getting only one image being displayed as thumbnail even if i select more than one. Do i need to randomly generate the picture box for displaying more than one image.
Can anybody help me solve the above problem.
I am pastiny my code here below
Try

            Dim currentDirectory As String = Directory.GetCurrentDirectory
            Dim fileChooser As OpenFileDialog = New OpenFileDialog
            fileChooser.Multiselect = True
            fileChooser.Filter = ("Supported Files|*.gif;*.jpg;*.bmp")
            Dim result As DialogResult = fileChooser.ShowDialog()
            Dim objThumbNail As Image
            Dim callBack As Image.GetThumbnailImageAbort
            Dim fileName() As String
            Dim filName As String
            Dim myFile As String
            Dim myfils As Integer
            Dim myDirectory As String
            Dim iHeight As Integer
            Dim iWidth As Integer
            Dim inp As IntPtr
            Dim i As Integer


            If result = DialogResult.Cancel Then
                Return
            End If

            fileName = fileChooser.FileNames

            If (fileName.Length = 0) Then
                MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return
                'Else
                'input = New FileStream(fileName, FileMode.Open, FileAccess.Read)
            End If

            imageNumber = (imageNumber + 1) Mod 3
            iWidth = 100
            iHeight = 100
            For i = 0 To fileName.Length - 1
                objThumbNail = Image.FromFile(fileName(i))
                PictureBox1.Image = Image.FromFile(fileName(i)).GetThumbnailImage(iWidth, iHeight, Nothing, inp)
            Next
             
            Button4.Enabled = True
                    Catch ex As Exception
            Console.OpenStandardOutput()
            Console.WriteLine("StackTrace" & vbCrLf & ex.StackTrace)
            Debug.Write("Stacktrace" & vbCrLf & ex.StackTrace)

        End Try
    End Sub
0
Comment
Question by:saidreddy
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 12348512
Why not add your thumbnails to a ListView so you can browse them like this:

Imports System
Imports System.IO
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Select Images"
        '
        'ListView1
        '
        Me.ListView1.AllowDrop = True
        Me.ListView1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ListView1.Location = New System.Drawing.Point(8, 40)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(456, 304)
        Me.ListView1.TabIndex = 3
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ProgressBar1.Location = New System.Drawing.Point(8, 40)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(456, 8)
        Me.ProgressBar1.TabIndex = 4
        Me.ProgressBar1.Visible = False
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.Multiselect = True
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(472, 350)
        Me.Controls.Add(Me.ProgressBar1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ListView1)
        Me.Name = "Form1"
        Me.Text = "Browse Images via Thumbnails in a ListView"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Const thumbWidth As Byte = 100
    Private Const thumbHeight As Byte = 100

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Multiselect = True
        OpenFileDialog1.Filter = ("Supported Files|*.gif;*.jpg;*.bmp")
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            If OpenFileDialog1.FileNames.Length > 0 Then
                updateImages(OpenFileDialog1.FileNames)
            End If
        End If
    End Sub

    Private Sub updateImages(ByVal fileNames() As String)
        Dim fi As FileInfo
        Dim fileName As String
        Dim thumbEntry As ListViewItem
        Dim imageList1 As ImageList = New ImageList
        Dim p As Integer, i As Integer, numFiles As Integer = fileNames.Length
        Dim myCallback As Image.GetThumbnailImageAbort = _
                New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

        ListView1.Items.Clear()
        imageList1.ImageSize = New Size(thumbWidth, thumbHeight)
        ListView1.LargeImageList = imageList1
        ListView1.SmallImageList = imageList1

        ListView1.BeginUpdate()
        ProgressBar1.Value = 0
        ProgressBar1.Visible = True
        i = 0
        For Each fileName In fileNames
            fi = New FileInfo(fileName)
            If fi.Extension.ToUpper.Equals(".BMP") Or _
                    fi.Extension.ToUpper.Equals(".JPG") Or _
                    fi.Extension.ToUpper.Equals(".GIF") Then
                Try
                    imageList1.Images.Add(Image.FromFile(fi.FullName).GetThumbnailImage(thumbWidth, thumbHeight, myCallback, IntPtr.Zero))
                    thumbEntry = New ListViewItem
                    thumbEntry.Text = fi.Name
                    thumbEntry.ImageIndex = imageList1.Images.Count - 1
                    ListView1.Items.Add(thumbEntry)
                Catch ex As Exception
                    MsgBox(fi.FullName & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Information, "Error Loading Image")
                End Try
            End If
            i = i + 1
            p = CInt(i / numFiles * 100)
            ProgressBar1.Value = p
            Application.DoEvents()
        Next fileName
        ProgressBar1.Visible = False
        ListView1.EndUpdate()
    End Sub

    Private Function ThumbnailCallback() As Boolean
        Return False
    End Function

End Class
0
 

Author Comment

by:saidreddy
ID: 12356146
Hi,
  Thanks for your reply. I have changed the code in the followoing way and the problem is only the last thumbnail is being shown and the others are shown only for a second and then they are not shown, I mean if i select three images it is showing the first image and then it disappears leaving a blank space and so on and the last one is displayed after three blank spaces
Can you tell me where the problem is

The code#
Try
            Dim currentDirectory As String = Directory.GetCurrentDirectory
            Dim objThumbNail As Image
            Dim callBack As Image.GetThumbnailImageAbort
            Dim fileName() As String
            Dim filName As String
            Dim myFile As String
            Dim myfils As Integer
            Dim myDirectory As String
            Dim iHeight As Integer
            Dim iWidth As Integer
            Dim inp As IntPtr
            Dim i As Integer
            Dim i_arr As Integer
            Dim fi As FileInfo
            Dim img As Image
            Dim Left As Integer = ThumbnailPadding
            Dim Top As Integer = ThumbnailPadding
            Dim ThispBox As Control
            Dim ThumbWidth As Integer = 90
            Dim ThumbHeight As Integer = 120
            Dim pBox As PictureBox
            Dim pbBoox() As Object
            Dim fileChooser As OpenFileDialog = New OpenFileDialog
            fileChooser.Multiselect = True
            fileChooser.Filter = ("Supported Files|*.gif;*.jpg;*.bmp;*.ico;*.cur;*.wfm;*.emf")
            Dim result As DialogResult = fileChooser.ShowDialog()

            pBox = New PictureBox
             
            If result = DialogResult.Cancel Then
                Return
            End If

            fileName = fileChooser.FileNames
            For i = 0 To fileName.Length - 1
                fi = New FileInfo(fileName(i))
                If fi.Extension = ".gif" Or fi.Extension = ".jpg" Or fi.Extension = ".jpeg" Or fi.Extension = ".bmp" Or fi.Extension = ".GIF" Or fi.Extension = ".JPG" Or fi.Extension = ".JPEG" Or fi.Extension = ".GIF" Then
                    'pbBox = New PictureBox
                    pBox.BorderStyle = BorderStyle.FixedSingle
                    pBox.SizeMode = PictureBoxSizeMode.CenterImage
                    pBox.Refresh()
                    img = Image.FromFile(fi.FullName)
                End If
                If Left > Panel1.Width - Panel1.Left + ThumbnailPadding + ThumbWidth Then
                    Left = ThumbnailPadding
                    Top = Top + 80

                End If


                'objThumbNail = Image.FromFile(fileName(i))
                '                pbBox.Image = Image.FromFile(fi.FullName).GetThumbnailImage(iWidth, iHeight, Nothing, inp)
                '               pbBox.Refresh()
                '
                '               Panel1.Controls.Add(pbBox)
                '              Panel1.Refresh()
                                 With pBox
                    .Left = Left
                    .Top = Top
                    .Width = 64
                    .Height = 64
                    .Image = CalculateThumbnailSize(img, pBox.Width, pBox.Height)
                    .Visible = True
                    .Tag = fi.FullName
                   
                    Panel1.Controls.Add(pBox)
                    Left = Left + 74
                End With
               
            Next
           
           
        Catch ex As Exception
            Debug.Write(ex.StackTrace)
            MsgBox(Err.Description)
        End Try
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The FUNCTION Code

Private Function CalculateThumbnailSize(ByRef Img As Image, ByVal MaxWidth As Integer, ByVal MaxHeight As Integer) As Image
        Dim tWidth As Integer, tHeight As Integer
        Dim Ratio As Single
        Dim I As Single

        If Img Is Nothing Then Exit Function

        If Img.Width / MaxWidth > Img.Height / MaxHeight Then
            Ratio = Img.Width / Img.Height
            tWidth = Img.Width / (Img.Width / MaxWidth)
            tHeight = tWidth / Ratio

        Else
            Ratio = Img.Height / Img.Width
            tHeight = Img.Height / (Img.Height / MaxHeight)
            tWidth = tHeight / Ratio

        End If

        Return Img.GetThumbnailImage(tWidth, tHeight, Nothing, Nothing)

    End Function
0
 

Author Comment

by:saidreddy
ID: 12356942
HI,
  I have solved the above problem. Everything works fine.
I have a question how can i assign the points to Idle_Mind ?

Thanks.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12362156
You can click on the "Accept" button that appears to the right of the solution that you want to accepts as an answer:
http://www.experts-exchange.com/help.jsp#hs5

~IM
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now