response.contenttype

M

Mike Kansky

I have an image.aspx that does the following:

if request("s")=1 then
response.redirect(http://www.domain.com/image1.gif)
else
response.redirect(http://www.domain.com/image2.gif)
end if

And then i use it like that in my pages: <img src="images.aspx?s=1">

Now the question:

I do not like the way i do it, i think it is better and faster to do it by
using response.contenttype="image/gif" in image.aspx. Am i right?

if i am right could anyone give me an example of code? I found some examples
but they were getting the image from database and saving into the response
object.
How do i do the same with an URL image?

Thanks a lot!
 
V

vMike

Mike Kansky said:
I have an image.aspx that does the following:

if request("s")=1 then
response.redirect(http://www.domain.com/image1.gif)
else
response.redirect(http://www.domain.com/image2.gif)
end if

And then i use it like that in my pages: <img src="images.aspx?s=1">

Now the question:

I do not like the way i do it, i think it is better and faster to do it by
using response.contenttype="image/gif" in image.aspx. Am i right?

if i am right could anyone give me an example of code? I found some
examples but they were getting the image from database and saving into the
response object.
How do i do the same with an URL image?

Thanks a lot!

You can stream it from a file something along the lines of the following


if request("s")=1 then


Response.ClearContent
Response.ContentType = "image/jpeg"
......
snip
Dim myImg as system.drawing.image =
system.drawing.image.fromfile(Yourfilenameandpath)
you can resize here if needed
dim origSize as Size = myImg.Size
if origSize.width < origSize.height then 'must be portrait
dim intHolder as int32 = intHorz
intHorz = intVert
intVert = intHolder
end if
Dim mysize as size = new size(intHorz,intVert)
Dim myBitmap as new bitmap(myImg,mysize)
mybitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
mybitmap.dispose()
myImg.dispose()
 
V

vMike

Mike Kansky said:
Can i do something like that from URL where image is located?
Sure. In its simplest form you would do this.

<%@ Page Language="VB" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>

<script runat=server language=vb>

Sub Page_Load(sender as object, e as eventargs)
GetPictureStream("http://yoururihere")

End Sub


Sub GetPictureStream(strURI as string)
Response.ClearContent
Response.ContentType = "image/jpeg"
Dim client As New WebClient()
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.2; .NET CLR 1.0.3705;)")
Dim data As Stream = client.OpenRead(strURI)
dim myimg as system.drawing.image =
system.drawing.image.fromstream(data)
myimg.Save(Response.OutputStream, ImageFormat.Jpeg)
myimg.dispose()
data.Close()

End Sub
</script>
 
M

Mike Kansky

Thanks Mike!

Last question:

Is it really faster then me doing response.redirect in Image.aspx?
Are there any other advantages of using contenttype?
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top