How to realize a C# page that give back an image instead of an html page

Discussion in 'ASP .Net' started by Etantonio, Dec 12, 2006.

  1. Etantonio

    Etantonio Guest

    Good morning,
    I have a static html page where I want to load image that day to day
    are in a different path,
    to achieve this I would want that the link point to a c# page where I
    create dinamically the new url,
    but how I can arrange a c# page that give back an image and not an html
    page ?
    Can you help me to realize this ??

    Many Thanks,

    Eng. Antonio D'Ottavio
    www.etantonio.it/en
    Etantonio, Dec 12, 2006
    #1
    1. Advertising

  2. Etantonio

    sloan Guest

    microsoft.public.dotnet.languages.csharp
    Tuesday, December 12, 2006 10:16 AM


    Please do not cross post.


    http://groups-beta.google.com/group...3bb6c39bdc0/0867fd4b2576dde6#0867fd4b2576dde6


    "Etantonio" <> wrote in message
    news:...
    > Good morning,
    > I have a static html page where I want to load image that day to day
    > are in a different path,
    > to achieve this I would want that the link point to a c# page where I
    > create dinamically the new url,
    > but how I can arrange a c# page that give back an image and not an html
    > page ?
    > Can you help me to realize this ??
    >
    > Many Thanks,
    >
    > Eng. Antonio D'Ottavio
    > www.etantonio.it/en
    >
    sloan, Dec 12, 2006
    #2
    1. Advertising

  3. Etantonio

    Goofy Guest

    I'm not sure that this is a cross post.

    The detail is a little ambiguous really. He could mean and aspx c# based
    webform being used to generate html to get the desired result, in which case
    posting here would be appropriate ?!?


    "sloan" <> wrote in message
    news:...
    >
    > microsoft.public.dotnet.languages.csharp
    > Tuesday, December 12, 2006 10:16 AM
    >
    >
    > Please do not cross post.
    >
    >
    > http://groups-beta.google.com/group...3bb6c39bdc0/0867fd4b2576dde6#0867fd4b2576dde6
    >
    >
    > "Etantonio" <> wrote in message
    > news:...
    >> Good morning,
    >> I have a static html page where I want to load image that day to day
    >> are in a different path,
    >> to achieve this I would want that the link point to a c# page where I
    >> create dinamically the new url,
    >> but how I can arrange a c# page that give back an image and not an html
    >> page ?
    >> Can you help me to realize this ??
    >>
    >> Many Thanks,
    >>
    >> Eng. Antonio D'Ottavio
    >> www.etantonio.it/en
    >>

    >
    >
    Goofy, Dec 12, 2006
    #3
  4. Re: How to realize a C# page that give back an image instead of anhtml page

    Hi,

    Etantonio wrote:
    > Good morning,
    > I have a static html page where I want to load image that day to day
    > are in a different path,
    > to achieve this I would want that the link point to a c# page where I
    > create dinamically the new url,
    > but how I can arrange a c# page that give back an image and not an html
    > page ?
    > Can you help me to realize this ??
    >
    > Many Thanks,


    In order to return an image to a Request instead of HTML code, you can
    use the following (typically in Page_Load):

    Response.Clear();
    Response.ContentType = "image/jpeg";
    Response.StatusCode = 200;
    Bitmap bmpOriginal = new Bitmap( "yourimage.jpg" );
    bmpOriginal.Save( Response.OutputStream, bmpOriginal.RawFormat );
    bmpOriginal.Dispose();
    bmpOriginal = null;
    Response.Close();
    return;

    It is not strictly necessary to Clear and Close the Response, but that's
    safer.

    On the client, simply use (for example)
    <img src="yourpage.aspx?picname=yourimage.jpg" />

    Note that using an ASHX custom HTTP handler instead of an ASPX is more
    efficient for this kind of things, because then you avoid the whole
    event wiring generated by the Page class.

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
    Laurent Bugnion, Dec 12, 2006
    #4
  5. Etantonio

    Etantonio Guest

    Thanks for your reply.
    Using your suggestion I arranged the following code in file
    http://www.etantonio.it/Temp/Trad.aspx

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    <%@ Page Language="c#" Trace="true" Debug="true" %>
    <%@ import Namespace="System.Net" %>
    <%@ import Namespace="System.IO" %>
    <%@ import Namespace="System.Drawing" %>
    <script runat="server">
    void Page_Load(Object Src, EventArgs E )
    {
    if (!Page.IsPostBack)
    {
    String sAddressTime = "http://www.etantonio.it/images/zh.gif";

    HttpWebRequest wreq;
    HttpWebResponse wresp;
    Stream mystream;
    Bitmap bmp;


    bmp = null;
    mystream = null;
    wresp = null;
    try
    {
    wreq = (HttpWebRequest)WebRequest.Create(sAddressTime);

    wresp = (HttpWebResponse)wreq.GetResponse();
    if ((mystream = wresp.GetResponseStream()) != null)
    {
    Response.Clear();
    Response.ContentType = "image/jpeg";
    Response.StatusCode = 200;
    Bitmap bmpOriginal = new Bitmap( "yourimage.jpg" );
    bmp = new Bitmap(mystream);
    bmp.Save( Response.OutputStream, bmp.RawFormat );
    bmp.Dispose();
    bmp = null;
    Response.Close();
    return;
    }
    }
    finally
    {
    if (mystream != null)
    mystream.Close();
    if (wresp != null)
    wresp.Close();
    }
    }
    }
    </script>
    <html>
    <head>/head>
    <body >
    </body>
    </html>
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    that it is called from a simple html file named CallTrad.aspx


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    />
    <title>Untitled Document</title>
    </head>

    <body>
    <img src="http://www.etantonio.it/Temp/Trad.aspx" />
    </body>
    </html>
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    the result is that I've not the image I require,
    I simply have a blank pge but with no image, any suggestion to me ??
    Thanks

    Antonio D'Ottavio
    www.etantonio.it/en
    Etantonio, Dec 12, 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. Tasperian Jigs
    Replies:
    2
    Views:
    413
    Oliver Wong
    Jan 10, 2006
  2. Etantonio
    Replies:
    0
    Views:
    299
    Etantonio
    Dec 12, 2006
  3. grocery_stocker
    Replies:
    10
    Views:
    612
    Keith Thompson
    May 25, 2005
  4. Barbiturico76

    Generate HTML and give it back to client

    Barbiturico76, Jul 28, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    289
    Barbiturico76
    Jul 29, 2008
  5. Guest
    Replies:
    2
    Views:
    96
    Robert Klemme
    Jan 9, 2009
Loading...

Share This Page