asp.net版本的timthumb php类
发布时间:2020-12-16 03:57:31 所属栏目:asp.Net 来源:网络整理
导读:有人知道着名的 PHP类“timthumb”的ASP.Net版本吗?只需要一个可以在同一行“timthumb”中工作的脚本,并为任何大小的图像生成高质量的基于方形或比率的缩略图. 这是php类的链接:http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/ 解决
有人知道着名的
PHP类“timthumb”的ASP.Net版本吗?只需要一个可以在同一行“timthumb”中工作的脚本,并为任何大小的图像生成高质量的基于方形或比率的缩略图.
这是php类的链接:http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/ 解决方法
我为你编写了这个:) :)我通过尝试
http://www.binarymoon.co.uk/demo/timthumb-basic/中的所有案例并将其与我的代码并排进行比较来测试它.据我所知,它基本相同.据说它不支持’zc’标志.
我把它写成通用处理程序(ashx文件),以便它运行得更快.支持成像缓存,但我对其进行了评论,因为它使测试修复非常困难.随意取消注释,以便长期提高性能(开头一个块,一个块写入内存流时接近结束). – -码 – – <%@ 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,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 – -测试用例 – – <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 /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET 使用System.Drawing 绘制随机验证码
- asp.net-mvc – 如何在ASP.NET MVC的一个视图中使用多个模型
- asp.net-mvc – ActionLink jQuery参数
- asp.net – System.Web.Security.FormsAuthentication.Encr
- asp.net-mvc – 获取文件内容的绝对路径
- asp.net-mvc – AutoMapper xUnit:缺少类型映射配置或不支
- asp.net – SignalR 2.0超时连接
- asp.net-mvc – F#Asp.Net CodeDom ProviderOptions问题
- asp.net-mvc – 从MVC控制器调用异步方法
- 在IIS7上运行ASP.NET 1.1的错误(applicationHost.config)
推荐文章
站长推荐
- ASP.NET MVC 3 – 处理多个域并共享单个代码库
- asp.net-mvc-5 – MVC5身份验证过滤器
- asp.net – 没有Eval的GridGroupHeaderItem.Aggr
- asp.net-mvc – 将一个属性与另一个属性进行比较
- 更改ASP.NET网站管理工具连接字符串
- asp.net – 如何增加Web服务请求的超时?
- asp.net – 集合的Asp MVC3自定义EditorTemplate
- asp.net – Oracle.ManagedDataAccess:TNS:无法
- 将ASP.NET MVC教给ASP.NET程序员的最佳学习资源是
- asp.net-mvc – 在ASP.Net MVC 2中为非归因模型验
热点阅读