locked
making image thumnails RRS feed

  • 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

     

    Sub make_thumb(ByVal filename As String, ByVal itemid As Integer, ByVal destination As String, ByVal type As String)

    Dim objImage, objThumbnail As System.Drawing.Image

    Dim strServerPath, strFilename As String

    Dim width As Short

    Dim shtWidth, shtHeight As Short

    ' Get image folder path on server - use "\" string if root

    strServerPath = Server.MapPath(destination)

    ' Retrieve name of file to resize from query string

    filename = itemid & "_" & filename

    strFilename = strServerPath & filename

    ' Retrieve file, or error.gif if not available

    Try

    objImage = Image.FromFile(strFilename)

    Catch ex As Exception

    lbstatus.Text = ex.Message & strFilename

    objImage = Image.FromFile(strServerPath &
    "error.gif")

    End Try

    'check the type

    If type = "thumb_" Then

    If objImage.Width > objImage.Height Then

    width = 93

    Else

    width = 70

    End If

    ElseIf type = "catpic_" Then

    If objImage.Width > objImage.Height Then

    width = 285

    Else

    width = 238

    End If

    ElseIf type = "special_" Then

    If objImage.Width > objImage.Height Then

    width = 50

    Else

    width = 38

    End If

     

    End If

    ' check the type

    If width = Nothing Then

    shtWidth = objImage.Width

    ElseIf width < 1 Then

    shtWidth = 70

    Else

    shtWidth = width

    End If

    ' Work out a proportionate height from width

    shtHeight = objImage.Height / (objImage.Width / shtWidth)

    ' Create thumbnail

    objThumbnail = objImage.GetThumbnailImage(shtWidth, _

    shtHeight,
    Nothing, System.IntPtr.Zero)

    ' Send down to client

    Response.ContentType = "image/jpeg"

    objThumbnail.Save(strServerPath & type & filename, Imaging.ImageFormat.Jpeg)

    ' Tidy up

    objImage.Dispose()

    objThumbnail.Dispose()

    End Sub

    Monday, 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 = 72

            resizedImage.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 - 1

     

     

                If arrayICI(x).FormatDescription.Equals("JPEG") Then


                    jpegICI = arrayICI(x)


                    Exit For

                End 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 ratio

            Dim 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.Height

                newWidth = Convert.ToInt32(ratio * bmpIn.Width)


                newHeight = fixedHeight
            Else

     

                ratio = CDec(fixedWidth) / bmpIn.Width

                newHeight = Convert.ToInt32(ratio * bmpIn.Height)


                newWidth = fixedWidth
            End If

     

            bmpOut = 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 FileUpload1

            Dim 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

     

    startOptimization(FileUpload1, 250, 100, FileUpload1.FileName, "../pics/")<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><o:p></o:p>
    Sunday, September 28, 2008 5:18 PM