从aspx页面返回图像失败

时间:2014-01-25 06:45:51

标签: c# jquery asp.net image-processing

我必须从我的服务器返回大约46张图片。除了aspx页面渲染旋转图像之外,网页还会进行几次jQuery调用。以下是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;
    }

我面临的问题是我得到一个奇怪的“无效参数”异常。我确保文件都正确存在。

有人可以帮忙吗?在控制台应用程序中使用相同的代码(控制台应用程序仅转换图像)

1 个答案:

答案 0 :(得分:0)

我认为这里的问题是你在Page_Load方法中执行操作。

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

如果您打算发送图像,请在编写图像后从方法返回,而不调用base.ProcessRequest()。

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