By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.FrameD imensionsList[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
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"T. Davis" <co*****@escmag.com> wrote in message
news:6f**************************@posting.google.c om...
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.FrameD imensionsList[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

Nov 18 '05 #2

P: n/a
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" <er**@cc.ensoft-software.com> wrote in message news:<Oh**************@TK2MSFTNGP10.phx.gbl>...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"T. Davis" <co*****@escmag.com> wrote in message
news:6f**************************@posting.google.c om...
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.FrameD imensionsList[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

Nov 18 '05 #3

P: n/a
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"T. Davis" <co*****@escmag.com> wrote in message
news:6f**************************@posting.google.c om...
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" <er**@cc.ensoft-software.com> wrote in message

news:<Oh**************@TK2MSFTNGP10.phx.gbl>...
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/
er**@cc.ensoft-software.com [remove the first "CC."]

"T. Davis" <co*****@escmag.com> wrote in message
news:6f**************************@posting.google.c om...
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.FrameD imensionsList[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

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.