Displaying TIFF with ASP.NET/C# - MemoryStream throws GDI+ Generic Error

T

T. Davis

In C#, I am able to successfully stream a TIFF image that comes from a
BLOB field in a database, save it to file, then convert the pages
within TIFF file into jpegs (using GDI+) and display on the web (using
ASP.NET).

However, when I generate the Image object using FromStream (passing in
the MemoryStream containing image bytes), an exception of "A generic
error occurred in GDI+" is thrown when performing the conversion/save
for display. I cannot find info on why this occurs?

Again, using FromFile works fine, and the image streamed from the DB
is fine.

-- start code snippet --

/* bytedata is a byte array holding the returned image (BLOB) */

Response.ContentType = "image/jpeg";

// get byte array's length
int ArraySize = new int();
ArraySize = byteData.GetUpperBound(0);

// put into memorystream and instantiate Image object
MemoryStream memStream = new MemoryStream();
memStream.Write(byteData,0,ArraySize);
System.Drawing.Image myImg =
System.Drawing.Image.FromStream(memStream);
memStream.Close();
memStream = null;

// prepare for display - debug showed correct FrameDimension used
System.Drawing.Imaging.FrameDimension fDimension;
fDimension = new System.Drawing.Imaging.FrameDimension(myImg.FrameDimensionsList[0]);

// just display the first page in TIFF, or display single-page TIFF
try {
myImg.SelectActiveFrame(fDimension,0);
myImg.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg); // EXCEPTION IS THROWN HERE
catch (Exception err)
{
throw err;
}
finally
{
myImg.Dispose();
}

-- end code snippet --


Help, please. Thanks in advance...

TD
 
E

Eric Newton

I believe bitmaps need to save to seekable streams

Write to a memory stream first, then byte copy out to Response.OutputStream

HTH
 
T

T. Davis

Eric -

Is MemoryStream not a seekable stream?

Also, could you elaborate on your suggestion... you suggest byte copy
out of the memory stream into an outputstream? I'm not sure I can
pass that outputstream then into the Image object, if I understand
correctly...

Thanks,
TD


Eric Newton said:
I believe bitmaps need to save to seekable streams

Write to a memory stream first, then byte copy out to Response.OutputStream

HTH

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

T. Davis said:
In C#, I am able to successfully stream a TIFF image that comes from a
BLOB field in a database, save it to file, then convert the pages
within TIFF file into jpegs (using GDI+) and display on the web (using
ASP.NET).

However, when I generate the Image object using FromStream (passing in
the MemoryStream containing image bytes), an exception of "A generic
error occurred in GDI+" is thrown when performing the conversion/save
for display. I cannot find info on why this occurs?

Again, using FromFile works fine, and the image streamed from the DB
is fine.

-- start code snippet --

/* bytedata is a byte array holding the returned image (BLOB) */

Response.ContentType = "image/jpeg";

// get byte array's length
int ArraySize = new int();
ArraySize = byteData.GetUpperBound(0);

// put into memorystream and instantiate Image object
MemoryStream memStream = new MemoryStream();
memStream.Write(byteData,0,ArraySize);
System.Drawing.Image myImg =
System.Drawing.Image.FromStream(memStream);
memStream.Close();
memStream = null;

// prepare for display - debug showed correct FrameDimension used
System.Drawing.Imaging.FrameDimension fDimension;
fDimension = new System.Drawing.Imaging.FrameDimension(myImg.FrameDimensionsList[0]);

// just display the first page in TIFF, or display single-page TIFF
try {
myImg.SelectActiveFrame(fDimension,0);
myImg.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg); // EXCEPTION IS THROWN HERE
catch (Exception err)
{
throw err;
}
finally
{
myImg.Dispose();
}

-- end code snippet --


Help, please. Thanks in advance...

TD
 
E

Eric Newton

MemoryStreams are seekable, unless a constructor variant is called that
forces no seeking... but in essense a MemoryStream is just a dynamically
sizing byte array.

Incidentally a Bitmap constructor can take a memory stream as a parameter

you could do a memStream.ToByteArray() then throw that at
Response.OutputStream.WriteBytes()


--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

T. Davis said:
Eric -

Is MemoryStream not a seekable stream?

Also, could you elaborate on your suggestion... you suggest byte copy
out of the memory stream into an outputstream? I'm not sure I can
pass that outputstream then into the Image object, if I understand
correctly...

Thanks,
TD


"Eric Newton" <[email protected]> wrote in message
I believe bitmaps need to save to seekable streams

Write to a memory stream first, then byte copy out to Response.OutputStream

HTH

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

T. Davis said:
In C#, I am able to successfully stream a TIFF image that comes from a
BLOB field in a database, save it to file, then convert the pages
within TIFF file into jpegs (using GDI+) and display on the web (using
ASP.NET).

However, when I generate the Image object using FromStream (passing in
the MemoryStream containing image bytes), an exception of "A generic
error occurred in GDI+" is thrown when performing the conversion/save
for display. I cannot find info on why this occurs?

Again, using FromFile works fine, and the image streamed from the DB
is fine.

-- start code snippet --

/* bytedata is a byte array holding the returned image (BLOB) */

Response.ContentType = "image/jpeg";

// get byte array's length
int ArraySize = new int();
ArraySize = byteData.GetUpperBound(0);

// put into memorystream and instantiate Image object
MemoryStream memStream = new MemoryStream();
memStream.Write(byteData,0,ArraySize);
System.Drawing.Image myImg =
System.Drawing.Image.FromStream(memStream);
memStream.Close();
memStream = null;

// prepare for display - debug showed correct FrameDimension used
System.Drawing.Imaging.FrameDimension fDimension;
fDimension = new System.Drawing.Imaging.FrameDimension(myImg.FrameDimensionsList[0]);

// just display the first page in TIFF, or display single-page TIFF
try {
myImg.SelectActiveFrame(fDimension,0);
myImg.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg); // EXCEPTION IS THROWN HERE
catch (Exception err)
{
throw err;
}
finally
{
myImg.Dispose();
}

-- end code snippet --


Help, please. Thanks in advance...

TD
 
Joined
May 19, 2009
Messages
1
Reaction score
0
FYI, I have found similar problems when loading an image with Image.FromStream and then calling SelectActiveFrame(). I talk about it in my "Image Formatting in C#" article on seanmcilvenna.com.

Thanks,
Sean
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top