Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: HttpHandler for images and browser behavior

Reply
Thread Tools

Re: HttpHandler for images and browser behavior

 
 
David J. Berman
Guest
Posts: n/a
 
      06-24-2003
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.
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Httphandler for images - displays properly but unable to print Krushna ASP .Net 1 07-26-2007 07:04 AM
Preload images returned by an HttpHandler =?Utf-8?B?Q2FzcGE=?= ASP .Net 2 03-28-2007 12:36 AM
Images not getting rendered when using httphandler =?Utf-8?B?SGl0ZXNo?= ASP .Net 3 05-13-2005 07:14 AM
Images are not getting displayed when using HTTPHandler =?Utf-8?B?SGl0ZXNo?= ASP .Net 2 04-25-2005 04:49 AM
How to resize all images sizes and coordinates of the images on resize browser rams.kakara@gmail.com ASP General 2 02-13-2005 09:03 AM



Advertisments