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]
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...
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>...
Oooh! That deserves a prize for bodging! ;-)
Nick.
--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."
Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
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.
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>...
> 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.