Results 1 to 18 of 18

Thread: image gallery

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    image gallery

    Hey guys is there a way to make a gallery that loads a directory automaticly?like no browse.

    lets say opens folder/images1

    and list all images(pictures not names)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: image gallery

    The code to load the images would be same whether there's browsing or not. The browsing part would just be displaying an FolderBrowserDialog to allow the user to select the path. Once you've got the path, wherever it came from, loading the images is exactly the same: you call Directory.GetFiles to get a list of all the image files in a folder, then you call Image.FromFile on each one to create an Image object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    ok so just a stupid question that is killing me..

    lets say this is my page

    Code:
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim fInfo As FileInfo()
            Dim x As Integer = 0
            Dim dirInfo As DirectoryInfo = New DirectoryInfo("C:\Users\TECHKER\Pictures\MISC")
            fInfo = dirInfo.GetFiles("*.jpg")
            Dim files As String()
            Dim File As String
    
            files = Directory.GetFiles("C:\Users\TECHKER\Pictures\MISC", "*.jpg")
    
            For Each File In files
                'Use your code that you provided
                'above to read the files' text.
                x += 1
            Next
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
        End Sub
    End Class
    when you have a function like that
    were do you put it?and how do you make it that when you press it shows the content?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: image gallery

    I'm not quite sure what you're asking because you've already placed the code in the Click event handler of Button1 on Form1, so it's already going to be executed when you click that button. The problem is, you aren't creating any Image objects from the files. As I said in my previous post, you need to call Image.FromFile to create an Image object from a file path. You can then do whatever's appropriate with that Image object, e.g. add it to a List(Of Image).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    well thats my problem. i know were to put the code but when you press how to you make it so it can list in listbox1..

    cause now there is now indication

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: image gallery

    List what? No indication of what? You need to be clear because we can't tell what you're thinking. You need to specify EXACTLY what you expect to happen. Are you saying that you want the Images to appear in the ListBox? If so then you'll be disappointed because, without some serious customisation, a ListBox won't display Images. You'd need a ListView or some custom combination of your own involving PictureBoxes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    thats it a list view.so my question is :

    were do you put the code.

    button1
    Code:
    Dim fInfo As FileInfo()
            Dim x As Integer = 0
            Dim dirInfo As DirectoryInfo = New DirectoryInfo("C:\Users\TECHKER\Pictures\MISC")
            fInfo = dirInfo.GetFiles("*.jpg")
            Dim files As String()
            Dim File As String
    
            files = Directory.GetFiles("C:\Users\TECHKER\Pictures\MISC", "*.jpg")
    
            For Each File In files
                'Use your code that you provided
                'above to read the files' text.
                x += 1
            Next
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
        End Sub
    now if i put a list view box on my for how do i link the button to it?how does the button know where to output the information?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: image gallery

    You don't link a ListView to a Button. Think about what you're trying to do. You're trying to load Images into a ListView when a Button is clicked. As such, whatever code you write must go in the Click event handler of the Button. You've already got that, so you're on the right track. Now you've just got to put the CORRECT code there. You already know, from my previous posts, that you need to call Image.FromFile to create an Image object for each file path. Where do you suppose that should go? Read what I posted:
    you need to call Image.FromFile to create an Image object for each file path
    Now read your code:
    For Each File In files
    Next, you know that you need to use a ListView. That means you need to do some reading on ListViews to see how they work and how you can load Images into one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    sorry it's because im very visual!lol need to see it to understand..

    i will study this.thx.

  10. #10
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: image gallery

    only because I'm bored at the moment:

    vb.net Code:
    1. Private IL As New ImageList
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         ListView1.View = View.LargeIcon
    5.         ListView1.LargeImageList = Me.IL
    6.         IL.ImageSize = New Size(128, 80) ' widescreen format 1.6 aspect ratio (adjust to your liking) | max 256x256
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         ListView1.Items.Clear()
    11.         IL.Images.Clear()
    12.         Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
    13.         For Each file As String In My.Computer.FileSystem.GetFiles(path, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*bmp", "*.jpg", "*.png", "*.gif"})
    14.             Dim thumb As Bitmap = GetThumbNail(file)
    15.             If Not IsNothing(thumb) Then
    16.                 IL.Images.Add(thumb)
    17.                 Dim lvi As ListViewItem = ListView1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file), IL.Images.Count - 1)
    18.                 lvi.Tag = file ' store the full path to the image for later use
    19.             End If
    20.         Next
    21.     End Sub
    22.  
    23.     Private Function GetThumbNail(ByVal ImageFileName As String) As Bitmap
    24.         Static PB As New PictureBox
    25.         PB.Size = IL.ImageSize
    26.         PB.SizeMode = PictureBoxSizeMode.Zoom
    27.         Try
    28.             Dim bmp As New Bitmap(IL.ImageSize.Width, IL.ImageSize.Height)
    29.             Using fs As New System.IO.FileStream(ImageFileName, IO.FileMode.Open)
    30.                 PB.Image = Image.FromStream(fs)
    31.                 PB.DrawToBitmap(bmp, PB.ClientRectangle)
    32.             End Using
    33.             Return bmp
    34.         Catch ex As Exception
    35.             MessageBox.Show(ex.Message & vbCrLf & "File: " & ImageFileName, "Error Loading Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
    36.             Return Nothing
    37.         End Try
    38.     End Function
    39.  
    40.     Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    41.         Dim pt As Point = ListView1.PointToClient(Cursor.Position)
    42.         Dim lvi As ListViewItem = ListView1.GetItemAt(pt.X, pt.Y)
    43.         If Not IsNothing(lvi) Then
    44.             Process.Start(lvi.Tag)
    45.         End If
    46.     End Sub


    1. You won't learn a thing by just copy and pasting that code
    2. It is not my code, but it works
    3. Spell check is your friend

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    thx dude!

    so this is the full page

    Code:
    Imports System.IO
    Public Class Form3
    
    
    
        Private IL As New ImageList
    
        Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListView1.View = View.LargeIcon
            ListView1.LargeImageList = Me.IL
            IL.ImageSize = New Size(128, 80) ' widescreen format 1.6 aspect ratio (adjust to your liking) | max 256x256
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListView1.Items.Clear()
            IL.Images.Clear()
            Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
            For Each file As String In My.Computer.FileSystem.GetFiles(path, FileIO.SearchOption.SearchTopLevelOnly, New String() {"*bmp", "*.jpg", "*.png", "*.gif"})
                Dim thumb As Bitmap = GetThumbNail(file)
                If Not IsNothing(thumb) Then
                    IL.Images.Add(thumb)
                    Dim lvi As ListViewItem = ListView1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file), IL.Images.Count - 1)
                    lvi.Tag = file ' store the full path to the image for later use
                End If
            Next
        End Sub
    
        Private Function GetThumbNail(ByVal ImageFileName As String) As Bitmap
            Static PB As New PictureBox
            PB.Size = IL.ImageSize
            PB.SizeMode = PictureBoxSizeMode.Zoom
            Try
                Dim bmp As New Bitmap(IL.ImageSize.Width, IL.ImageSize.Height)
                Using fs As New System.IO.FileStream(ImageFileName, IO.FileMode.Open)
                    PB.Image = Image.FromStream(fs)
                    PB.DrawToBitmap(bmp, PB.ClientRectangle)
                End Using
                Return bmp
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbCrLf & "File: " & ImageFileName, "Error Loading Image", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return Nothing
            End Try
        End Function
    
        Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
            Dim pt As Point = ListView1.PointToClient(Cursor.Position)
            Dim lvi As ListViewItem = ListView1.GetItemAt(pt.X, pt.Y)
            If Not IsNothing(lvi) Then
                Process.Start(lvi.Tag)
            End If
        End Sub
    End Class
    Can i change the directory?
    i just downloaded a book..going to start big time..
    Wrox.Visual.Basic.2008.Programmers.Reference.Feb.2008

    really appreciate the help.
    Last edited by techker; Jan 24th, 2010 at 09:15 PM. Reason: found the error i had

  12. #12
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: image gallery

    sure you can. the line:

    vb Code:
    1. Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

    is where you set the path of the folder that contains your images

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    so i have tried changing the path with

    Code:
    Dim path As String = Environment.GetFolderPath("c:/....") 
    or
    Dim path As String = Environment.GetFolderPath(My.Application.Info.DirectoryPath & "shoulder/dum-01.bmp")
    
    no success error messages..
    how can i set it to my project folders..

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: image gallery

    Have you read the documentation for the Environment.GetFolderPath method? That will tell you how it should be used.

    If you want to get the path of your program folder then you've already got it. That's exactly what My.Application.Info.DirectoryPath is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    i did but i cant seem to pin point it..

    cause My.Application.Info.DirectoryPath gives me an integer error..

    cause the way it is set up that is the only way it will work..cause when i read up directory path i need to change the code..string to integer


    Application data : C:\Documents and Settings\LeeT\Application Data
    Commom Application data : C:\Documents and Settings\All Users\Application Data
    Common Program files : C:\Program Files\Common Files
    Cookies : C:\Documents and Settings\LeeT\Cookies
    Desktop : C:\Documents and Settings\LeeT\Desktop
    Desktop Directory : C:\Documents and Settings\LeeT\Desktop
    Favorites : C:\Documents and Settings\LeeT\Favorites
    History path : C:\Documents and Settings\LeeT\Local Settings\History
    Internet Cache : C:\Documents and Settings\LeeT\Local Settings\Temporary Internet Files
    My Computer :
    My Documents : C:\Documents and Settings\LeeT\My Documents
    My Music : C:\Documents and Settings\LeeT\My Documents\My Music
    My Pictures : C:\Documents and Settings\LeeT\My Documents\My Pictures
    Personal : C:\Documents and Settings\LeeT\My Documents
    Recent : C:\Documents and Settings\LeeT\Recent
    SendTo : C:\Documents and Settings\LeeT\SendTo
    Start Menu : C:\Documents and Settings\LeeT\Start Menu
    System path : C:\WINDOWS\system32
    Templates : C:\Documents and Settings\LeeT\Templates

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    tried this to

    Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\shoulder"

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: image gallery

    You misunderstand me. You do NOT need Environment.GetFolderPath at all. My.Application.Info.DirectoryPath IS the path.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: image gallery

    So tlike this.

    i gues cause i was using 2 path's..

    Dim path As String = My.Application.Info.DirectoryPath & "shoulder/dum-01.bmp"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width