Asked by:
making image thumnails

Question
-
User-1364604259 posted
is thier a way to create thumbnails of images with the option to determain the jpg comprission. the below code generate the thumbs but with bad compression
strServerPath = Server.MapPath(destination)
' Retrieve name of file to resize from query string filename = itemid & "_" & filenamestrFilename = strServerPath & filename
' Retrieve file, or error.gif if not available TryobjImage = Image.FromFile(strFilename)
Catch ex As Exceptionlbstatus.Text = ex.Message & strFilename
objImage = Image.FromFile(strServerPath & "error.gif") End Try 'check the type If type = "thumb_" Then If objImage.Width > objImage.Height Thenwidth = 93
Elsewidth = 70
End If ElseIf type = "catpic_" Then If objImage.Width > objImage.Height Thenwidth = 285
Elsewidth = 238
End If ElseIf type = "special_" Then If objImage.Width > objImage.Height Thenwidth = 50
Elsewidth = 38
End IfshtWidth = objImage.Width
ElseIf width < 1 ThenshtWidth = 70
ElseshtWidth = width
End If ' Work out a proportionate height from widthshtHeight = objImage.Height / (objImage.Width / shtWidth)
' Create thumbnailobjThumbnail = objImage.GetThumbnailImage(shtWidth, _
shtHeight, Nothing, System.IntPtr.Zero) ' Send down to clientResponse.ContentType =
"image/jpeg"objThumbnail.Save(strServerPath & type & filename, Imaging.ImageFormat.Jpeg)
' Tidy upobjImage.Dispose()
objThumbnail.Dispose()
End SubMonday, August 11, 2008 3:50 PM
All replies
-
User966832816 posted
http://codebetter.com/blogs/eric.wise/archive/2005/05/15/63236.aspx
http://www.codeproject.com/KB/GDI-plus/thumbgenerator.aspx
Tuesday, August 12, 2008 3:51 AM -
User-1364604259 posted
Thank for your replay
following the link it directed me to http://www.imageoptimizer.net/pages/?page=TryNow with a very nice control. but its not free any other suggestion for a free code to do excatly the same as this one
Thursday, August 14, 2008 5:59 AM -
User-1364604259 posted
i found the solution on the net and made some modification. here it is
Private Sub WarpImageDimensions(ByVal imageToSave As Bitmap, ByVal width As Integer, ByVal imgquality As Long, ByVal Savename As String, ByVal directory As String)
' RE-SIZE THE IMAGE ACCORDING TO A FIXED WIDTH OF 350 PIXELS
Dim resizedImage As Bitmap = ResizeSubmittedImage(imageToSave, width)' SET THE RESOLUTION OF THE IMAGE TO 72dpi
Const res As Single = 72resizedImage.SetResolution(res, res)
' SET THE INTERPOLATION MODE
Dim g As Graphics = Graphics.FromImage(resizedImage)g.InterpolationMode = InterpolationMode.HighQualityBicubic
' RE-SET THE IMAGE'S COMPRESSION QUALITY TO "30"
Dim encoderParams As New EncoderParameters()Dim quality As Long() = New Long(0) {}
quality(0) = imgquality
Dim ep As New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)
encoderParams.Param(0) = ep
Dim arrayICI As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
Dim jpegICI As ImageCodecInfo = Nothing
For x As Integer = 0 To arrayICI.Length - 1If arrayICI(x).FormatDescription.Equals("JPEG") Then
jpegICI = arrayICI(x)
Exit ForEnd If
Next' Dim mem As New MemoryStream()
Dim path = Server.MapPath(directory)resizedImage.Save(path & Savename, jpegICI, encoderParams)
' SAVE THE IMAGE TO THE DATABASE AS A BINARY BYTE ARRAY
' Dim bits As Byte() = mem.ToArray()' do database INSERT operations into DB field of type IMAGE here
'mem.Close()
g.Dispose()
End Sub
Private Function ResizeSubmittedImage(ByVal bmpIn As Bitmap, ByVal width As Integer) As Bitmap
' re-size a submitted image while maintaining its aspect ratioDim bmpOut As Bitmap = Nothing
Dim newHeight As Integer = 0
Dim newWidth As Integer = 0
Dim fixedWidth As Integer = width
Dim fixedHeight As Integer = bmpIn.Height / (bmpIn.Width / width)
Dim ratio As Decimal
' if the image is too small, then just use it as is
If bmpIn.Width < fixedWidth Then
ratio = CDec(fixedHeight) / bmpIn.HeightnewWidth = Convert.ToInt32(ratio * bmpIn.Width)
newHeight = fixedHeight
Elseratio = CDec(fixedWidth) / bmpIn.Width
newHeight = Convert.ToInt32(ratio * bmpIn.Height)
newWidth = fixedWidth
End IfbmpOut = New Bitmap(newWidth, newHeight)
Dim g As Graphics = Graphics.FromImage(bmpOut)
g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight)
g.DrawImage(bmpIn, 0, 0, newWidth, newHeight)
bmpIn.Dispose()
Return bmpOut
End Function
Protected Sub startOptimization(ByVal controlname As FileUpload, ByVal width As String, ByVal quality As String, ByVal savename As String, ByVal directory As String)
' get the stream of data for the image from a server-side Form with an <INPUT> control with an ID of FileUpload1Dim length As Integer = CInt(controlname.PostedFile.InputStream.Length)
Dim imageBits As Byte() = New Byte(length - 1) {}
' read the digital bits of the image into the byte array
controlname.PostedFile.InputStream.Read(imageBits, 0, length)
' save the byte array as a Bitmap object
Dim ms As New MemoryStream()
ms.Write(imageBits, 0, imageBits.Length)
Dim unrenderedImage As New Bitmap(controlname.PostedFile.InputStream)
'ms.Dispose
' ms.Close()' manipulate the image according to the specification and save it to the database
WarpImageDimensions(unrenderedImage, width, quality, savename, directory)
End Sub
' to call the optimization method
startOptimization(controlname, width, quality, filenametosave, destination)
example
Sunday, September 28, 2008 5:18 PM