loading a sequence of images from a selected folder into a picturebox control...
hello, i have a simple photo viewer application where i want the user to click a "load sequence" button and then i want something like a folderbroswerdialog to appear then the user can pick a folder with images in it then the images get loaded into the picturebox and the user can click a "forward" button to go to the next image in the sequence and a "backward" button to go to the previous image in the sequence, it's kinda like windows 7 photo viewer.
Thank you very much guys for your answers :)
Re: loading a sequence of images from a selected folder into a picturebox control...
try this:
vb Code:
Public Class Form1
Dim files() As String
Dim arrayIndex As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'select folder
Dim fbd As New FolderBrowserDialog
fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
arrayIndex = 0
files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
If files.Count > 0 Then
PictureBox1.Image = New Bitmap(files(0))
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'forward
If files.Count > arrayIndex + 1 Then
arrayIndex += 1
PictureBox1.Image = New Bitmap(files(arrayIndex))
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'back
If arrayIndex >= 1 Then
arrayIndex -= 1
PictureBox1.Image = New Bitmap(files(arrayIndex))
End If
End Sub
End Class
Re: loading a sequence of images from a selected folder into a picturebox control...
thank you I'll try it out now
Re: loading a sequence of images from a selected folder into a picturebox control...
wow you're unbelievable it worked straight off the bat! i would like to add you as a friend and ill definitely rate you ;). i have just one other question, how can i have like a global keyboard shortcut to go to the next photo and previous even when my form isn't the active one e.g. crl+alt+ right arrow/ left arrow
Thank you
Re: loading a sequence of images from a selected folder into a picturebox control...
for global hotkeys search the forum for RegisterHotkey/UnRegisterHotkey api functions
Re: loading a sequence of images from a selected folder into a picturebox control...
hi again i got the global hotkeys working and all was well but then i tried a folder with 17 images instead of about 5-4 and it jumps around a bit like if you push forward for the first time it will jump about 5 photos and it also won't start with the first photo in the folder. i think what I'm trying to say is that i want the photos to go in a certain order, if it helps all the photos in the folder are named 1-17 or 1-5 e.g. 1.jpg, 2.jpg ect. (they get created by another program) in that way.
thank you .paul.
Re: loading a sequence of images from a selected folder into a picturebox control...
ok. post your latest code + i'll show you how to sort the files + hopefully stop the jumping
Re: loading a sequence of images from a selected folder into a picturebox control...
I think the code is all the same as last time but here it is:
vb Code:
Dim files() As String
Dim arrayIndex As Integer
Private Sub LoadSequenceToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LoadSequenceToolStripMenuItem.Click
'select folder
Dim fbd As New FolderBrowserDialog
fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
arrayIndex = 0
files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
If files.Count > 0 Then
PictureBox1.Image = New Bitmap(files(0))
End If
End If
End Sub
'forward
If files.Count > arrayIndex + 1 Then
arrayIndex += 1
PictureBox1.Image = New Bitmap(files(arrayIndex))
End If
'back
If arrayIndex >= 1 Then
arrayIndex -= 1
PictureBox1.Image = New Bitmap(files(arrayIndex))
End If
it just seems to go in whatever order it wants and jumps around all over the place which wont work with what I'm trying to design. I also noticed that we're in a bit of a time difference because your in the UK and I'm in Australia, right now it's 12:24pm in aus lol i don't know what time it is in the UK but hopefully you can reply as soon as possible. thanks
Re: loading a sequence of images from a selected folder into a picturebox control...
sorry for the delay. as you said we're 11 hours behind Sydney here + i slept in as it's New Years Day:D
vb Code:
Private Sub LoadSequenceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadSequenceToolStripMenuItem.Click
'select folder
Dim fbd As New FolderBrowserDialog
fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
arrayIndex = 0
files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
Try
Array.Sort(files, Function(x, y) CInt(IO.Path.GetFileNameWithoutExtension(x)).CompareTo(CInt(IO.Path.GetFileNameWithoutExtension(y))))
Catch ex As Exception
End Try
If files.Count > 0 Then
PictureBox1.Image = New Bitmap(files(0))
End If
End If
End Sub
Re: loading a sequence of images from a selected folder into a picturebox control...
Okay that's awesome it works! now there one more thing i need if i may.
okay so what i'm aiming to do is have three picture boxes stacked under each other and i want the first picturebox "picturebox1" to have 1 image before picturebox2 and i want picturebox2 to have one image before picturebox3, so i want picturebox three to have the latest image and picturebox2 to have the middle image and picturebox1 to have the oldest image and when you click the forward and backward buttons they all change accordingly. it might be hard to understand but here's the minor code changes i did to get a semi working effect that jumps to many images instead of just having three images in the three pictureboxes.
vb Code:
Private Sub LoadSequenceToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles LoadSequenceToolStripMenuItem.Click
'select folder
Dim fbd As New FolderBrowserDialog
fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
arrayIndex = 0
files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
Try
Array.Sort(files, Function(x, y) CInt(IO.Path.GetFileNameWithoutExtension(x)).CompareTo(CInt(IO.Path.GetFileNameWithoutExtension(y))))
Catch ex As Exception
End Try
If files.Count > 0 Then
PictureBox1.Image = New Bitmap(files(0))
PictureBox2.Image = New Bitmap(files(0))
PictureBox3.Image = New Bitmap(files(0))
End If
End If
End Sub
'forward
If files.Count > arrayIndex + 1 Then
arrayIndex += 1
PictureBox1.Image = New Bitmap(files(arrayIndex))
End If
If files.Count > arrayIndex + 2 Then
arrayIndex += 2
PictureBox2.Image = New Bitmap(files(arrayIndex))
End If
If files.Count > arrayIndex + 3 Then
arrayIndex += 3
PictureBox3.Image = New Bitmap(files(arrayIndex))
End If
'back
If arrayIndex >= 1 Then
arrayIndex -= 1
PictureBox1.Image = New Bitmap(files(arrayIndex))
End If
If arrayIndex >= 2 Then
arrayIndex -= 2
PictureBox2.Image = New Bitmap(files(arrayIndex))
End If
If arrayIndex >= 3 Then
arrayIndex -= 3
PictureBox3.Image = New Bitmap(files(arrayIndex))
End If
pretty simple minded i know :p but it's pretty much the best i could do, i'm just really happy i have your brilliant help for the time being and i'm very curious to know how someone can get to your level of vb.net skill i've been doing vb on and off for a little while now and i can't seem to even remember those large amounts of code, anyways thank you very much Paul i'm very grateful. i'll be looking forward to your next post :)
Re: loading a sequence of images from a selected folder into a picturebox control...
try this. i didn't thoroughly test it + it might need tweaking if your selected folder has less than 3 picture files:
vb Code:
Public Class Form1
Dim files() As String
Dim arrayIndex As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'select folder
Dim fbd As New FolderBrowserDialog
fbd.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures
Dim extensions() As String = {".bmp", ".gif", ".jpg", ".png"}
If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
arrayIndex = 0
files = IO.Directory.GetFiles(fbd.SelectedPath).Where(Function(s1) extensions.Any(Function(s2) s2 = IO.Path.GetExtension(s1))).ToArray
Try
Array.Sort(files, Function(x, y) CInt(IO.Path.GetFileNameWithoutExtension(x)).CompareTo(CInt(IO.Path.GetFileNameWithoutExtension(y))))
Catch ex As Exception
End Try
PictureBox1.Image = If(files.Count >= 1, New Bitmap(files(0)), Nothing)
PictureBox2.Image = If(files.Count >= 2, New Bitmap(files(1)), Nothing)
PictureBox3.Image = If(files.Count >= 3, New Bitmap(files(2)), Nothing)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'back
If arrayIndex >= 1 Then
arrayIndex -= 1
PictureBox1.Image = If(files.Count >= arrayIndex, New Bitmap(files(arrayIndex)), Nothing)
PictureBox2.Image = If(files.Count >= arrayIndex + 1, New Bitmap(files(arrayIndex + 1)), Nothing)
PictureBox3.Image = If(files.Count >= arrayIndex + 3, New Bitmap(files(arrayIndex + 2)), Nothing)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'forward
If files.Count > arrayIndex + 3 Then
arrayIndex += 1
PictureBox1.Image = If(files.Count >= arrayIndex, New Bitmap(files(arrayIndex)), Nothing)
PictureBox2.Image = If(files.Count >= arrayIndex + 1, New Bitmap(files(arrayIndex + 1)), Nothing)
PictureBox3.Image = If(files.Count >= arrayIndex + 3, New Bitmap(files(arrayIndex + 2)), Nothing)
End If
End Sub
End Class
Re: loading a sequence of images from a selected folder into a picturebox control...
okay this ones probably an easy one but say if i have a menustrip button that says new window and i want it to open another from1 like having too form ones open and you can just keep on opening new ones how would i do that, i've seen it done before and i already tried goggling it. Thanks
Re: loading a sequence of images from a selected folder into a picturebox control...
Quote:
Originally Posted by
happycamper1221
i'm very curious to know how someone can get to your level of vb.net skill i've been doing vb on and off for a little while now and i can't seem to even remember those large amounts of code
i've been programming since about 1992.
you'll remember more as you go forward + build on your experience.
have a look at this:
www.homeandlearn.co.uk
Re: loading a sequence of images from a selected folder into a picturebox control...
Quote:
Originally Posted by
happycamper1221
okay this ones probably an easy one but say if i have a menustrip button that says new window and i want it to open another from1 like having too form ones open and you can just keep on opening new ones how would i do that, i've seen it done before and i already tried goggling it. Thanks
here's how. i used a standard button, but you know how to use a menustrip button already.
in your vb IDE, select Project-->Add Windows Form to add Form2, then:
vb Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim frm As New form2
frm.show()
End Sub
Re: loading a sequence of images from a selected folder into a picturebox control...
hey again, would there be a simple way to add a fading transition effect between image changes? if so how would i do it.
Re: loading a sequence of images from a selected folder into a picturebox control...
here's an example that fades in/out the picturebox.image, so it alternately shows the picturebox.backgroundimage/picturebox.image:
http://www.scproject.biz/fadeImage.zip
Re: loading a sequence of images from a selected folder into a picturebox control...
okay so i have this code working :
vb Code:
Imports System.Drawing.Imaging
Public Class Form1
Dim fadeImage As Bitmap
Dim alpha As Decimal = 1D
Dim increment As Decimal = -0.1D
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
If alpha = 1D Then
increment = -0.05D
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim img As New Bitmap("Penguins.jpg")
PictureBox1.Image = img
img = New Bitmap("Desert.jpg")
PictureBox1.BackgroundImage = img
fadeImage = DirectCast(PictureBox1.Image.Clone, Bitmap)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
alpha = CDec(alpha + increment)
Dim image_attr As New ImageAttributes
Dim cm As ColorMatrix
Dim bm As New Bitmap(fadeImage.Width, fadeImage.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
Dim rect As Rectangle = _
Rectangle.Round(fadeImage.GetBounds(GraphicsUnit.Pixel))
cm = New ColorMatrix(New Single()() { _
New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 0.0, CSng(alpha), 1.0}})
image_attr.SetColorMatrix(cm)
gr.DrawImage(fadeImage, rect, 0, 0, fadeImage.Width, fadeImage.Height, _
GraphicsUnit.Pixel, image_attr)
PictureBox1.Image = bm
If alpha = 0D Then
increment = 0.05D
Timer1.Enabled = False
ElseIf alpha = 1D Then
'i got rid of the increment thing here because it wouldn't do anything when i changed it
Timer1.Enabled = False
End If
End Sub
End Class
Re: loading a sequence of images from a selected folder into a picturebox control...
okay so i have this code working :
vb Code:
Imports System.Drawing.Imaging
Public Class Form1
Dim fadeImage As Bitmap
Dim alpha As Decimal = 1D
Dim increment As Decimal = -0.1D
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
' i added a new increment code here so i could change the increment both ways.
If alpha = 1D Then
increment = -0.05D
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim img As New Bitmap("Penguins.jpg")
PictureBox1.Image = img
img = New Bitmap("Desert.jpg")
PictureBox1.BackgroundImage = img
fadeImage = DirectCast(PictureBox1.Image.Clone, Bitmap)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
alpha = CDec(alpha + increment)
Dim image_attr As New ImageAttributes
Dim cm As ColorMatrix
Dim bm As New Bitmap(fadeImage.Width, fadeImage.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
Dim rect As Rectangle = _
Rectangle.Round(fadeImage.GetBounds(GraphicsUnit.Pixel))
cm = New ColorMatrix(New Single()() { _
New Single() {1.0, 0.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 1.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 1.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 0.0, 0.0, 0.0}, _
New Single() {0.0, 0.0, 0.0, CSng(alpha), 1.0}})
image_attr.SetColorMatrix(cm)
gr.DrawImage(fadeImage, rect, 0, 0, fadeImage.Width, fadeImage.Height, _
GraphicsUnit.Pixel, image_attr)
PictureBox1.Image = bm
If alpha = 0D Then
increment = 0.05D
Timer1.Enabled = False
ElseIf alpha = 1D Then
'i got rid of the increment thing here because it wouldn't do anything when i changed it.
Timer1.Enabled = False
End If
End Sub
End Class
and what would be fantastic is if i could have some sort of class or separate file that i could just import in the one place and just reference to for this fade so for example which i have no idea what I'm typing:
vb Code:
Private Sub PictureBox2_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox2.Click
'maybe there's some variables declared like "fademe" and "img1", "img2"
fademe.images = img1,amg2
img1 = my.resources.img1
img2 = my.resources.img2
fademe.intervalin = 0.5
fademe.intervalout = 0.5
"ect... no idea - but i think i just invented a new programming language :p.
End Sub
so if you see what i mean (if it's possible) how could i go about doing this. once again thank you so so much you are my Savior :).