2011-01-26 76 views
1

我试图做的是根据当前浏览器的宽度/高度更新图像大小的宽度/高度的图像源。一切工作本地很好,但图像不会更新我的本地环境之外。Jquery img src通过Response.OutputStream更新

任何帮助/提示将不胜感激!

图片

<img class='image' src='./ImageResize.aspx?image=http://img.dailymail.co.uk/i/pix/2008/04_02/alligatorL_468x343.jpg' /> 

jQuery的SRC操作

//Adds resize image demensions 

    $('image').each(function() { 

     var sSource = $(this).attr('src'); 
     sSource = sSource + "&width=" + $(window).width + "&height=" + 
        $(window).height(); 
     $(this).attr('src', sSource); 
    }); 

Asp.net代码背后

protected void ImageWork() 
{ 
    var sPath = ""; 
    var sWidth = 0; 
    var sHeight = 0; 

    if (Request["image"] != null) 
    { 
     sPath = Request["image"].ToString(); 
    } 
    if (Request["width"] != null & Request["height"] != null) 
    { 
     sWidth = Convert.ToInt32(Request["width"]); 
     sHeight = Convert.ToInt32(Request["height"]); 
    } 

    if (!string.IsNullOrEmpty(sPath) & (sWidth > 0) & (sHeight > 0)) 
    { 

     if (sPath.Contains("http")) 
     { 

      MemoryStream xPath; 
      WebClient wc = new WebClient(); 
      byte[] originalData = wc.DownloadData(sPath); 

      xPath = new MemoryStream(originalData); 

      using (Bitmap image = new Bitmap(xPath)) 
      { 
       int xWidth = image.Width; 
       int xHeight = image.Height; 

       if ((xWidth < sWidth) & (xHeight < sHeight)) 
       { 
        Response.ContentType = "image/Jpeg"; 
        image.Save(Response.OutputStream, ImageFormat.Jpeg); 
       } 
       else 
       { 
        xWidth = (int)Math.Floor(((double)image.Width * ((double)sWidth/(double)image.Width))); 
        xHeight = (int)Math.Floor((double)image.Height * ((double)sHeight/(double)image.Height)); 

        using (Bitmap newImage = new Bitmap(image, xWidth, xHeight)) 
        { 
         Response.ContentType = "image/Jpeg"; 
         newImage.Save(Response.OutputStream, ImageFormat.Jpeg); 
        } 
       } 
      } 
     } 
     else 
     { 
      var xPath = sPath; 
      using (Bitmap image = new Bitmap(xPath)) 
      { 
       int xWidth = image.Width; 
       int xHeight = image.Height; 

       if ((xWidth < sWidth) & (xHeight < sHeight)) 
       { 
        Response.ContentType = "image/Jpeg"; 
        image.Save(Response.OutputStream, ImageFormat.Jpeg); 
       } 
       else 
       { 
        xWidth = (int)Math.Floor(((double)image.Width * ((double)sWidth/(double)image.Width))); 
        xHeight = (int)Math.Floor((double)image.Height * ((double)sHeight/(double)image.Height)); 

        using (Bitmap newImage = new Bitmap(image, xWidth, xHeight)) 
        { 
         Response.ContentType = "image/Jpeg"; 
         newImage.Save(Response.OutputStream, ImageFormat.Jpeg); 
        } 
       } 
      } 
     } 

    } 
} 
+0

您是否在浏览器中看到错误? – TheGeekYouNeed 2011-01-26 18:09:57

回答

2

我不是专家,但我觉得你的JS代码第一行应该说:

$('.image').each(function() ... 

注意“图像”之前的点(。)。