Display Images as thumbnails

I have a directory with images of various sizes and would like to display them as thumbnails in my VB5 Application.

I eventually want a page with around 10 thumbnails that I can click on any thumbnail  image and have it displayed  in a larger picturebox

Does anyone help with some code examples so I i can resize the images to fit the picturebox

backflashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dbrckoviCommented:
Hi!

I have uploaded the sample project to http://www.geocities.com/dbrckovi/Thumbs.zip

If you have any questions, ask.
0
dbrckoviCommented:
If you would like to stretch the image to fit in the large Picture Box, then use Image Control instead PictureBox.

 - delete Picture1
 - create Image control  on it's place
 - change Image's name to "Picture1"
 - Set it's BorderStyle to 1 - FixedSingle
 - set it's Stretch property to True
0
anvCommented:
hi  backflash

what u do is...

insert an image control onto ur form...

set its Stretch Property to True..

set its size as per the size of the thumbnail u want to display..

whenever u want to show any thumbnail of an image..

show it into the image control u just added...

for eg:

image1.Picture = LoadPicture("C:\abc.bmp")

Cheers :))

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jimbobmcgeeCommented:
The purpose of thumbnails, in my opinion, is that they are supposed to take less time to load than their full-size counterparts.  If you are displaying the full-size image in a scaled-down Image control, it will take the same amount of time to load as if you showed it full-size in the first place.

So, personally, I would use a batch image processor, such as IrfanView (http://www.irfanview.com) to resample and apply borders as required, in a separate 'thumbnails' directory...

If that's not a concern to you, I would enumerate all images in your folder with the FSO and use that to determine how many image boxes to create.  Then extend a premade (at design time) Image control array by the number of image files found (at runtime) and populate the Image controls as described above.  

If you have a control array of Image controls, you only need to write code once, relating it to the Index of the Image control that was clicked on...

HTH

J.
0
backflashAuthor Commented:
I   had  tried the image and Picturebox  control  but the problem with using the  image control is that the aspect ratio is lost when using the stretch property, and the Picturebox control is resized to fit the picture size.

I was sort of hoping that someone could show me how to scale an image down to fit and centre in a  Picturebox control while retaining the aspect ratio
0
dbrckoviCommented:
Try this:

 - create a picturebox    Picture1
 - inside Picture1 create Image1
  - paste this:
'---------------------------------------------
Private Sub Form_Load()
    Image1.Top = 0
    Image1.Left = 0
   
    LoadImage ("c:\windows\backgrnd.gif")
End Sub

Sub LoadImage(path)
    Dim Change As Single
    Dim OriginalWidth As Single
    Dim OriginalHeight As Single

    Image1.Visible = False
    Image1.Stretch = False
    Image1.Picture = LoadPicture(path)
   
    OriginalWidth = Image1.Width
    OriginalHeight = Image1.Height
    Image1.Stretch = True
   
    If Image1.Width > Picture1.Width Then
        Image1.Width = Picture1.Width
        Change = OriginalWidth / Image1.Width
        Image1.Height = Image1.Height / Change
    End If

    If Image1.Height > Picture1.Height Then
        Image1.Height = Picture1.Height
        Change = OriginalHeight / Image1.Height
        Image1.Width = Image1.Width / Change
    End If

    If Image1.Width < Picture1.Width Then
        Image1.Left = Picture1.Width / 2 - Image1.Width / 2
    End If

    If Image1.Height < Picture1.Height Then
        Image1.Top = Picture1.Height / 2 - Image1.Height / 2
    End If
    Image1.Visible = True
End Sub
'---------------------------------------------------------------------------
0
dbrckoviCommented:
Ups.

There's a bug in my code.
Change loadimage function with this one:           (one line added)
'-----------------------------------------
Sub LoadImage(path)
    Dim Change As Single
    Dim OriginalWidth As Single
    Dim OriginalHeight As Single

    Image1.Visible = False
    Image1.Stretch = False
    Image1.Picture = LoadPicture(path)
   
    OriginalWidth = Image1.Width
    OriginalHeight = Image1.Height
    Image1.Stretch = True
   
    If Image1.Width > Picture1.Width Then
        Image1.Width = Picture1.Width
        Change = OriginalWidth / Image1.Width
        Image1.Height = Image1.Height / Change
        OriginalHeight = Image1.Height
    End If
   
    If Image1.Height > Picture1.Height Then
        Image1.Height = Picture1.Height
        Change = OriginalHeight / Image1.Height
        Image1.Width = Image1.Width / Change
    End If

    If Image1.Width < Picture1.Width Then
        Image1.Left = Picture1.Width / 2 - Image1.Width / 2
    End If

    If Image1.Height < Picture1.Height Then
        Image1.Top = Picture1.Height / 2 - Image1.Height / 2
    End If
    Image1.Visible = True
End Sub
'------------------------------------------------
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
backflashAuthor Commented:
Wow  Great examples and coding
Much appreciated   dbrckovi


Thanks for your kind advice jimbobmcgee, also appreciated

Regards backflash
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.