Обновление JQuery IMG SRC через Response.OutputStream - PullRequest
1 голос
/ 26 января 2011

Я пытаюсь обновить источник изображения с измененной шириной / высотой изображения на основе текущей ширины / высоты браузера.Все отлично работает локально, но изображения не обновляются за пределами моей местной среды.

Любая помощь / советы будут высоко оценены!

Изображение

<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 Ответ

2 голосов
/ 26 февраля 2011

Я не эксперт, но я думаю, что ваша первая строка кода JS должна сказать:

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

Обратите внимание на точку (.) Перед «изображением».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...