php类的asp.net版本

php类的asp.net版本,php,asp.net,thumbnails,Php,Asp.net,Thumbnails,有人知道著名的PHP类“timthumb”的ASP.Net版本吗?只需要一个脚本,将工作在同一行的“timthumb”,并产生正方形或比例为基础的缩略图与任何大小的图像质量良好 下面是指向php类的链接:看起来您希望执行以下操作: 最糟糕的情况是,timthumb是开源的,所以你可以在.NET中创建它,并将其开源给全世界!(当然,要正确地归因于原文)我写这篇文章只是为了你:)。。。我通过在中尝试所有案例并将其与代码并排进行可视化比较来测试它。据我所知,基本上是一样的。尽管如此,它不支持“zc”

有人知道著名的PHP类“timthumb”的ASP.Net版本吗?只需要一个脚本,将工作在同一行的“timthumb”,并产生正方形或比例为基础的缩略图与任何大小的图像质量良好


下面是指向php类的链接:

看起来您希望执行以下操作:


最糟糕的情况是,timthumb是开源的,所以你可以在.NET中创建它,并将其开源给全世界!(当然,要正确地归因于原文)

我写这篇文章只是为了你:)。。。我通过在中尝试所有案例并将其与代码并排进行可视化比较来测试它。据我所知,基本上是一样的。尽管如此,它不支持“zc”标志

我将其作为通用处理程序(ashx文件)编写,以便它运行得更快。图像缓存是受支持的,但我对其进行了注释,因为它使得测试修复非常困难。从长远来看,可以随意取消注释它,以提高性能(当它将其写入内存流时,一个块正好位于开始处,另一个块接近结束处)

---代码---


