2011-01-26 75 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() ... 

注意「圖像」之前的點(。)。