response.contenttype

Discussion in 'ASP .Net' started by Mike Kansky, Jun 17, 2006.

  1. Mike Kansky

    Mike Kansky Guest

    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!
    Mike Kansky, Jun 17, 2006
    #1
    1. Advertising

  2. Mike Kansky

    vMike Guest

    "Mike Kansky" <> wrote in message
    news:%...
    >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()
    vMike, Jun 17, 2006
    #2
    1. Advertising

  3. Mike Kansky

    Mike Kansky Guest

    Can i do something like that from URL where image is located?




    "vMike" <> wrote in message
    news:MAJkg.12642$...
    >
    > "Mike Kansky" <> wrote in message
    > news:%...
    >>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()
    >
    Mike Kansky, Jun 17, 2006
    #3
  4. Mike Kansky

    vMike Guest

    "Mike Kansky" <> wrote in message
    news:...
    > 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>
    vMike, Jun 17, 2006
    #4
  5. Mike Kansky

    Mike Kansky Guest

    Thanks Mike!

    Last question:

    Is it really faster then me doing response.redirect in Image.aspx?
    Are there any other advantages of using contenttype?
    "vMike" <> wrote in message
    news:bCQkg.9202$y%...
    >
    > "Mike Kansky" <> wrote in message
    > news:...
    >> 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>
    >
    >
    >
    >
    Mike Kansky, Jun 18, 2006
    #5
    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. alien2_51

    Response.ContentType

    alien2_51, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    737
    alien2_51
    Jun 26, 2003
  2. Bart Vanwesemael
    Replies:
    1
    Views:
    510
    William F. Robertson, Jr.
    Jul 28, 2003
  3. TJS
    Replies:
    5
    Views:
    1,028
    Alvin Bruney
    Nov 19, 2003
  4. =?Utf-8?B?RXlhbCBaaW5kZXI=?=

    Response.ContentType and Viewstate problem

    =?Utf-8?B?RXlhbCBaaW5kZXI=?=, Apr 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    442
    =?Utf-8?B?RXlhbCBaaW5kZXI=?=
    Apr 22, 2004
  5. Katie
    Replies:
    8
    Views:
    250
    Anthony Jones
    Aug 17, 2006
Loading...

Share This Page