返回变量的错误为Image

时间:2016-11-17 12:13:32

标签: c# html asp.net

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

但我在返回bmPhoto Page_Load(object, System.EventArgs) is null

时收到错误
  

注意

我知道这段代码错了,但我想了解如何在缓冲区中执行相同的功能。

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
    Response.Cache.VaryByParams["Image;Width;Height;needToFill"] = true;
    Response.ContentType = "image/jpeg";
    string imageLocation = Server.MapPath(Request.QueryString["Image"]);
    int Width = Convert.ToInt32(Request.QueryString["Width"]);
    int Height = Convert.ToInt32(Request.QueryString["Height"]);
    System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageLocation);
    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;
    bool needToFill=true;
    nScaleW = ((double)Width / (double)sourceWidth);
    nScaleH = ((double)Height / (double)sourceHeight);

    if (Request.QueryString["needToFill"] != null) {
        needToFill = Convert.ToBoolean(Request.QueryString["needToFill"]);
    }

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

2 个答案:

答案 0 :(得分:2)

您无法从void方法返回值:

void Page_Load(Object sender, EventArgs e) 

你只需要写回应。删除return语句并将方法更改为:

void Page_Load(Object sender, EventArgs e) 
{
    //...
    var bytes= (byte[])new ImageConverter().ConvertTo(bmPhoto, typeof(byte[]));
    Response.ContentType = "image/bmp";
    Response.OutputStream.Write(bytes, 0, bytes.Length);
}

答案 1 :(得分:1)

在我看来,您试图从返回Bitmap的事件处理程序返回void对象。即。

void Page_Load(Object sender, EventArgs e) {

    //...

    using (System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(bmPhoto)) {

        //...

        return bmPhoto; // <-- HERE
    }
}

要解决此问题,如果要将其用于以后的用户,我会将Bitmap对象设置在某个变量中。如果你不再需要它,就不要从方法中返回任何东西。

正如@Juergen Gutsch评论的那样,您可以尝试将图像写入响应流。