Re: HttpHandler for images and browser behavior

Discussion in 'ASP .Net' started by David J. Berman, Jun 24, 2003.

  1. No no I don't know why that doesn't work because it really should but I'll
    give you a snippet that DOES work. Oddly enough, it works by NOT specifying
    the content type. Bizzare eh? Also, it works fine as URL in address bar or
    as img src=.

    Here ya go free of charge. Sorry if it looks like html to you - you get
    what you pay for :) Insert it using paste as html or something and it
    should look fine. This code shows a thumbnail picture from a sql database
    using a simple select thumbnail from pictures query. If there is no data,
    it draws a black box instead. Notice that I had to comment out the mime
    type in order to get it to work:

    OleDbConnection myConn = new
    OleDbConnection(ConfigurationSettings.AppSettings["OLEDBDSN"].ToString());

    OleDbCommand myCmd = new OleDbCommand(sqlText, myConn);

    OleDbDataReader dr = null;


    //open the database and get a datareader

    try

    {

    myConn.Open();

    dr = myCmd.ExecuteReader();


    if ( dr.Read() ) // Check to make sure we loaded a user record

    {

    // Response.ContentType = "image/jpeg";

    Response.BinaryWrite( (byte[]) dr["Thumbnail"] );

    }

    else

    {

    // Draw the .JPG

    //------------------------------------------------------

    // Create a bitmap that measures 130 pixels square

    Bitmap bitmap = new Bitmap (130, 130,

    PixelFormat.Format32bppArgb);

    // Create a Graphics object for drawing to the bitmap

    Graphics g = Graphics.FromImage (bitmap);

    // TODO: Use Graphics methods to draw the image

    // Set the response's content type to image/jpeg

    // Response.ContentType = "image/jpeg";

    // Write the image to the HTTP response

    bitmap.Save (Response.OutputStream, ImageFormat.Jpeg);

    // Clean up before returning

    bitmap.Dispose ();

    g.Dispose ();

    //------------------------------------------------------

    } // Draw the image

    } // try

    catch(Exception myException)

    {

    Trace.Write("Oops. The error: " + myException.Message);

    } // catch

    finally

    { // Do this no matter what

    // Close the database connenction

    myConn.Close();

    }

    } // Done trying to load image from database



    "Kevin Spencer" <> wrote in message
    news:#...
    > Thanks again, George. Adding a "Content-Disposition" header had no effect.

    I
    > have to agree that it must be a bug in IE. As I mentioned before, if I put
    > the address with QueryString into an image tag in an HTML page, it renders
    > fine. But if I type the address in the browser window, it doesn't. Hope
    > Microsoft is listening to this thread!
    >
    > Thanks again,
    >
    > Kevin Spencer
    > Microsoft FrontPage MVP
    > Internet Developer
    > http://www.takempis.com
    > Big things are made up of
    > lots of Little things.
    >
    > "George Ter-Saakov" <> wrote in message
    > news:%...
    > > Then it's a bug in the browser.
    > > First it must check Mime type. and only then to check extension of the

    > URL.
    > >
    > > George.
    > >
    > > "Kevin Spencer" <> wrote in message
    > > news:...
    > > > Thanks George. Believe me, I checked the ContentType in every way I

    > could,
    > > > and it is correct. I will try using Content-Disposition and let you

    > know.
    > > >
    > > > As for your question as to how the browser knows to hand it off to
    > > > Photoshop, that's no mystery. The original file had a .psd extension.

    > That
    > > > file extension is mapped to Photoshop in the OS.
    > > >
    > > > Thanks again,
    > > >
    > > > Kevin Spencer
    > > > Microsoft FrontPage MVP
    > > > Internet Developer
    > > > http://www.takempis.com
    > > > Big things are made up of
    > > > lots of Little things.
    > > >
    > > > "George Ter-Saakov" <> wrote in message
    > > > news:...
    > > > > The only question I have how does it know to pop up Adobe Photoshop?
    > > > > It seems to me it because of the file extension in the ULF.
    > > > > Are you sure that proper MIME type is set? It does not mean anything

    > > that
    > > > it
    > > > > works in <img src=..> because it's determines image type by the

    header
    > > of
    > > > > the image.
    > > > >
    > > > > Anyway you probably can change the behavior by using

    > > Content-Disposition.
    > > > > Response.AddHeader("Content-Disposition",

    "inline;filename=image.jpg")
    > > > > But i would double check that correct mime type is set. Show us the

    > mime
    > > > > type you are setting. It must be "img/jpeg" for jpeg file.
    > > > >
    > > > > George.
    > > > >
    > > > > "Kevin Spencer" <> wrote in message
    > > > > news:%...
    > > > > > I'm writing an HttpHandler for images that can modify images and

    > > return
    > > > > the
    > > > > > odified version of the image. One requirement is that it can work

    > with
    > > > > .psd
    > > > > > files, which are Adobe Photoshop images, and can't, of course, be
    > > > rendered
    > > > > > by a browser. However, one of the modifications is to change the

    > file
    > > > > format
    > > > > > of the image requested. So, the browser might request
    > > > > > "foo.psd?cmd=fmt&ext=jpg" to get a JPG back instead of the

    original
    > > PSD
    > > > > > file. For testing, I created an HTML page with an image tag in it,

    > and
    > > > set
    > > > > > it as my Start Page, so that I can debug in the IDE. When a

    request
    > > for
    > > > a
    > > > > > different format is received by the Handler, it sets the
    > > > > > Response.ContentType property to the proper MIME type for the

    image
    > > file
    > > > > > type it returns. In the HTML page, the browser correctly renders

    the
    > > JPG
    > > > > in
    > > > > > the page. However, if I put the URL to the image file, with the

    same
    > > > > > QueryString, in the browser's Address window and run it that way,

    > the
    > > > > > browser doesn't seem to recognize the ContentType header, and I

    get
    > an
    > > > > > "Open/Save" dialog. Of course, if I choose "Open," Adobe Photoshop
    > > > opens,
    > > > > > and gives me an error (since the file is NOT a psd, but only has a

    > > .psd
    > > > > > extension). If I choose to Save the image, and name it with a .jpg
    > > > > > extension, it opens fine in whatever image program I want to open

    it
    > > > with,
    > > > > > as it IS a JPG.
    > > > > >
    > > > > > What I don't understand is whay the difference in browser

    behavior.
    > In
    > > > > both
    > > > > > cases, the browser is sending an identical Request for the image,

    > and
    > > > > > getting back the identical Response. However, it displays

    correctly
    > in
    > > > the
    > > > > > context of an HTML page, but not as just a file. Is there any HTTP
    > > > header
    > > > > I
    > > > > > can append that will enable the browser to display the image

    > directly?
    > > > > >
    > > > > > TIA,
    > > > > >
    > > > > > Kevin Spencer
    > > > > > Microsoft FrontPage MVP
    > > > > > Internet Developer
    > > > > > http://www.takempis.com
    > > > > > Big things are made up of
    > > > > > lots of Little things.
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    David J. Berman, Jun 24, 2003
    #1
    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. =?Utf-8?B?SGl0ZXNo?=

    Images are not getting displayed when using HTTPHandler

    =?Utf-8?B?SGl0ZXNo?=, Apr 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    441
    =?Utf-8?B?SGl0ZXNo?=
    Apr 25, 2005
  2. =?Utf-8?B?SGl0ZXNo?=

    Images not getting rendered when using httphandler

    =?Utf-8?B?SGl0ZXNo?=, May 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    838
    =?Utf-8?B?SGVybWl0IERhdmU=?=
    May 13, 2005
  3. =?Utf-8?B?Q2FzcGE=?=

    Preload images returned by an HttpHandler

    =?Utf-8?B?Q2FzcGE=?=, Mar 9, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    379
    Chilumba Mubashi [MSFT]
    Mar 28, 2007
  4. Krushna
    Replies:
    1
    Views:
    434
    Peter Bucher [MVP]
    Jul 26, 2007
  5. Replies:
    2
    Views:
    1,227
    Adrienne
    Feb 13, 2005
Loading...

Share This Page