繁体   English   中英

如何从返回ContentType图片的网址执行aspx?

[英]How can I execute an aspx from url returning an ContentType Image?

此函数返回调整大小并居中的图像。 想像执行thumb.aspx?image = test.jpg&width = 100&height = 50&needToFill = tru‌e一样执行它,以获取ContentType =“ image / jpeg”

public static System.Drawing.Image FixedSize(Image image, int Width, int Height, bool needToFill)
{
    int sourceWidth = image.Width;
    int sourceHeight = image.Height;
    int sourceX = 0;
    int sourceY = 0;
    double destX = 0;
    double destY = 0;
    double nScale = 0;
    double nScaleW = 0;
    double nScaleH = 0;
    nScaleW = ((double)Width / (double)sourceWidth);
    nScaleH = ((double)Height / (double)sourceHeight);
    if (!needToFill)
    {
        nScale = Math.Min(nScaleH, nScaleW);
    }
    else
    {
        nScale = Math.Max(nScaleH, nScaleW);
        destY = (Height - sourceHeight * nScale) / 2;
        destX = (Width - sourceWidth * nScale) / 2;
    }
    if (nScale > 1)
        nScale = 1;
    int destWidth = (int)Math.Round(sourceWidth * nScale);
    int destHeight = (int)Math.Round(sourceHeight * nScale);

    System.Drawing.Bitmap bmPhoto = null;
    try
    {
        bmPhoto = new System.Drawing.Bitmap(destWidth + (int)Math.Round(2 * destX), destHeight + (int)Math.Round(2 * destY));
    }
    catch (Exception ex)
    {
        throw new ApplicationException(string.Format("destWidth:{0}, destX:{1}, destHeight:{2}, desxtY:{3}, Width:{4}, Height:{5}",
            destWidth, destX, destHeight, destY, Width, Height), ex);
    }
    using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto))
    {
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;
        grPhoto.SmoothingMode = SmoothingMode.HighQuality;
        Rectangle to =  new System.Drawing.Rectangle((int)Math.Round(destX), (int)Math.Round(destY), destWidth, destHeight);
        Rectangle from = new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
        grPhoto.DrawImage(image, to, from, System.Drawing.GraphicsUnit.Pixel);
        return bmPhoto;
    }
}

我可以以某种方式添加它吗?

void Page_Load(Object sender, EventArgs e){

您可以使用HTTP处理程序来满足此类要求。 ASP.NET HTTP处理程序是响应对ASP.NET Web应用程序的请求而运行的过程(通常称为“端点”)。 要了解更多信息,请查看HTTP处理程序和HTTP模块概述

ASHX示例

要像这样处理请求: http://localhost:19610/ImageHandler.ashx?width=200&height=200

  1. 将一个新的通用处理程序(.ashx)添加到项目并将其命名为ImageHandler.ashx
  2. 编写代码以从查询字符串获取参数并执行处理并返回适当的响应:
using System;
using System.Collections.Generic;
using System.Web;
using System.Drawing;

namespace WebApplication1 /*use your application namespace*/
{
    public class ImageHandler: IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            int width = 0;
            int.TryParse(context.Request.QueryString["width"], out width);
            var height = 0;
            int.TryParse(context.Request.QueryString["height"], out height);
            if (width <= 0) width = 100;
            if (height <= 0) height = 100;
            using (var image = new Bitmap(width, height))
            {
                using (var g = Graphics.FromImage(image))
                    g.Clear(Color.Red);
                byte[] buffer = 
                    (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
                context.Response.ContentType = "image/bmp";
                context.Response.OutputStream.Write(buffer, 0, buffer.Length);
            }
        }
        public bool IsReusable { get { return false; } }
    }
}

ASPX示例

如果出于任何原因要改用aspx ,则可以创建一个aspx文件,而无需在文件后面添加任何代码,如下所示:

<%@ Page Title="Home Page" Language="C#" %>
<script language="C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        int width = 0;
        int.TryParse(Request.QueryString["width"], out width);
        var height = 0;
        int.TryParse(Request.QueryString["height"], out height);
        if (width <= 0) width = 100;
        if (height <= 0) height = 100;
        using (var image = new System.Drawing.Bitmap(width, height))
        {
            using (var g = System.Drawing.Graphics.FromImage(image))
                g.Clear(System.Drawing.Color.Red);
            byte[] buffer =
            (byte[])new System.Drawing.ImageConverter().ConvertTo(image, typeof(byte[]));
            Response.ContentType = "image/bmp";
            Response.OutputStream.Write(buffer, 0, buffer.Length);
        }
    }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM