Images not getting rendered when using httphandler

Discussion in 'ASP .Net' started by =?Utf-8?B?SGl0ZXNo?=, May 12, 2005.

  1. Hi,

    I am getting the response from another Website by using the HttpHandler in
    my current site. I am getting the page but all the images on that page are
    not appearing only placeholder are displayed.

    Can anybody know this issue and help me to resolve this.

    In past i received the response saying that i should download the image
    first and then parse the actual response and modify the src attribute of the
    img tag and then rendered the output on the browser. But i could not able to
    find a way to do this, if this approach is correct then if somebody can
    provide me a code snippet then it will be really helpful.

    Thanks
    Hitesh
     
    =?Utf-8?B?SGl0ZXNo?=, May 12, 2005
    #1
    1. Advertising

  2. Hitesh,

    Have a look at this article from aspalliance
    http://aspalliance.com/articleViewer.aspx?aId=141

    that example uses an aspx page to output the image.
    once you have that working and you would like to optimise it then put the
    code into http handler
    This link should help with handler (custom) and image manipulation cod
    http://www.microsoft.com/belux/nl/msdn/community/columns/desmet/httphandler.mspx
    I have used the code similar to the one in above link from ms but i just
    created a generic handler .ashx rather than a custom handler.

    HTH

    Regards,

    Hermit Dave
    http://hdave.blogspot.com



    "Hitesh" wrote:

    > Hi,
    >
    > I am getting the response from another Website by using the HttpHandler in
    > my current site. I am getting the page but all the images on that page are
    > not appearing only placeholder are displayed.
    >
    > Can anybody know this issue and help me to resolve this.
    >
    > In past i received the response saying that i should download the image
    > first and then parse the actual response and modify the src attribute of the
    > img tag and then rendered the output on the browser. But i could not able to
    > find a way to do this, if this approach is correct then if somebody can
    > provide me a code snippet then it will be really helpful.
    >
    > Thanks
    > Hitesh
     
    =?Utf-8?B?SGVybWl0IERhdmU=?=, May 12, 2005
    #2
    1. Advertising

  3. Hi Hermit,

    Thanks for your useful reply, i will try to use that information. I have
    seen the contents of the URL that you have suggested so the first one says
    that. When i am getting the response from the web server where my actual
    pages (They are actually reports) exists in the Handler then first i have to
    parsse that response and replace all the src attributes of the <img> tag to
    look something like this:

    <img src=<%ReadImage.aspx?imgPath='old value of the src attribute'%>

    and then i should write this parsed html response to the browser and now the
    ReadImage.aspx comes into picture and that will read the image from the web
    server which is hidden from the outside world (Reports are here) and render
    on the user's browser but user has in no way access to the images on the
    hidden server.

    Is this understanding is correct ? or i am not on the right track.

    Please if you can take our some of your precious time and give your
    suggestion on the above details that i have given then it will help me to
    start in the right direction.

    Thanks once again for your knowldge sharing.

    "Hermit Dave" wrote:

    > Hitesh,
    >
    > Have a look at this article from aspalliance
    > http://aspalliance.com/articleViewer.aspx?aId=141
    >
    > that example uses an aspx page to output the image.
    > once you have that working and you would like to optimise it then put the
    > code into http handler
    > This link should help with handler (custom) and image manipulation code
    > http://www.microsoft.com/belux/nl/msdn/community/columns/desmet/httphandler.mspx
    > I have used the code similar to the one in above link from ms but i just
    > created a generic handler .ashx rather than a custom handler.
    >
    > HTH
    >
    > Regards,
    >
    > Hermit Dave
    > http://hdave.blogspot.com
    >
    >
    >
    > "Hitesh" wrote:
    >
    > > Hi,
    > >
    > > I am getting the response from another Website by using the HttpHandler in
    > > my current site. I am getting the page but all the images on that page are
    > > not appearing only placeholder are displayed.
    > >
    > > Can anybody know this issue and help me to resolve this.
    > >
    > > In past i received the response saying that i should download the image
    > > first and then parse the actual response and modify the src attribute of the
    > > img tag and then rendered the output on the browser. But i could not able to
    > > find a way to do this, if this approach is correct then if somebody can
    > > provide me a code snippet then it will be really helpful.
    > >
    > > Thanks
    > > Hitesh
     
    =?Utf-8?B?SGl0ZXNo?=, May 13, 2005
    #3
  4. Hitesh,

    I am copying contents of one of the image handlers i have written and how i
    tend to use it.
    What i tend to do is store images in database and then i render them as
    needed. For that purpose i pass the ProductColorID. You can choose to pass
    path or some unique identifier as a part of handlers query string. See the
    way i am calling the handler. I just write the handler url in my aspx page.
    The brower then calls the handler to fetch the image.
    If you are passing image from hard drive then you will have to use
    Image.FromFile();

    so what you can do is pass a unique identified or path and load the image
    from file or. Infact you dont need to anything if you dont want manipulation.
    you can also use
    Response.WriteFile() method. look into that as well...

    HTH

    Hermit
    -------------------------------------------------------------------------------------
    IProductList.aspx
    ------------------------
    <asp:datalist id="dlProducts" runat="server" RepeatDirection="Horizontal"
    ShowFooter="False" HorizontalAlign="Center"
    ShowHeader="False" CellPadding="0">
    <EditItemStyle HorizontalAlign="Center"></EditItemStyle>
    <ItemStyle VerticalAlign="Top"></ItemStyle>
    <ItemTemplate>
    <TABLE cellSpacing="0" cellPadding="1" border="0">
    <TR>
    <TD vAlign="bottom" align="center">
    <asp:HyperLink id="pfProductNav" runat="server" Target=_top
    BorderStyle="None" NavigateUrl='<%# "ProductInfo.aspx?Product=" +
    DataBinder.Eval(Container.DataItem, "ProductID") + "&Color=" +
    DataBinder.Eval(Container.DataItem, "ProductColorID") %>' Height="100px"
    ImageUrl='<%#
    "~/GetImage.ashx?IsDefault=true&showSize=Thumbnail&ProductColorID=" +
    DataBinder.Eval(Container.DataItem, "ProductColorID") %>'>
    </asp:HyperLink>
    </TD>
    </TR>
    </TABLE>
    </ItemTemplate>
    </asp:datalist>

    ---------------------------------------------------------------------

    GetImage.ashx
    ----------------------------------------------------------------------
    <%@ WebHandler Language="C#" Class="ImageGenerator" %>

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Threading;
    using System.Web;
    using System.Web.Caching;
    using StepTallV2.Components;

    public class ImageGenerator : IHttpHandler
    {
    public void ProcessRequest (HttpContext context)
    {
    //
    // Extract input parameters from the query string
    //
    bool bIsDefaultImage = true;
    bool bShowThumbnail = true;
    bool bIsFeatured = false;

    string ProductColorID = (string) context.Request["ProductColorID"];
    string showImageSize = (string)context.Request["showSize"];
    string isDefaultImage = (string) context.Request["isDefaultImage"];

    if(ProductColorID == null || ProductColorID == "")
    {
    DrawImageNotFound(ref context);
    return;
    }

    if(isDefaultImage != null && isDefaultImage == "false")
    bIsDefaultImage = false;

    ImageType imgType = ImageType.Full;

    switch(showImageSize)
    {
    case "UltraSmall":
    imgType = ImageType.UltraSmall;
    break;
    case "Thumbnail":
    imgType = ImageType.Thumbnail;
    break;
    case "Full":
    imgType = ImageType.Full;
    break;
    default:
    imgType = ImageType.Featured;
    break;
    }

    // write the content type
    context.Response.ContentType = "image/jpeg";
    context.Response.Expires = 20;

    // check the cache
    Cache myCache = context.Cache;
    string key = ProductColorID + "-" + Convert.ToString(bIsDefaultImage) +
    "-" + imgType.ToString();
    if(myCache[key] != null)
    {

    byte[] bytesImg = (byte[]) myCache[key];
    try
    {
    context.Response.BinaryWrite(bytesImg);
    }
    finally
    {
    bytesImg = null;
    }
    return;
    }

    // fetch the object from db or create it.
    ImagesDB myImageDB = new ImagesDB();
    byte[] ImageData = myImageDB.GetImage(int.Parse(ProductColorID),
    bIsDefaultImage);
    if(ImageData != null)
    {
    WriteImage(ref ImageData, ref context, imgType, key);

    ImageData = null;
    return;
    }
    else
    {
    DrawImageNotFound(ref context);
    }
    }

    public bool IsReusable
    {
    get { return true; }
    }

    public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
    {
    }

    public bool ThumbnailCallback()
    {
    return true;
    }

    private void WriteImage(ref byte[] ImageData, ref HttpContext context,
    ImageType imgType, string cacheKey)
    {
    System.Drawing.Image myImage = null;
    MemoryStream myStream = new MemoryStream();
    MemoryStream processedMemStream = new MemoryStream();
    try
    {
    myStream.Write(ImageData, 0, ImageData.Length);
    myImage = System.Drawing.Image.FromStream(myStream);
    int imageWidth = myImage.Width;
    int imageHeight = myImage.Height;
    int processedHeight;
    if(imgType == ImageType.Full)
    {
    processedHeight = imageHeight;
    }
    else
    {
    processedHeight = (int)imgType;
    }
    double multiplicationFactor = (double)processedHeight/(double)imageHeight;
    int processedWidth = (int)( (double)imageWidth * multiplicationFactor);

    Bitmap processedBP = new Bitmap(processedWidth, processedHeight);
    Graphics g = Graphics.FromImage(processedBP);
    try
    {
    g.SmoothingMode =SmoothingMode.HighQuality;
    g.InterpolationMode =InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode =PixelOffsetMode.HighQuality;

    Rectangle rect=new Rectangle(0,0,processedWidth,processedHeight);
    //Draw the old image on to the new image using the graphics object:
    g.DrawImage(myImage,rect,0,0,myImage.Width,myImage.Height,GraphicsUnit.Pixel);

    processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);
    processedBP.RotateFlip(RotateFlipType.Rotate180FlipNone);

    processedBP.Save(processedMemStream, ImageFormat.Jpeg);
    byte[] processedImageData = processedMemStream.ToArray();
    if(processedImageData != null)
    {
    context.Cache.Add(cacheKey, processedImageData, null,
    Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10),
    CacheItemPriority.Normal, new
    CacheItemRemovedCallback(this.RemovedCallback));

    context.Response.BinaryWrite(processedImageData);
    processedImageData = null;
    }
    }
    finally
    {
    g.Dispose();
    processedBP.Dispose();
    }
    }
    finally
    {
    processedMemStream.Close();
    myStream.Close();
    myImage.Dispose();
    }
    }

    private void DrawImageNotFound(ref HttpContext context)
    {
    Bitmap myBitMap = new Bitmap(50,50);
    Graphics myGraphics = Graphics.FromImage(myBitMap);
    myGraphics.DrawLine(new Pen(Color.Red), 0, 0, 50, 50);
    myGraphics.DrawLine(new Pen(Color.Red), 0, 50, 50, 0);
    context.Response.ContentType = "image/gif";
    myBitMap.Save(context.Response.OutputStream, ImageFormat.Gif);
    myBitMap.Dispose();
    myGraphics.Dispose();
    }
    }
    ---------------------------------------------------------------------------

    "Hitesh" wrote:

    > Hi Hermit,
    >
    > Thanks for your useful reply, i will try to use that information. I have
    > seen the contents of the URL that you have suggested so the first one says
    > that. When i am getting the response from the web server where my actual
    > pages (They are actually reports) exists in the Handler then first i have to
    > parsse that response and replace all the src attributes of the <img> tag to
    > look something like this:
    >
    > <img src=<%ReadImage.aspx?imgPath='old value of the src attribute'%>
    >
    > and then i should write this parsed html response to the browser and now the
    > ReadImage.aspx comes into picture and that will read the image from the web
    > server which is hidden from the outside world (Reports are here) and render
    > on the user's browser but user has in no way access to the images on the
    > hidden server.
    >
    > Is this understanding is correct ? or i am not on the right track.
    >
    > Please if you can take our some of your precious time and give your
    > suggestion on the above details that i have given then it will help me to
    > start in the right direction.
    >
    > Thanks once again for your knowldge sharing.
    >
    > "Hermit Dave" wrote:
    >
    > > Hitesh,
    > >
    > > Have a look at this article from aspalliance
    > > http://aspalliance.com/articleViewer.aspx?aId=141
    > >
    > > that example uses an aspx page to output the image.
    > > once you have that working and you would like to optimise it then put the
    > > code into http handler
    > > This link should help with handler (custom) and image manipulation code
    > > http://www.microsoft.com/belux/nl/msdn/community/columns/desmet/httphandler.mspx
    > > I have used the code similar to the one in above link from ms but i just
    > > created a generic handler .ashx rather than a custom handler.
    > >
    > > HTH
    > >
    > > Regards,
    > >
    > > Hermit Dave
    > > http://hdave.blogspot.com
    > >
    > >
    > >
    > > "Hitesh" wrote:
    > >
    > > > Hi,
    > > >
    > > > I am getting the response from another Website by using the HttpHandler in
    > > > my current site. I am getting the page but all the images on that page are
    > > > not appearing only placeholder are displayed.
    > > >
    > > > Can anybody know this issue and help me to resolve this.
    > > >
    > > > In past i received the response saying that i should download the image
    > > > first and then parse the actual response and modify the src attribute of the
    > > > img tag and then rendered the output on the browser. But i could not able to
    > > > find a way to do this, if this approach is correct then if somebody can
    > > > provide me a code snippet then it will be really helpful.
    > > >
    > > > Thanks
    > > > Hitesh
     
    =?Utf-8?B?SGVybWl0IERhdmU=?=, May 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David J. Berman

    Re: HttpHandler for images and browser behavior

    David J. Berman, Jun 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,158
    David J. Berman
    Jun 24, 2003
  2. =?Utf-8?B?SGl0ZXNo?=

    Images are not getting displayed when using HTTPHandler

    =?Utf-8?B?SGl0ZXNo?=, Apr 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    469
    =?Utf-8?B?SGl0ZXNo?=
    Apr 25, 2005
  3. =?Utf-8?B?Q2FzcGE=?=

    Preload images returned by an HttpHandler

    =?Utf-8?B?Q2FzcGE=?=, Mar 9, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    406
    Chilumba Mubashi [MSFT]
    Mar 28, 2007
  4. Joe Reiss

    HttpHandler not getting called

    Joe Reiss, Jun 5, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    472
    PlatinumBay
    Jun 6, 2007
  5. Hitesh

    Getting access denied error on using HTTPHandler

    Hitesh, Apr 13, 2005, in forum: ASP .Net Security
    Replies:
    0
    Views:
    142
    Hitesh
    Apr 13, 2005
Loading...

Share This Page