Create new project.
Form size should be around 600, 380.
Add a label on the top-left part of the main form, text: Folder
Add a button below the label, text: Load Folder
Add a button on the right of the previous button, text: Change View
Below the two buttons add a ListView from the toolbox, size around 180, 180
Click on the listview -> go to properties -> View: change view to "List"
On the right of the ListView and the 2 buttons add a big PictureBox, size around 340, 230
Click on the PictureBox -> go to properties -> SizeMode: change to "StretchImage"
Add a label above the picture box, text: Filename
Go to toolbox, double click a FolderBrowseDialog in order to add it to your program.
We will load the pictures in the listview and then we can view them in the picturebox from there.
Double click on the "load folder" button and go to the coding page. At the very start, before the Form1 class starts, insert:
Imports System.IO
- this is necessary to initialize when we are loading files into the code.
Outside the button1 sub declare this variables:
Private a1 As String()
Private a2 As String() = {".jpg", ".jpeg", ".png"}
- we will use this variables to load the content of a folder inside and then filter the file extensions we desire.
Inside button1 sub add this:
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
a1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
- all of this was already explained in example 11. We needed "imports system.IO" in order to be able to use the "Directory.Getfiles" function. "a1" will store all the files from the selected folder path. The "for" loop will filter the extensions we added when declaring "a2".
ListView1.Items.Add(i) - inside the for loop we'll add the desired files to our listview
Go to the design page and double click on the Change view button. Before we write anything inside the sub, we'll have to declare a variable outside:
Private cc As Integer
Inside the button2 sub insert:
- every button2 click will change the view inside the listview.
In this case we have only 5 possible choices between numbers 1 to 5 which represent the 5 possible views a listview can take. You can click on the ListView1 object on the form -> go to properties -> find View and click on the dropdown menu to notice the possible views.
The "Select case" statement is used for simplifying the process of choosing one single element out of a group of elements. This statement could always be replaced with a whole bunch of IF-Then statements, but that would only complicate and widen the code written. So, we mainly use this statement to simplify and shorten our code. Standardly it gets used when creating menus.
Before we continue with the next sub, we must declare another variable outside:
Private d As String
Double click on the listview element. Change the mouse event of the sub from this:
Insert this inside the sub:
Every time we click on an item inside the listview, we take the filepath of that item from it's name and we insert it into the picturebox.
PictureBox1.Image = Image.FromFile(d) - this is how we enter an image into the picturebox directly from a file. "d" is obviously going to be the file path for the image.
You can easily insert a message box at the beginning of this sub to check out how the "ListView1.FocusedItem.ToString" looks like.
Go to the "Solution Explorer" window -> Right click with your mouse on the name of your application -> click on the "Add" option -> click on "Windows form..." -> click "Add".
This will add another window form to your application, "Form2.vb" tab should be present now.
Go to this new tab and make the form somewhat bigger.
Go back to Form1 and double click on the listview again. Replace the currently handled mouse event:
Handles ListView1.SelectedIndexChanged
with:
Handles ListView1.MouseDoubleClick
Inside the sub add this:
- double clicking on an item in the listview will make the new form appear with the desired picture inside.
Form2.ClientSize = Image.FromFile(d).Size - the new form will have the same size as the picture we are loading inside
Inside form1, below the listview control, add another button, text: Draw Circle
On the right of this button add another button, text: Clear
On the right of the clear button add one more, text: Draw Ellipse
Double click on the "Draw circle" button, add this inside:
- we will be using "ff" to draw an ellipse inside the bounds of the picturebox. The location of the drawing is relative to the starting location of the picturebox.
If we wrote "Dim ff As Graphics = CreateGraphics()" then the location would be relative to the form's client area.
ff.DrawEllipse(Pens.White, 40, 40, 50, 50) - same as drawing a rectangle, the first 2 numbers inside the parentheses determine the location of the drawing while the following two determine the size. Having the same width and height values obviously give us a circle rather than an ellipse.
It's important to notice that we can drawn inside the control even when there is no picture inside - we are just drawing inside the space of the control (as if drawing "on top of it"). If there was a picture present inside the picturebox in that moment, and we were to draw the same ellipse there again, it's essential to remember that we are not drawing on the picture itself (changing the picture itself) - we are just drawing on top of the picturebox control (while bound to it's size inside the program).
Go to the design page and double click the Clear button. Insert inside the sub:
Refresh()
- since we used an object as our drawing canvas, clicking on the "Clear" button will result in removing our drawing.
Double click the "Draw Ellipse" button, add this inside the sub:
Dim ff As Graphics = Graphics.FromImage(PictureBox1.Image) - the painting canvas of the "ff" variable is going to be the image taken from the picturebox.
ff.FillEllipse(Brushes.Red, 60, 60, 160, 80) - this will paint an ellipse on the picturebox image. Every time we use ff.some_function() -> then "some_function" will be changing the graphics (the image) that we connected with "ff". Without adding "refresh" at the end we wouldn't see the changes that were made to the picturebox image. Clicking on the "Clear" button won't remove the drawing made by the Draw Ellipse button.
The Draw Ellipse button requires the presence of an image to work otherwise pressing it will make the program crash.
Go to the design page, click on the Draw Ellipse button, go to properties, change "visible" to false.
Inside the coding page go to the first listview sub and insert this at the bottom:
In the design page add another button, text: Save Image. Double click the button, add inside:
PictureBox1.Image.Save("E:\Image.jpg")
- choose any save location you prefer inside the brackets.
Final code:
Public Class Form1
Private a1 As String()
Private a2 As String() = {".jpg", ".jpeg", ".png"}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
Label1.Text = FolderBrowserDialog1.SelectedPath
a1 = Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*.*")
For Each i In a1
For i2 = 0 To a2.GetUpperBound(0)
If i.EndsWith(a2(i2)) Then
ListView1.Items.Add(i)
End If
Next
Next
End If
End Sub
Private cc As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
cc = cc + 1
Select Case cc
Case 1
ListView1.View = View.LargeIcon
Case 2
ListView1.View = View.Details
Case 3
ListView1.View = View.SmallIcon
Case 4
ListView1.View = View.List
Case 5
ListView1.View = View.Tile
cc = 0
End Select
End Sub
Private d As String
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.MouseClick
d = Replace(ListView1.FocusedItem.ToString, "ListViewItem: {", "")
d = Replace(d, "}", "")
PictureBox1.Image = Image.FromFile(d)
Label2.Text = Replace(d, FolderBrowserDialog1.SelectedPath, "")
If Button5.Visible = False Then
Button5.Visible = True
End If
End Sub
Private Sub ListView1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.MouseDoubleClick
d = Replace(ListView1.FocusedItem.ToString, "ListViewItem: {", "")
d = Replace(d, "}", "")
Form2.ClientSize = Image.FromFile(d).Size
Form2.BackgroundImage = Image.FromFile(d)
Form2.Show()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim ff As Graphics = PictureBox1.CreateGraphics
ff.DrawEllipse(Pens.White, 40, 40, 50, 50)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Refresh()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim ff As Graphics = Graphics.FromImage(PictureBox1.Image)
ff.FillEllipse(Brushes.Red, 60, 60, 160, 80)
Refresh()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
PictureBox1.Image.Save("E:\Image.jpg")
End Sub
End Class
Start program.
Load image into picturebox, press Draw Circle button, then Save.
Load image into picturebox, press Draw Ellipse button, then Save.
Drawings present "on top" of controls won't be saved when we are saving the image that is "below them". We have to be changing the image itself instead and then save it.