【问题标题】:What can I do to get images resized by passing a width parameter in the request?如何通过在请求中传递宽度参数来调整图像大小?
【发布时间】:2019-05-10 17:44:35
【问题描述】:

我想要我的压缩图像,但我不想在上传时压缩它们,也不想使用 css 或类似的东西调整宽度和高度。 我想调用这样的图像:

<img src="/images/exaple.png?width200px">

我的 2000px x 1000px 图像在我的页面上将是 200px x 100px。

我只找到了您必须付费购买的程序,而且,由于我在 Windows 上工作,因此我没有 Linux 服务器来安装此程序。

【问题讨论】:

    标签: jquery html asp.net image compression


    【解决方案1】:

    首先我将宽度作为变量添加=

    <img src="/images/exaple.png?width=200px">
    

    然后你需要使用asp.net来处理图片的静态文件。
    除非您在 web.config 上的处理程序上声明它,否则静态文件(如图像)不会从 asp.net 传递。

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

    最后你需要创建处理程序

    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;
        }
        }
    }
    

    参考:HTTP Handlers for Images in ASP.NET

    其他例子: https://www.c-sharpcorner.com/UploadFile/dacca2/working-with-image-in-httphandler/

    http://www.wrox.com/WileyCDA/Section/Two-ASP-NET-HttpHandler-Image-Hacks.id-291916.html

    【讨论】:

    • 非常感谢您回答我!我不知道如何创建一个处理程序,我从来没有做过,我在谷歌上寻找那个。我必须保存这个文件.cs?我必须把它放在哪里?谢谢
    • @MirkoBaldassini 您可以创建一个与您的站点连接的不同 dll 库(稍后将 dll 放在 bin 上),但您也可以将其添加到 App_code
    • 我已经下载了Visual Studio 2017,我已经把你的代码,但它有很多错误,可能是因为我没有配置真正的员工,我尝试过其他人但一样,我对dll,但我还是没有,疯了
    • @MirkoBaldassini 从这里获取代码:jigar.net/viewcontent.ashx?contentid=71
    • 我也遇到了这个问题,因为它对我说“csc”是未知的。无论如何我纠正了 Visual Studio 上的错误,我只是在 System.Web 和绘图中添加了一些参考,最后我有了我的编译文件。我把它放在我创建的文件夹 bin 中,并像你对我说的那样上传了 web.config。我做了一个简单的页面 html,只有一个带有变量的 img,就像你告诉我的那样,但它什么也没做。图像是 3000 像素而不是 200 像素的宽度,就像我调用的那样。怎么了?非常感谢
    猜你喜欢
    • 2011-06-13
    • 2013-07-29
    • 2011-12-25
    • 2020-02-14
    • 2013-04-28
    • 2014-07-15
    • 2016-04-03
    • 2016-06-01
    • 1970-01-01
    相关资源
    最近更新 更多