我已经在dll中编译了C#文件。
using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace ImageHandler{
public class HttpImageHandler: IHttpHandler{
int Width = 0;
int Height = 0;
public void ProcessRequest(System.Web.HttpContext context)
{
if(context.Request.Params["height"] != null){
try{
Height = int.Parse(context.Request.Params["height"]);
}catch{
Height = 0;
}
}
if(context.Request.Params["width"] != null){
try{
Width = int.Parse(context.Request.Params["width"]);
}catch{
Width = 0;
}
}
if (Width <= 0 && Height <=0)
{
context.Response.Clear();
context.Response.ContentType =
getContentType(context.Request.PhysicalPath);
context.Response.WriteFile(context.Request.PhysicalPath);
context.Response.End();
}
else
{
context.Response.Clear();
context.Response.ContentType =
getContentType(context.Request.PhysicalPath);
byte[] buffer =
getResizedImage(context.Request.PhysicalPath,Width,Height);
context.Response.OutputStream.Write
(buffer, 0, buffer.Length);
context.Response.End();
}
}
public bool IsReusable{
get{
return false;
}
}
byte[] getResizedImage(String path ,int width,int height)
{
Bitmap imgIn = new Bitmap(path);
double y = imgIn.Height;
double x = imgIn.Width;
double factor = 1;
if(width > 0){
factor = width/x;
}else if (height>0){
factor = height/y;
}
System.IO.MemoryStream outStream =
new System.IO.MemoryStream();
Bitmap imgOut =
new Bitmap((int)(x * factor),(int)(y * factor));
Graphics g = Graphics.FromImage(imgOut);
g.Clear(Color.White);
g.DrawImage(imgIn,new Rectangle(0,0,(int)(factor * x),
(int)(factor * y)),
new Rectangle(0,0,(int)x,(int)y),GraphicsUnit.Pixel);
imgOut.Save(outStream, getImageFormat(path));
return outStream.ToArray();
}
string getContentType(String path){
switch (Path.GetExtension(path)) {
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default : break;
}
return "";
}
ImageFormat getImageFormat(String path){
switch (Path.GetExtension(path)) {
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default : break;
}
return ImageFormat.Jpeg;
}
}
}
然后,我将ImageHandler.dll放入了bin文件夹。 我已经设置了网络配置。
<configuration>
<system.web>
<customErrors mode="Off" />
<compilation debug="true" />
<httpHandlers>
<add verb="*" path="*.bmp" type="ImageHandler.HttpImageHandler,ImageHandler"/>
<add verb="*" path="*.jpg" type="ImageHandler.HttpImageHandler,ImageHandler"/>
<add verb="*" path="*.gif" type="ImageHandler.HttpImageHandler,ImageHandler"/>
<add verb="*" path="*.png" type="ImageHandler.HttpImageHandler,ImageHandler"/>
</httpHandlers>
</system.web>
最后,我尝试用压缩后的新尺寸调用图片。
<img src="/images/exaple.png?width=200px">
但是什么也不做。 我在Google上找到了一个正在谈论要放入web.config并在添加/编辑应用程序扩展名映射窗口中的IIS中添加手册的家伙,他说,但是,我有IIS10,它与众不同,我试图在“处理程序映射”中添加脚本映射,但仍然不执行任何操作。
怎么了? 非常感谢