2014-01-25 53 views
1

我必须从我的服务器返回约46图像。除了aspx页面渲染旋转图像外,网页还会创建几个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; 
    } 

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

有人可以请帮助?相同的代码在控制台应用程序中使用时工作正常(控制台应用程序仅转换图像)

+0

您所描述的“无效参数”异常位于何处? – cubitouch

+0

你在什么时候得到这个例外?你能通过代码,并发布异常细节和导致它的线?从我所看到的代码看起来很明智。 – sh1rts

+0

它出现在这里b = RotateImage(b,RotationAngle,true); – user1144596

回答

0

我假设问题在于您在Page_Load方法中执行操作。

覆盖ProcessRequest方法,请检查您的查询字符串参数,然后调用你当前的代码。

如果你的意思是发送图像,从方法,而无需编写图像之后调用base.ProcessRequest()返回。

不要忘记纪念这个作为回答,如果它工作。

+0

事实证明,问题是如果旋转角度被指定为超过180。使它小于180,它工作正常 – user1144596