Simple Question! - Streaming a small image to the browser in c#

M

malcolm

I've seen several posts around the Internet and many of them either
don't work or only partially work. All I'm trying to do is record some
action asynchronously from an html file, a sort of logging mechanism.
The product is a CD catalog that will be delivered to many customers
and they want the ability to log specific pages accross the Internet,
and there's no gaurantee these people will be connected to the
Internet, the ones that are though, we want to log their actions. I've
done something similar in COM a while ago, but can't get it to work in
..NET.

Thus, i will put an 1 X 1 pixel image tag at the bottom of each page I
want logged. I'm also going to pass querystring information, which is
all perfectly legal in HTML as long as your output is binary.

<img src="http://www.domain.com/log.aspx?a=33&b=467">

All I'm trying to do is write the log.aspx page to simply return an
image that I have stored on my webserver. it should return the same
1X1 transparent image every time. Behind the scenes, the aspx page
will log the querystring info to the db (this part's done).

Here's the best way I've got it to work (code is simplified to
relevant parts), but it has 2 problems: 1) it creates the image on the
fly (don't think that's necessary) and 2) the image is not transparent
(even after setting many settings on the bitmap object).

private void Page_Load(object sender, System.EventArgs e)
{
Bitmap bitmap = new Bitmap(1, 1);
try
{
Response.ContentType = "image/gif";
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
catch {}
finally
{
bitmap.Dispose();
}
}

I'm guessing there's a simpler (much different) way to achieve this.
That is, load an image from the fileserver and spit it out to the
window. I've spent several hours messing around with stream objects
and such, but can't find the right way...

Thanks, in advance, for any help!

-dave
 
G

Guest

here's the logic, too lazy to write cod

1. have a 1x1 transparent gif on file serve
2. Open gif as Fil
3. read bytes from FileStrea
4. write bytes to HttpResponse.OutputStrea
5. set HttpResponse.ContentType to "image/gif

done

a cleaner solution would be to use a IHttpHandler instead of .aspx file. might perform better too since you can reuse a HttpHandler. even better, embed the 1x1 transparent gif with the IHttpHandler assembly, then you got a self contained unit without dependency on external files.
 
J

Jon Skeet [C# MVP]

All I'm trying to do is write the log.aspx page to simply return an
image that I have stored on my webserver. it should return the same
1X1 transparent image every time.

Why don't you just have it as a web-accessible file then, and don't
bother using ASP.NET at all for that file?
 
M

malcolm

Hello Jon, thanks for the feedback, but I think you missed my point in
my original post. I will be passing querystring information that I
must capture and store to the database. Example:

<img src="http://www.domain.com/log.aspx?a=1&b=2">

the file log.aspx needs to:
1) gather all the querystring info and log it to the database (this
can even happen asynch, which is nice)
2) return an image (and only an image).

i won't be able to do #1 if I do what you said (which is the whole
point of this approach)

I'm sure there's several ways to solve this, but this seems like a
very good way. Basically to log info to a remote server on an html
page that originates from a CD (not from a website). The cool thing
about this approach is that images are asynch by nature and the image
is a 1x1 tranparant image (not noticable) at the very end of the html
stream (so the user won't even notice).

thanks,
dave
 
J

Jon Skeet [C# MVP]

malcolm said:
Hello Jon, thanks for the feedback, but I think you missed my point in
my original post. I will be passing querystring information that I
must capture and store to the database.

Ah, right, I'm with you.
Example:

<img src="http://www.domain.com/log.aspx?a=1&b=2">

the file log.aspx needs to:
1) gather all the querystring info and log it to the database (this
can even happen asynch, which is nice)
2) return an image (and only an image).

i won't be able to do #1 if I do what you said (which is the whole
point of this approach)

Sure. One thing you could do is just send a redirect to the image. That
would mean an extra round trip though, unfortunately.

Other than that, basically you can do as the other posters have said
(IIRC) - read the file in and write it out. If it's going to be
commonly used, you could read it in just once and cache the byte array
so you could quickly write it out again. There's certainly no need to
load it as a bitmap or anything like that.
 
M

malcolm

Very interesting. This is the best way (ISAPI filters). I actually
having it working now and it's deployed, but I definetely want to try
it your way, since I've never really written one of these before.

Thanks for the tip!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top