通过Response.OutputStream更新Jquery img src

时间:2011-01-26 17:39:14

标签: asp.net jquery image

我要做的是根据当前浏览器的宽度/高度更新图像宽度/高度的图像源。一切都在当地很好用,但图像不会在我当地的环境之外更新。

非常感谢任何帮助/提示!

图像

<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);
                    }
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:2)

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

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

注意“image”之前的点(。)。