我将dll放在bin文件夹中,但仍然不执行任何操作?

时间:2018-12-14 12:09:57

标签: asp.net dll

我已经在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文件夹中,并设置了Web配置。

<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">

但是什么也不做。 我有IIS10,我尝试在Handler Mapping中添加一个脚本映射,但仍然不执行任何操作。

怎么了? 谢谢

0 个答案:

没有答案