mike mcintyre's

.N e t J o u r n a l

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

OctNovember 2009Dec
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

Topics

Source Code

Source Code: Generate Thumbnail Images with .NET Image Class and Visual Basic 2005

Generate Thumbnail Images with .NET Image Class and Visual Basic 2005

You can use the .NET 2.0 Image class' GetThumbnailImage method to generate a scaled thumbnail of an image.

If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image.

Screen Shots


Example Code

 

Private Sub GetThumbnail(ByVal pathToImageFile As String, ByVal scaleToPercent As Decimal)

 

    ' Declare a variable named imageAjustmentPercent of type Decimal.

    Dim imageAdjustmentPercent As Decimal

 

    ' Divide the scaleToPercent passed into this method by 100;

    '    assign the result to the imageAjustmentPercent variable.

    imageAdjustmentPercent = CDec(scaleToPercent / 100)

 

    ' Declare two variables of type Integer named

    '    adjustedImageWidth and adjustedImageHeight.

    Dim adjustedImageWidth, adjustedImageHeight As Integer

 

    ' Declare a variable named theImage of type Image.

    Dim theImage As Image

 

    ' Get an image object from the image file;

    '    assign the image object to the theImage variable.

    theImage = Image.FromFile(pathToImageFile)

 

    ' Calculate adjustedImageWidth and adjustedImageHeight using the imageAdjustmentPercent.

    adjustedImageWidth = CType(theImage.Width * imageAdjustmentPercent, Integer)

    adjustedImageHeight = CType(theImage.Height * imageAdjustmentPercent, Integer)

 

    ' Call the Image class' GetThumbnail method to create a new scaled

    '   image object from the orginal image object. Assign it to theImage varable.

    theImage = theImage.GetThumbnailImage(adjustedImageWidth, adjustedImageHeight, Nothing, Nothing)

 

    ' Assign theImage to DemoPicturebox.

    Me.DemoPictureBox.Image = theImage

 

End Sub

Attached is a Visual Studio 2005 Windows Forms Visual Basic solution that demonstrates how to generate scaled thumbnail images from image files.

mike mcintyre  http://www.getdotnetcode.com

posted on Thursday, December 28, 2006 11:26 AM