[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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

0
backflash
Asked:
backflash
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
backflashAuthor Commented:
Wow  Great examples and coding
Much appreciated   dbrckovi


Thanks for your kind advice jimbobmcgee, also appreciated

Regards backflash
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now