473,288 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Images not getting rendered when using httphandler

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
Nov 19 '05 #1
3 3036
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/ms...tphandler.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

Nov 19 '05 #2
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/ms...tphandler.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

Nov 19 '05 #3
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&Pr oductColorID=" +
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.Rotate180Fli pNone);
processedBP.RotateFlip(RotateFlipType.Rotate180Fli pNone);

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/ms...tphandler.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

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a...
4
by: trinitypete | last post by:
Hi, I have a picture gallery type application that loads up several photographs, due to the file sizes of the images the page load is slow - Yes I know I need to thumbnail and reduce image for...
2
by: Hitesh | last post by:
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...
7
by: gemel | last post by:
I am developing an application that uses SQL 2000 as my source of images. I have successfully created the code to load the images onto the SQL Server and also to retrieve the images into a dataset....
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
5
by: Peter Lapic | last post by:
I have to create a image web service that when it receives an imageid parameter it will return a gif image from a file that has been stored on the server. The client will be an asp.net web page...
61
by: phil-news-nospam | last post by:
Why does SVG need a different tag than other images? IMHO, SVG should be implemented as an image type just like any other image type, allowing it to work with <img> tags, and ... here is the...
8
by: Bill | last post by:
Anyone have any success in using global.asax to protect images in a folder from being linked to by external websites? I'd tried to use global.asa in the past, with no success. Any help would be...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.