C# 从aspx页面返回图像失败

C# 从aspx页面返回图像失败,c#,jquery,asp.net,image-processing,C#,Jquery,Asp.net,Image Processing,我必须从我的服务器返回大约46个图像。网页进行了几个jQuery调用,此外,aspx页面还呈现旋转的图像。以下是aspx页面代码 string CaseID = Request.QueryString["CaseID"].ToString(); string ImageID = Request.QueryString["ImageID"].ToString(); int RotationAngle = Convert.ToInt32(Request.QuerySt

我必须从我的服务器返回大约46个图像。网页进行了几个jQuery调用,此外,aspx页面还呈现旋转的图像。以下是aspx页面代码

string CaseID = Request.QueryString["CaseID"].ToString();
        string ImageID = Request.QueryString["ImageID"].ToString();
        int RotationAngle = Convert.ToInt32(Request.QueryString["RotationAngle"].ToString());
        string ImagePath = Request.QueryString["ImagePath"].ToString();

        string fileName = string.Empty;
        FileInfo fi;

        if (String.IsNullOrEmpty(CaseID) || String.IsNullOrEmpty(ImageID))
        {
            fileName = "genetics.png";
            fi = new FileInfo(Server.MapPath("~/images/") + fileName);

        }
        else
        {
            fileName = ImagePath;
            fi = new FileInfo(Server.MapPath("~/" + fileName));
        }

        MemoryStream ms = new MemoryStream();

        try
        {
            if (fi.Exists)
            {
                Response.ContentType = "image/" + fi.Extension;

               Bitmap b = new Bitmap(fi.FullName);
            //   Bitmap image = RotateImage(b, RotationAngle, true);
               b = RotateImage(b, RotationAngle, true);

                using (ms = new MemoryStream())
                {
                    switch (fi.Extension.ToLower())
                    {
                        case "jpg":
                            b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;
                        case "jpeg":
                            b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                            break;
                        case "png":
                            b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            break;
                    }
                   b.Dispose();
               //    b.Dispose();
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
        finally
        {
            ms.WriteTo(Response.OutputStream);
            Response.Flush();
        }
图像旋转代码如下所示:

  public static Bitmap RotateImage(System.Drawing.Image image, float angle)
    {
        // center of the image
        float rotateAtX = image.Width / 2;
        float rotateAtY = image.Height / 2;
        bool bNoClip = false;
        return RotateImage(image, rotateAtX, rotateAtY, angle, bNoClip);
    }

    public static Bitmap RotateImage(System.Drawing.Image image, float angle, bool bNoClip)
    {
        // center of the image
        float rotateAtX = image.Width / 2;
        float rotateAtY = image.Height / 2;
        return RotateImage(image, rotateAtX, rotateAtY, angle, bNoClip);
    }

    public static Bitmap RotateImage(System.Drawing.Image image, float rotateAtX, float rotateAtY, float angle, bool bNoClip)
    {
        int W, H, X, Y;
        if (bNoClip)
        {
            double dW = (double)image.Width;
            double dH = (double)image.Height;

            double degrees = Math.Abs(angle);
            if (degrees <= 90)
            {
                double radians = 0.0174532925 * degrees;
                double dSin = Math.Sin(radians);
                double dCos = Math.Cos(radians);
                W = (int)(dH * dSin + dW * dCos);
                H = (int)(dW * dSin + dH * dCos);
                X = (W - image.Width) / 2;
                Y = (H - image.Height) / 2;
            }
            else
            {
                degrees -= 90;
                double radians = 0.0174532925 * degrees;
                double dSin = Math.Sin(radians);
                double dCos = Math.Cos(radians);
                W = (int)(dW * dSin + dH * dCos);
                H = (int)(dH * dSin + dW * dCos);
                X = (W - image.Width) / 2;
                Y = (H - image.Height) / 2;
            }
        }
        else
        {
            W = image.Width;
            H = image.Height;
            X = 0;
            Y = 0;
        }

        //create a new empty bitmap to hold rotated image
        Bitmap bmpRet = new Bitmap(W, H);
        bmpRet.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        //make a graphics object from the empty bitmap
        Graphics g = Graphics.FromImage(bmpRet);

        //Put the rotation point in the "center" of the image
        g.TranslateTransform(rotateAtX + X, rotateAtY + Y);

        //rotate the image
        g.RotateTransform(angle);

        //move the image back
        g.TranslateTransform(-rotateAtX - X, -rotateAtY - Y);

        //draw passed in image onto graphics object
        g.DrawImage(image, new PointF(0 + X, 0 + Y));

        return bmpRet;
    }
我面临的问题是,我得到一个奇怪的无效参数异常。我已确保所有文件都正确存在


有人能帮忙吗?同样的代码在控制台应用程序中使用时也可以正常工作。控制台应用程序仅转换图像。我假设这里的问题是您正在使用Page_Load方法执行操作

重写ProcessRequest方法,检查查询字符串参数,然后调用当前代码

如果要发送图像,请在写入图像后从方法返回,而不调用base.ProcessRequest


如果有效,请不要忘记将其标记为答案。

您描述的无效参数异常位于何处?在什么时候会出现此异常?你能一步一步地检查代码,并发布异常详细信息和导致异常的行吗?从我看来,你的代码看起来很合理;我想知道它是否和我现在指定的旋转角度有关,旋转角度可以是0到3600之间的任何值。事实证明,问题是旋转角度是否被指定为大于180。使其小于180,工作正常