Last post Aug 15, 2007 10:37 AM by Challenged
Aug 15, 2007 12:18 AM|Challenged|LINK
I need thumbnails of either 190w x 108h or 124w x 70h from an original image that might be way off in terms of proportions(aspect ratio).
Requirements are for exactly sized thumbs no matter what the dimensions are of the original image.
Scratching head on how to shrink image and crop to get the thumb. Doesn't matter if some of the image is missing (ie: tall image ends up as wide thumbnail).
I got this code off a post in this list that I converted to vb.net. Not sure if it is even a start.
Public Sub ResizePicture(ByVal originalpath As String, ByVal newpath As String, ByVal newsize As Size)
Using newbmp As New Bitmap(newsize.Width, newsize.Height), oldbmp As Bitmap = TryCast(Bitmap.FromFile(originalpath), Bitmap)
Using newgraphics As Graphics = Graphics.FromImage(newbmp)
'this line added to set interpolationMode to high quality
newgraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
If CSng(oldbmp.Width) / CSng(newsize.Width) = CSng(oldbmp.Height) / CSng(newsize.Height) Then
'Target size has a 1:1 aspect ratio
newgraphics.DrawImage(oldbmp, 0, 0, newsize.Width, newsize.Height)
ElseIf CSng(oldbmp.Width) / CSng(newsize.Width) > CSng(oldbmp.Height) / CSng(newsize.Height) Then
'There will be white space on the top and bottom
newgraphics.DrawImage(oldbmp, 0.0F, CSng(newbmp.Height) / 2.0F - (oldbmp.Height * (CSng(newbmp.Width) / CSng(oldbmp.Width))) / 2.0F, CSng(newbmp.Width), oldbmp.Height * (CSng(newbmp.Width) / CSng(oldbmp.Width)))
ElseIf CSng(oldbmp.Width) / CSng(newsize.Width) < CSng(oldbmp.Height) / CSng(newsize.Height) Then
'There will be white space on the sides
newgraphics.DrawImage(oldbmp, CSng(newbmp.Width) / 2.0F - (oldbmp.Width * (CSng(newbmp.Height) / CSng(oldbmp.Height))) / 2.0F, 0.0F, oldbmp.Width * (CSng(newbmp.Height) / CSng(oldbmp.Height)), CSng(newbmp.Height))
Aug 15, 2007 02:41 AM|TATWORTH|LINK
Have you seen http://www.codeproject.com/aspnet/EasyThumbs.asp "Shows how to store images in SQL Database Image Storage & create Thumnails easiliy from"
Aug 15, 2007 10:37 AM|Challenged|LINK
Don't need to store image in database, for one. Also, that code will just resize an image rather than crop it from the smallest reduction possible.