|
-
Jan 24th, 2010, 07:58 PM
#1
Thread Starter
Member
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)
-
Jan 24th, 2010, 08:09 PM
#2
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.
-
Jan 24th, 2010, 08:34 PM
#3
Thread Starter
Member
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?
-
Jan 24th, 2010, 08:37 PM
#4
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).
-
Jan 24th, 2010, 08:39 PM
#5
Thread Starter
Member
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
-
Jan 24th, 2010, 08:51 PM
#6
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.
-
Jan 24th, 2010, 08:54 PM
#7
Thread Starter
Member
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?
-
Jan 24th, 2010, 09:01 PM
#8
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: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.
-
Jan 24th, 2010, 09:04 PM
#9
Thread Starter
Member
Re: image gallery
sorry it's because im very visual!lol need to see it to understand..
i will study this.thx.
-
Jan 24th, 2010, 09:05 PM
#10
Re: image gallery
only because I'm bored at the moment:
vb.net Code:
Private IL As New ImageList
Private Sub Form1_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
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
-
Jan 24th, 2010, 09:12 PM
#11
Thread Starter
Member
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
-
Jan 24th, 2010, 09:21 PM
#12
Re: image gallery
sure you can. the line:
vb Code:
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
is where you set the path of the folder that contains your images
-
Jan 25th, 2010, 06:42 AM
#13
Thread Starter
Member
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..
-
Jan 25th, 2010, 07:07 AM
#14
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.
-
Jan 25th, 2010, 07:11 AM
#15
Thread Starter
Member
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
-
Jan 25th, 2010, 07:20 AM
#16
Thread Starter
Member
Re: image gallery
tried this to
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\shoulder"
-
Jan 25th, 2010, 08:27 AM
#17
Re: image gallery
You misunderstand me. You do NOT need Environment.GetFolderPath at all. My.Application.Info.DirectoryPath IS the path.
-
Jan 25th, 2010, 05:04 PM
#18
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|