Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3609538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:38:26+00:00 2026-05-18T21:38:26+00:00

Anyone know of a ASP.Net version of the famous PHP class timthumb? Just need

  • 0

Anyone know of a ASP.Net version of the famous PHP class “timthumb”? Just need a script which will work in the same line of “timthumb” and produce square or ratio based thumbnails with good quality for any sized images.

Here is the link to the php class: http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:38:26+00:00Added an answer on May 18, 2026 at 9:38 pm

    I wrote this up just for you 🙂 … I tested it by trying all of the cases in http://www.binarymoon.co.uk/demo/timthumb-basic/ and comparing it visually side by side with my code. As far as I can tell it’s basically identical. With that said it does not support the ‘zc’ flag.

    I wrote it as a generic handler (ashx file) so that it should run faster. Imaging caching is supported but I commented it out since it makes testing fixes very tough. Feel free to uncomment it for improved performance over the long run (one block right at the beginning and one near the end when it writes it to the memory stream).

    —Code—

    <%@ WebHandler Language="VB" Class="Thumb" %>
    
        Imports System
        Imports System.Web
        Imports System.Drawing
        Imports System.IO
    
        Public Class Thumb : Implements IHttpHandler
            Private Shared _DefaultWidth As Integer = 100
            Private Shared _DefaultHeight As Integer = 100
            Private Shared _DefaultQuality As Integer = 90
    
            Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
                ''check the cache for the image first
                'Dim cacheObj As Object = context.Cache("Thumb-" + context.Request.Url.ToString().ToLower())
                'If cacheObj IsNot Nothing Then
                '    Dim msCache As MemoryStream = DirectCast(cacheObj, MemoryStream)
    
                '    WriteImage(msCache, context)
    
                '    Exit Sub
                'End If
    
                'process request (since it wasn't in the cache)
                Dim imgPath As String = context.Request.QueryString("src")
    
                If String.IsNullOrEmpty(imgPath) Then
                    context.Response.End()
                End If
    
                'get image path on server
                Dim serverPath As String = context.Server.MapPath(imgPath)
                If Not File.Exists(serverPath) Then
                    context.Response.End()
                End If
    
                'load image from file path
                Dim img As Image = Bitmap.FromFile(serverPath)
                Dim origRatio As Double = (Math.Min(img.Width, img.Height) / Math.Max(img.Width, img.Height))
    
                '---Calculate thumbnail sizes---
                Dim destWidth As Integer = 0
                Dim destHeight As Integer = 0
                Dim destRatio As Double = 0
    
                Dim qW As String = context.Request.QueryString("w")
                Dim qH As String = context.Request.QueryString("h")
    
                If Not String.IsNullOrEmpty(qW) Then
                    Int32.TryParse(qW, destWidth)
                    If destWidth < 0 Then
                        destWidth = 0
                    End If
                End If
    
                If Not String.IsNullOrEmpty(qH) Then
                    Int32.TryParse(qH, destHeight)
                    If destHeight < 0 Then
                        destHeight = 0
                    End If
                End If
    
                'if both width and height are 0 then use defaults (100x100)
                If destWidth = 0 And destHeight = 0 Then
                    destWidth = _DefaultWidth
                    destHeight = _DefaultHeight
    
                    'else, if the width is specified, calculate an appropriate height
                ElseIf destWidth > 0 And destHeight > 0 Then
                    'do nothing, we have both sizes already
                ElseIf destWidth > 0 Then
                    destHeight = Math.Floor(img.Height * (destWidth / img.Width))
                ElseIf destHeight > 0 Then
                    destWidth = Math.Floor(img.Width * (destHeight / img.Height))
                End If
    
                destRatio = (Math.Min(destWidth, destHeight) / Math.Max(destWidth, destHeight))
    
                'calculate source image sizes (rectangle) to get pixel data from        
                Dim sourceWidth As Integer = img.Width
                Dim sourceHeight As Integer = img.Height
    
                Dim sourceX As Integer = 0
                Dim sourceY As Integer = 0
    
                Dim cmpx As Integer = img.Width / destWidth
                Dim cmpy As Integer = img.Height / destHeight
    
                'selection is based on the smallest dimension
                If cmpx > cmpy Then
                    sourceWidth = img.Width / cmpx * cmpy
                    sourceX = ((img.Width - (img.Width / cmpx * cmpy)) / 2)
                ElseIf cmpy > cmpx Then
                    sourceHeight = img.Height / cmpy * cmpx
                    sourceY = ((img.Height - (img.Height / cmpy * cmpx)) / 2)
                End If
    
                '---create the new thumbnail image---
                Dim bmpThumb As New Bitmap(destWidth, destHeight)
                Dim g = Graphics.FromImage(bmpThumb)
                g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                g.DrawImage(img, _
                            New Rectangle(0, 0, destWidth, destHeight), _
                            New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)
    
                '-----write out Thumbnail to the output stream------        
                'get jpeg image coded info so we can use it when saving
                Dim ici As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders().Where(Function(c) c.MimeType = "image/jpeg").First()
    
                'save image to memory stream
                Dim ms As New MemoryStream()
                bmpThumb.Save(ms, ici, BuildQualityParams(context))
    
                ''save image to cache for future use
                'context.Cache("Thumb-" + context.Request.Url.ToString().ToLower()) = ms
    
                'write the image out
                WriteImage(ms, context)
            End Sub
    
            Private Sub WriteImage(ByVal ms As MemoryStream, ByVal context As HttpContext)
                'clear the response stream
                context.Response.Clear()
    
                'set output content type to jpeg
                context.Response.ContentType = "image/jpeg"
    
                'write memory stream out
                ms.WriteTo(context.Response.OutputStream)
    
                'flush the stream and end the response
                context.Response.Flush()
                context.Response.End()
            End Sub
    
            'for adjusting quality setting if needed, otherwise 90 will be used
            Private Function BuildQualityParams(ByVal context As HttpContext) As Imaging.EncoderParameters
                Dim epParams As New Imaging.EncoderParameters(1)
    
                Dim q As Integer = _DefaultQuality
    
                Dim strQ As String = context.Request.QueryString("q")
                If Not String.IsNullOrEmpty(strQ) Then
                    Int32.TryParse(strQ, q)
                End If
    
                epParams.Param(0) = New Imaging.EncoderParameter(Imaging.Encoder.Quality, q)
    
                Return epParams
            End Function
    
            Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
                Get
                    Return False
                End Get
            End Property
        End Class
    

    —Test Cases—

    <img alt="" src="Thumb.ashx?src=~/images/castle1.jpg" /> 
    <br />
    <img alt="" src="Thumb.ashx?src=~/images/castle1.jpg&q=100" /> 
    <br />
    <img alt="" src="Thumb.ashx?src=~/images/castle1.jpg&q=10" /> 
    <br />
    <img alt="" src="Thumb.ashx?src=~/images/castle1.jpg&w=200" /> 
    <br />
    <img alt="" src="Thumb.ashx?src=~/images/castle1.jpg&h=150" /> 
    <br />
    <img alt="" src="Thumb.ashx?src=~/images/castle1.jpg&h=180&w=120" /> 
    <br />
    <img alt="" src="Thumb.ashx?src=~/images/castle1.jpg&h=80&w=210" /> 
    <br />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have or know where I can get a version of the ASP.NET
Does anyone know of an ASP.NET guide to implementing OpenID and what information can
Does anyone know how to pass a C# ASP.NET array to a JavaScript array?
Does anyone know of a CSS Adapter for the LinkButton control for ASP.Net 2?
Does anyone know if this has been released yet? I went to asp.net and
I know that in the next version of ASP.NET we'll finally be able to
I need the actual debug symbols from the released version of ASP.NET MVC 2
Does anyone know of a good means to manage ASP.net membership parameters and accounts
Does anyone know how to redirect current request in ASP.NET using http status code
I have an asp.net site that is using version 1.1 of the .NET framework.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.