导入系统
导入系统.Web
导入系统。绘图
导入System.IO
公共类Thumb:实现IHttpHandler
私有共享\u默认宽度为整数=100
私有共享\u默认高度为整数=100
私有共享\u默认质量为整数=90
Public Sub-ProcessRequest(ByVal上下文作为HttpContext)实现IHttpHandler.ProcessRequest
''首先检查缓存中的图像
'Dim cacheObj As Object=context.Cache(“Thumb-”+context.Request.Url.ToString().ToLower())
“如果cacheObj不是什么,那么
'Dim msCache As MemoryStream=DirectCast(cacheObj,MemoryStream)
'WriteImage(msCache,上下文)
“出口接头
"完"
'处理请求(因为它不在缓存中)
Dim imgPath As String=context.Request.QueryString(“src”)
如果String.IsNullOrEmpty(imgPath),则
context.Response.End()
如果结束
'获取服务器上的映像路径
Dim serverPath作为字符串=context.Server.MapPath(imgPath)
如果文件不存在(serverPath),则
context.Response.End()
如果结束
'从文件路径加载图像
将img设置为Image=Bitmap.FromFile(服务器路径)
双精度尺寸公差=(数学最小值(img.Width,img.Height)/数学最大值(img.Width,img.Height))
“---计算缩略图大小---
宽度为整数=0
将高度设置为整数=0
Dim destRatio为双精度=0
Dim qW As String=context.Request.QueryString(“w”)
Dim qH As String=context.Request.QueryString(“h”)
如果不是String.IsNullOrEmpty(qW),则
Int32.TryParse(qW,宽度)
如果宽度小于0,则
宽度=0
如果结束
如果结束
如果不是String.IsNullOrEmpty(qH),则
Int32.TryParse(qH,高度)
如果高度<0,则
高度=0
如果结束
如果结束
'如果宽度和高度均为0,则使用默认值(100x100)
如果destWidth=0,destHeight=0,则
destWidth=\u DefaultWidth
destHeight=\u默认高度
'否则,如果指定了宽度,请计算适当的高度
否则,如果destWidth>0,destHeight>0,则
“什么都不要做,我们已经有两种尺码了
如果destWidth>0,则
destHeight=数学地板(img.Height*(destWidth/img.Width))
如果destHeight>0,则
destWidth=数学地板(img.Width*(destHeight/img.Height))
如果结束
destatio=(Math.Min(destWidth,destHeight)/Math.Max(destWidth,destHeight))
'计算源图像大小(矩形)以从中获取像素数据
Dim sourceWidth为整数=img.Width
Dim sourceHeight作为整数=img.Height
Dim sourceX为整数=0
Dim sourceY作为整数=0
Dim cmpx为整数=img.Width/destWidth
Dim cmpy作为整数=img.Height/destHeight
'选择基于最小尺寸
如果cmpx>cmpy,则
sourceWidth=img.Width/cmpx*cmpy
sourceX=((img.Width-(img.Width/cmpx*cmpy))/2)
ElseIf cmpy>cmpx然后
sourceHeight=img.Height/cmpy*cmpx
sourceY=((img.Height-(img.Height/cmpy*cmpx))/2)
如果结束
'---创建新的缩略图图像---
将bmpThumb设置为新位图(destWidth、destHeight)
Dim g=图形。来自图像(bmpThumb)
g、 插值模式=Drawing2D.InterpolationMode.HighQualityBicubic
g、 合成质量=绘图2D.CompositingQuality.HighQuality
g、 SmoothingMode=Drawing2D.SmoothingMode.AntiAlias
g、 绘图图像(img_
新矩形(0,0,destWidth,destHeight)_
新矩形(sourceX、sourceY、sourceWidth、sourceHeight)、GraphicsUnit.Pixel)
'----将缩略图写入输出流----
'获取jpeg图像编码信息,以便保存时使用
将ici设置为Imaging.ImageCodecInfo=Imaging.ImageCodecInfo.GetImageEncoders()。其中(函数(c)c.MimeType=“image/jpeg”).First()
'将图像保存到内存流
将ms变暗为新内存流()
bmpThumb.Save(ms、ici、BuildQualityParams(上下文))
''将图像保存到缓存以备将来使用
'context.Cache(“Thumb-”+context.Request.Url.ToString().ToLower())=ms
“把图像写出来
<%@ 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
<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 />
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace myProject.Handlers
{
    public class Thumb : IHttpHandler
    {
        private static int _DefaultWidth = 100;
        private static int _DefaultHeight = 100;
        private static int _DefaultQuality = 90;

        public void ProcessRequest(HttpContext context)
        {
            string imgPath = context.Request.QueryString["src"];

            if (string.IsNullOrEmpty(imgPath))
            {
                context.Response.End();
            }

            string serverPath = context.Server.MapPath(imgPath);
            if (!File.Exists(serverPath))
            {
                context.Response.End();
            }

            Image img = Bitmap.FromFile(serverPath);
            double origRatio = (Math.Min(img.Width, img.Height) / Math.Max(img.Width, img.Height));

            int destWidth = 0;
            int destHeight = 0;
            double destRatio = 0;
            double dHeight = 0;
            double dWidth = 0;

            string qW = context.Request.QueryString["w"];
            string qH = context.Request.QueryString["h"];

            if (!string.IsNullOrEmpty(qW))
            {
                Int32.TryParse(qW, out destWidth);
                if (destWidth < 0)
                {
                    destWidth = 0;
                }
            }

            if (!string.IsNullOrEmpty(qH))
            {
                Int32.TryParse(qH, out destHeight);
                if (destHeight < 0)
                {
                    destHeight = 0;
                }
            }

            if (destWidth == 0 & destHeight == 0)
            {
                destWidth = _DefaultWidth;
                destHeight = _DefaultHeight;
            }
            else if (destWidth > 0 & destHeight > 0)
            {
                //do nothing, we have both sizes already
            }
            else if (destWidth > 0)
            {
                dHeight = double.Parse(destWidth.ToString()) / double.Parse(img.Width.ToString());
                destHeight = int.Parse(Math.Floor(img.Height * dHeight).ToString());
            }
            else if (destHeight > 0)
            {
                dWidth = double.Parse(destHeight.ToString()) / double.Parse(img.Height.ToString());
                destWidth = int.Parse(Math.Floor(img.Width * dWidth).ToString());
            }

            destRatio = (Math.Min(destWidth, destHeight) / Math.Max(destWidth, destHeight));

            int sourceWidth = img.Width;
            int sourceHeight = img.Height;

            int sourceX = 0;
            int sourceY = 0;

            int cmpx = img.Width / destWidth;
            int cmpy = img.Height / destHeight;

            if (cmpx > cmpy)
            {
                sourceWidth = img.Width / cmpx * cmpy;
                sourceX = ((img.Width - (img.Width / cmpx * cmpy)) / 2);
            }
            else if (cmpy > cmpx)
            {
                sourceHeight = img.Height / cmpy * cmpx;
                sourceY = ((img.Height - (img.Height / cmpy * cmpx)) / 2);
            }

            Bitmap bmpThumb = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage(bmpThumb);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            g.DrawImage(img, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

            ImageCodecInfo ici = ImageCodecInfo.GetImageEncoders().Where(c => c.MimeType == "image/jpeg").First();

            MemoryStream ms = new MemoryStream();
            bmpThumb.Save(ms, ici, BuildQualityParams(context));

            WriteImage(ms, context);
        }

        private void WriteImage(MemoryStream ms, HttpContext context)
        {
            context.Response.Clear();

            context.Response.ContentType = "image/jpeg";

            ms.WriteTo(context.Response.OutputStream);

            context.Response.Flush();
            context.Response.End();
        }

        private EncoderParameters BuildQualityParams(HttpContext context)
        {
            EncoderParameters epParams = new EncoderParameters(1);

            int q = _DefaultQuality;

            string strQ = context.Request.QueryString["q"];
            if (!string.IsNullOrEmpty(strQ))
            {
                Int32.TryParse(strQ, out q);
            }

            epParams.Param[0] = new EncoderParameter(Encoder.Quality, q);

            return epParams;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}