473,221 Members | 1,936 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,221 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 3028
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...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
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: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.