VB icon

Making a thumbnail of an image

Email
Submitted on: 1/3/2015 12:50:00 PM
By: Andrew (from psc cd)  
Level: Not Given
User Rating: By 4 Users
Compatibility: VB 5.0, VB 6.0
Views: 666
 
     Being optimistic, I have made a code that will resize an image, allowing the user to save the image as a thumbnail. Here's hoping I've solved problems. Even if it doesn't work for you, it should give you a small idea of how it's done.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Making a thumbnail of an image
' Description:Being optimistic, I have made a code that will resize an image, allowing the user to save the image as a thumbnail. Here's hoping I've solved problems.
Even if it doesn't work for you, it should give you a small idea of how it's done.
' By: Andrew (from psc cd)
'
' Inputs:All provided
'
' Assumes:Source and dest are two picturebox objects.
'
' Side Effects:I haven't had much time to test it, but BE WARNED; it might not work if the source image is smaller than your thumbnail size. I tested this at 60 x 80.
'**************************************

Sub thumbnail(width As Integer, height As Integer, source As PictureBox, dest As PictureBox)
 'This should help me to create a thumbnail of an image.
 
 'ix and iy help to grab the pixels from the relative positions
 'of the thumbnail from the image.
 Dim ix As Single, iy As Single
 
 'x and y are just For...Next variables and xcounter/ycounter
 'are used for reference to the thumbnail.
 Dim x As Single, y As Single, xcounter As Integer, ycounter As Integer
 
 'These are a few safety precautions that you should take to
 'make sure that the code works. The ScaleMode of the
 'pictureboxes and their parents must be pixels.
 source.Parent.ScaleMode = vbPixels
 dest.Parent.ScaleMode = vbPixels
 source.ScaleMode = vbPixels
 dest.ScaleMode = vbPixels
 
 'Calculate ix and iy, which are the 'steps' from which to grab
 'pixels. Think of it as a fixed grid.
 ix = source.ScaleWidth / width
 iy = source.ScaleHeight / height
 
 'Resize the thumbnail picturebox to accomodate the new
 'thumbnail. There's a trap here; the thumbnail may not be
 'exactly the size required.
 'If you simply put dest.height = height and so on for the
 'width, you might get the extra border on the right and
 'bottom of the thumbnail.
 dest.height = source.ScaleHeight / iy
 dest.width = source.ScaleWidth / ix
 'Now we make the thumbnail.
 For y = 0 To source.ScaleHeight - 1 Step iy
 For x = 0 To source.ScaleWidth - 1 Step ix
'Grab the image from the source and place it in the
'right spot in the thumbnail picture box.
dest.PSet (xcounter, ycounter), source.Point(x, y)
xcounter = xcounter + 1
 Next
 ycounter = ycounter + 1
 xcounter = 0
 Next
 'The next line is not mandatory, except if you want the
 'thumbnail to become a picture object.
 Set dest.Picture = dest.Image
End Sub
'To save the thumbnail you would then write a line such as
'SavePicture dest.picture, "thumbnail.bmp" (or
'SavePicture dest.image), remembering that the result is a
'bitmap picture.


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.