BinaryWrite with multiple images

R

Richard

Hello,

I have a multipage tiff file which I am trying to output to a web browser
using Response.BinaryWrite. I am taking the tiff file, then looping for
each page and saving it off to a jpg in a memory stream. The problem I'm
having is when the page comes up, only the first image is shown. If I save
each page off in a file I can see that each page is unique and it's not a
problem with the stream, it seems to be a problem with outputting to the
browser. And my loop is running through for each page. My code is below,
any tips would be greatly appreciated. Thanks!

for (int i=0; i< tifPageCount; i++)
{

System.IO.MemoryStream JPGStream = new MemoryStream();

JPGStream.SetLength(0);


TifImage.SelectActiveFrame(new
System.Drawing.Imaging.FrameDimension(TifImage.FrameDimensionsList[0]), i);

TifImage.Save(JPGStream, System.Drawing.Imaging.ImageFormat.Jpeg);

JPGStream.Position = 0;

byte[] NewBuffer = new byte[JPGStream.Length];

JPGStream.Read(NewBuffer, 0, (int) JPGStream.Length);

JPGStream.Close();

Response.ContentType = "image/jpeg";

Response.OutputStream.Write(NewBuffer, 0, NewBuffer.Length);

Response.ContentType = "text/html";

Response.Write("<BR><BR><BR>");

}

Response.End();

TifImage.Dispose();
 
J

Jon Skeet [C# MVP]

Richard said:
I have a multipage tiff file which I am trying to output to a web browser
using Response.BinaryWrite. I am taking the tiff file, then looping for
each page and saving it off to a jpg in a memory stream.

You're trying to write out several responses in one. That just isn't
the way the web works. Instead, you should write out HTML which
requests each JPEG in turn. Then for each of those JPEG requests, you
supply just the single JPEG.

Btw, you should read http://www.pobox.com/~skeet/csharp/readbinary.html
 
R

Richard

Thanks for your help, I think I see what you're saying but want to make
sure. Instead of doing a bunch of 'Response.Write' commands, I should write
everything out to a separate string, then do a Response.Write(MyString)?

One thing I didn't mention, I can't save the pages off to files on the hard
drive, so I don't think I could do a '<img src>' tag, I have to be able to
write it out as binary. Would that be an additional problem? Thanks again!
 
J

Jon Skeet [C# MVP]

Richard said:
Thanks for your help, I think I see what you're saying but want to make
sure. Instead of doing a bunch of 'Response.Write' commands, I should write
everything out to a separate string, then do a Response.Write(MyString)?
No...

One thing I didn't mention, I can't save the pages off to files on the hard
drive, so I don't think I could do a '<img src>' tag, I have to be able to
write it out as binary. Would that be an additional problem? Thanks again!

No, you *must* use <img src=...> tags. You could cache the images in
memory if you need to, but the basic way things will have to work is:

1) Browser requests page
2) You respond with a page full of <img src=...> tags
3) Browser requests an image
4) You response with the image
5) Go back to step 3 for each image you've specified
6) User gets to see pictures
 
R

Richard

How could I use the caching? My problem is, the files that I'm needing to
display are Tiff files. As I understand it, IE can't display tiff images.
So I'm trying to take each tiff file and convert each page to a jpg in
memory( I can't manually save the jpg out to the server to display due to
security reasons because I'd have to give ASPNET user write permission...or
will caching get around that?). Do you have any suggestions, or any links
that might help me out? Sorry I'm a bit new to the Asp.net world. Thanks
again for your help!
 
J

Jon Skeet [C# MVP]

Richard said:
How could I use the caching? My problem is, the files that I'm needing to
display are Tiff files. As I understand it, IE can't display tiff images.
So I'm trying to take each tiff file and convert each page to a jpg in
memory( I can't manually save the jpg out to the server to display due to
security reasons because I'd have to give ASPNET user write permission...or
will caching get around that?). Do you have any suggestions, or any links
that might help me out? Sorry I'm a bit new to the Asp.net world. Thanks
again for your help!

Convert all the images into jpegs, then cache the data in the session
(read up on ASP.NET sessions in general for more information). When the
session expires, the memory will be freed, and all will be well. If you
need the images after the session has expired, you have to convert the
images again.
 
R

Richard

I'll read up on the sessions, thanks again!


Jon Skeet said:
Convert all the images into jpegs, then cache the data in the session
(read up on ASP.NET sessions in general for more information). When the
session expires, the memory will be freed, and all will be well. If you
need the images after the session has expired, you have to convert the
images again.
 

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

Top