-
May 13th, 2021, 08:24 AM
#1
Thread Starter
Lively Member
Displaying images
I need to display thumbnails and sized images (JPG, JPEG, BMP, GIF). What would be the best approach, a control or class or just use the native VB controls (image/picture box)?
Thanks in advance!
Rick
-
May 13th, 2021, 08:48 AM
#2
Re: Displaying images
Unless you want to do something very specific I see no reason why you shouldn't use the default picture boxes. What makes you think they might not suit your purpose?
-
May 13th, 2021, 08:54 AM
#3
Thread Starter
Lively Member
Re: Displaying images
Nothing really... I just wanted to see what was out there before I commit. I moved straight from 6.0 (years ago) to Studio 2019 so I expect a lot has changed insofar as classes and controls. In the past I used a 3rd party control but those are not as prolific or affordable as they were back in the day.
Rick
-
May 13th, 2021, 10:03 AM
#4
Re: Displaying images
Okay. Why did you use third party controls? Your question appears to be about just displayin thumbails and vb6 has pretty decent picture box controls.
-
May 13th, 2021, 10:18 AM
#5
Thread Starter
Lively Member
Re: Displaying images
At that time I mainly went with 3rd party controls due to the effects and editing, but that is not as big a deal this time. Thumbnail creation is though, in other words creating an icon and a hover thumbnail. So fact resizing will be critical.
-
May 13th, 2021, 10:30 AM
#6
Re: Displaying images
I've read your posts several times and it still isn't fully clear what you're trying to do. Do you want to display a smaller image, then when the user hovers over the image it expands to its original size?
-
May 13th, 2021, 06:33 PM
#7
Thread Starter
Lively Member
Re: Displaying images
I need to create a thumbnail for use in say a listview control and display a larger thumbnail if the use hovers over the image and even larger if they click on the image.
Rick
-
May 13th, 2021, 09:28 PM
#8
Re: Displaying images
Take a look at this example:
Code:
Public Class Form1
Private ReadOnly _imagePaths As IEnumerable(Of String)
Sub New()
Dim directoryInfo = New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures))
_imagePaths = directoryInfo.GetFiles("*.png").Select(Function(file) file.FullName)
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Cursor = Cursors.WaitCursor
FlowLayoutPanelContainer.Controls.AddRange(CreatePictureBoxes(_imagePaths).ToArray())
Cursor = Cursors.Default
End Sub
Private Function CreatePictureBoxes(paths As IEnumerable(Of String)) As IEnumerable(Of PictureBox)
Dim pictureBoxes = New List(Of PictureBox)
For Each path In paths
Dim pictureBox = New PictureBox() With {
.BorderStyle = BorderStyle.FixedSingle,
.Cursor = Cursors.Hand,
.Margin = New Padding(Convert.ToInt32(Math.Floor(Font.Size))),
.Size = New Size(64, 64),
.SizeMode = PictureBoxSizeMode.StretchImage,
.Tag = path
}
pictureBox.LoadAsync(path)
AddHandler pictureBox.MouseClick, AddressOf PictureBox_MouseClick
AddHandler pictureBox.MouseEnter, AddressOf PictureBox_MouseEnter
AddHandler pictureBox.MouseLeave, AddressOf PictureBox_MouseLeave
pictureBoxes.Add(pictureBox)
Next
Return pictureBoxes
End Function
Private Sub PictureBox_MouseClick(sender As Object, e As MouseEventArgs)
Dim pictureBox = DirectCast(sender, PictureBox)
If (e.Button = MouseButtons.Left) Then
pictureBox.Size = pictureBox.Parent.Size
ElseIf (e.Button = MouseButtons.Right) Then
pictureBox.Size = New Size(255, 255)
End If
End Sub
Private Sub PictureBox_MouseEnter(sender As Object, e As EventArgs)
Dim pictureBox = DirectCast(sender, PictureBox)
pictureBox.Size = New Size(255, 255)
End Sub
Private Sub PictureBox_MouseLeave(sender As Object, e As EventArgs)
Dim pictureBox = DirectCast(sender, PictureBox)
pictureBox.Size = New Size(64, 64)
End Sub
End Class
What this does is:
- Create a String collection
- Populate the collection with every png file from MyPictures
- Change the Form's cursor to the wait cursor (to indicate that it is loading)
- Loop over the collection
- Create a new PictureBox
- Load the image using the currently iterated path
- Bind the MouseClick, MouseEnter, and MouseLeave events:
- In the MouseEnter event, make the size of the control 255, 255
- In the MouseLeave event, make the size of the control 64, 64
- In the MouseClick event:
- During a left mouse click, make the size of the control fill the size of its parent
- During a right mouse click, make the size of the control 255, 255
- Add the controls to a FlowLayoutPanel
- Change the Form's cursor to the default cursor (to indicate that the loading is complete)
In this example I use a simple PictureBox and the total size of the code is 61 lines, including whitespace/styling. There are a few things that this example assumes, which can easily be changed:
- There is a FlowLayoutPanel control added via the designer, named FlowLayoutPanelContainer
- You want png files in MyPictures
You could make this more efficient if the number of images you're loading starts to get too large. For example, you could only create n PictureBoxes at a time and dynamically load them when the FlowLayoutContrainer scrolls, similar to an "infinite scroll" effect, and then cache the loaded images so that once they're loaded you don't need to reload them. But that might be overkill, it all depends on how many images you are handling at any given time.
Last edited by dday9; May 13th, 2021 at 10:07 PM.
Reason: spelin an grammur
-
May 13th, 2021, 10:08 PM
#9
Re: Displaying images
Create an Image object however is appropriate and display it in a PictureBox. Simple. If you want a thumbnail, call GetThumbnailImage on that Image object. Also simple.
For future reference, you don't have to "commit" without testing for yourself whether you ask us or not. You should have tried the standard PictureBox for yourself first, in a simple test project. If it did everything you need then there's no need to ask us at all. If it seemed to be deficient in some way, then you should ask us about that deficiency specifically. Look first, ask questions later.
-
May 17th, 2021, 06:07 AM
#10
Thread Starter
Lively Member
Re: Displaying images
I did experiment with the picture box and have used it in previous versions of this app, I asked to get other ideas that I may not have considered.
-
May 17th, 2021, 06:08 AM
#11
Thread Starter
Lively Member
Re: Displaying images
Thanks a bunch, this is VERY useful..... I ended up using several elements of this code.
Rick
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
|