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

  • Thread starter Thread starter T. Davis
  • Start date Start date
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
 
I believe bitmaps need to save to seekable streams

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

HTH
 
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
 
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
 
Back
Top