Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Generic Error in GDI+"

19 views
Skip to first unread message

Simon Clarke

unread,
Sep 10, 2003, 12:44:03 PM9/10/03
to
Hi,

I'm haivng a little difficulty saving an image encapsulated by an
Image object to a stream - when Image.Save() is called an exception is
thrown with the message "Generic Error in GDI+". This code is executed
as part of a Windows Forms application running from a local codebase
(so no permission problems etc.). The code is as follows:

// inputStream is initialised ok.
Image myImage = Image.FromStream(inputStream);

// At this point myImage contains a valid image that can be viewed by
asigning
// it to a PictureBox etc.

MemoryStream outputStream = new MemoryStream();
myImage.Save(outputStream , System.Drawing.Imaging.ImageFormat.Bmp);
// This call to Image.Save() throws the exception "Generic Error in
GDI+"

The image is an ordinary 24-bit bitmap.

I'm totally out of ideas so any help would be much appreciated!

Thanks,
Simon Clarke.

Bob Powell [MVP]

unread,
Sep 10, 2003, 12:48:36 PM9/10/03
to
I think you'll have to explicitly close the input stream before attempting
to save the image again.

--
Bob Powell [MVP]
C#, System.Drawing

ANNOUNCING: Well Formed.
The monthy electronic magazine for
Windows Forms and GDI+ engineers
http://www.bobpowell.net/wellformed.htm

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm


"Simon Clarke" <goo...@simonclarke.org> wrote in message
news:75d95899.03091...@posting.google.com...

Simon Clarke

unread,
Sep 11, 2003, 5:44:41 AM9/11/03
to
Thanks for the suggestions I've had - I think I've worked out what the
problem is and at any rate it now works...

When you read the image from the stream it appears to read the data
but not process the image data itself (i.e. it only reads the header
to work out how many bytes to read from the stream). For some reason
when you call Image.Save() it fails to process the raw image data
before attempting to save it, hence the exception. However, if you
perform an operation on the image (rotating or fliping it for example)
then the Image object is forced to process the raw image data, and
subsequently Image.Save() operates correctly.

So a solution was simple (if slightly clumsy) ... Flip the image twice
after reading from the stream!

goo...@simonclarke.org (Simon Clarke) wrote in message news:<75d95899.03091...@posting.google.com>...

Nak

unread,
Sep 11, 2003, 10:23:48 AM9/11/03
to
> So a solution was simple (if slightly clumsy) ... Flip the image twice
> after reading from the stream!

Oooh! That deserves a prize for bodging! ;-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


John Hornick [MSFT]

unread,
Sep 11, 2003, 10:32:00 AM9/11/03
to
Hi,

I don't see that behavior here. Is it possible you were closing the
input stream before calling Save()? Remember that you need to keep
the source stream/memory/file alive for the life of the Bitmap.

Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit http://www.microsoft.com/security for current information on security.

Simon Clarke

unread,
Sep 11, 2003, 1:13:28 PM9/11/03
to
I've written a little test app to play around with closing streams (or
not) and it seems to sort the problem out (The error was raised by a
client and it was his code that closed the stream - so problem
solved!). However...

It is interesting that in my "solution" (I was under pressure to get
it fixed fast so call it a "bodge" if you like :-) ) I closed the
input stream, then flipped the image and finally saved it - and
closing the input stream had no ill-effect on the flip operation.

This makes me wonder: why Image.RotateFlip() should succeed and yet
Image.Save() (both operations surely need access to the same image
data) fails if the input stream is closed (I know very little about
the inner workings of GDI etc. so forgive me if this is a stupid
question! - This more out of pure interest than practical value)?

Cheers,
Simon.

JHor...@online.microsoft.com (John Hornick [MSFT]) wrote in message news:<StcU4FHe...@cpmsftngxa06.phx.gbl>...

Nak

unread,
Sep 11, 2003, 4:05:33 PM9/11/03
to
I understand totally how strange that is. I have had problems with the
GetThumbnailImage method before, so I stopped using it, I had to actually
call the routine twice to get a good quality image, if I just called it once
the image would be really bad (stretched) quality. Bodges come in very
handy, it's just a shame that they need to be used sometimes!

John Hornick [MSFT]

unread,
Sep 12, 2003, 10:43:06 AM9/12/03
to
Hi,

> I've written a little test app to play around with closing streams (or
> not) and it seems to sort the problem out (The error was raised by a
> client and it was his code that closed the stream - so problem
> solved!). However...
>
> It is interesting that in my "solution" (I was under pressure to get
> it fixed fast so call it a "bodge" if you like :-) ) I closed the
> input stream, then flipped the image and finally saved it - and
> closing the input stream had no ill-effect on the flip operation.
>
> This makes me wonder: why Image.RotateFlip() should succeed and yet
> Image.Save() (both operations surely need access to the same image
> data) fails if the input stream is closed (I know very little about
> the inner workings of GDI etc. so forgive me if this is a stupid
> question! - This more out of pure interest than practical value)?


The rules of engagement with GDI+ are that the stream/file/memory must
remain open/viable for the life of the Bitmap. If you close the source
stream and continue to use the Bitmap, you will get undefined behavior.

We have an article on this coming out (soon?), but it's currently
stuck in edit-review.

0 new messages